android 2.2 通信
android客户端和服务端通信返回的数据需要修改界面的控件的时候必须在子线程中,不能在ui线程;
可以在界面中建立一个子线程,一般继承AsyncTask ,在doInBackground中和服务端交互,然后在onPostExecute中修改界面,这个方法我认为比较麻烦所有建议用Service方法
Activity和Service通信的方法有好几种,下面着重介绍一下Broadcast(广播的形式)
首先建立service 代码如下:
public class MyService extends Service {
public class LocalBinder extends Binder{
public String stringToSend="I'm the test String";
public MyService getService(){
Log.i("TAG","getService--> "+ MyService.this);
return MyService.this;
}
}
private final IBinder mBinder=new LocalBinder();
@Override
public IBinder onBind(Intent intent){
Log.i("TAG","onBind.........");
return mBinder;
}
@Override
public void onCreate(){
super.onCreate();
Log.i("TAG","onCreate--> "+ MyService.this);
}
@Override
public void onDestroy(){
super.onDestroy();
Log.i("TAG","onDestory--> "+ MyService.this);
}
@Override
public void onStart(Intent intent,int startId){
super.onStart(intent,startId);
Log.i("TAG","onStart--> "+ MyService.this);
}
@Override
public int onStartCommand(Intent intent,int flags,int startId){
Log.i("TAG","onStartCommand--> "+ MyService.this);
return super.onStartCommand(intent,flags,startId);
}
@Override
public boolean onUnbind(Intent intent){
Log.i("TAG","onUnbind--> "+ MyService.this);
return super.onUnbind(intent);
}
public void asyncSendPerson(final String name) {
// 休息5秒,模拟异步任务
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//可以在子线程中直接发送广播
sendContentBroadcast(name);
}
}).start();
}
/*
发送广播
*/
public void sendContentBroadcast(String data)
{
Intent intent=new Intent();
intent.setAction("com.mosi.ll.XXX");
intent.putExtra("data", data);
sendBroadcast(intent);
}
Activity中的代码:
首先声明变量
private ServiceConnection sc; //链接
private MyService myServie;
private ContentReceiver mReceiver;//继承类
onCreate的时候初始化变量
sc=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
myServie=((MyService.LocalBinder)iBinder).getService();
String recStr = ((MyService.LocalBinder)iBinder).stringToSend;
Log.i(TAG,"The String is : " + recStr);
Log.i(TAG, "onServiceConnected : myService ---> " + myServie);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
sc=null;
Log.i(TAG, "onServiceDisconnected : ServiceConnection --->"+ sc);
}
};
bindService(new Intent(Main.this,MyService.class),sc,BIND_AUTO_CREATE);
doRegisterReceiver();
public class ContentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String name = intent.getStringExtra("data");
jsonButton.setText(name);//修改界面
}
}
private void doRegisterReceiver(){
mReceiver=new ContentReceiver();
IntentFilter filter = new IntentFilter(
"com.mosi.ll.XXX");
registerReceiver(mReceiver, filter);
}
@Override
protected void onDestroy(){
super.onDestroy();
unbindService(sc);
if(mReceiver!=null){
unregisterReceiver(mReceiver);
}
}
代码写完了以后还需要在AndroidManifest.xml中加入配置文件
<service
android:name=".MyService.MyService"
android:process="com.mosi.ll.XXX">
<intent-filter>
<action android:name="com.mosi.ll.XXX" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
intent-filter是意图过滤器,用来过滤是否接受广播内容以及接受的方式;