实例介绍
【实例简介】客户端是 android java代码,电脑端是 c 代码
【实例截图】

【核心代码】
package com.liaowf.androidpc.androidclient;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class AndroidClient extends Activity {
private LinearLayout linearlayout_layout = null;
private Button connwifiButton = null,getpcButton = null,shutdownButton = null,
rebootButton = null,logoffButton = null,sleepButton = null,lockButton = null;
private TextView stateTxtView = null;
private int command_port = 31696;
public TextView getStateTxtView(){
return this.stateTxtView;
}
public LinearLayout getLinearLayout(){
return this.linearlayout_layout;
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
shutdownButton = (Button)this.findViewById(R.id.button_shutdown);
rebootButton = (Button)this.findViewById(R.id.button_reboot);
logoffButton = (Button)this.findViewById(R.id.button_logoff);
sleepButton = (Button)this.findViewById(R.id.button_sleep);
lockButton = (Button)this.findViewById(R.id.button_lock);
getpcButton = (Button)this.findViewById(R.id.button_getpc);
stateTxtView = (TextView)this.findViewById(R.id.textView_statetext);
connwifiButton = (Button)this.findViewById(R.id.button_connwifi);
// connwifiButton.setEnabled(false);
linearlayout_layout = (LinearLayout)this.findViewById(R.id.linearlayout_layout);
connwifiButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));//进入无线网络配置界面
}
});
getpcButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
UDPIPHostNameAsyncTask task = new UDPIPHostNameAsyncTask(AndroidClient.this);
UDPIPHostNameArg arg = new UDPIPHostNameArg();
arg.broadcast_ip = Config.broadcast_ip;
arg.broadcast_port = Config.broadcast_port;
arg.command = Config.iphostname_cmd;
task.execute(arg);
}
});
shutdownButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// Dialog dialog = new Dialog(AndroidClient.this);
showInformation("确认关机吗?", Config.shutdown_cmd);
}
});
rebootButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
showInformation("确定重启吗?", Config.reboot_cmd);
}
});
logoffButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
showInformation("确定注销吗?", Config.logoff_cmd);
}
});
sleepButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showInformation("确定睡眠吗?", Config.sleep_cmd);
}
});
lockButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showInformation("确定锁定吗?", Config.lock_cmd);
}
});
}
private void onclickCommand(String command){
CommandAsyncTask task = new CommandAsyncTask(AndroidClient.this);
CommandArg arg = new CommandArg();
List<ComputerInfo> cList = getSelectComputer();
if(cList==null || cList.size()==0){
Toast.makeText(AndroidClient.this, "你没有选择控制电脑", Toast.LENGTH_SHORT).show();
stateTxtView.setText("你没有选择控制电脑");
}else {
arg.computerInfos = cList;
arg.port = command_port;
arg.command = command;
task.execute(arg);
}
}
private List<ComputerInfo> getSelectComputer(){
List<ComputerInfo> cList = new ArrayList<ComputerInfo>();
int childcount = linearlayout_layout.getChildCount();
for(int i=0;i<childcount;i ){
View view = linearlayout_layout.getChildAt(i);
if(view instanceof CheckBox){
CheckBox _checkbox = (CheckBox)view;
String _str = _checkbox.getText().toString();
int _i = _str.indexOf("-");
if((_i!=-1) && _checkbox.isChecked()==true){
ComputerInfo c = new ComputerInfo();
c.IP = _str.substring(0,_i);
c.hostName = _str.substring(_i 1);
cList.add(c);
}
}
}
return cList;
}
private boolean showInformation(String msg,final String cmd)
{
AlertDialog.Builder builder = new Builder(AndroidClient.this);
builder.setMessage(msg);
builder.setTitle("提示");
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Log.v("androidClient", cmd);
AndroidClient.this.onclickCommand(cmd);
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
return true;
}
}
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论