android 使用Service和Activity通过广播通信

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是意图过滤器,用来过滤是否接受广播内容以及接受的方式;




转载于:https://www.cnblogs.com/lylylyly/p/7028191.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值