Android之Service与Activity通信机制

        在Android中,Activity主要负责前台页面的展示,Service主要负责需要长期运行的任务,所以在我们实际开发中,就会常常遇到Activity与Service之间的通信,我们一般在Activity中启动后台Service,通过Intent来启动,Intent中我们可以传递数据给Service,而当我们Service执行某些操作之后想要更新UI线程,我们应该怎么做呢?接下来我就介绍两种方式来实现Service与Activity之间的通信问题

 

Service知识重温       

 

        Service主要在后台运行,我们可以在这里做网络状态监测,地理位置监测,文件下载以及系统状态监测等,然后通过broadcast广播,发送给前台进行处理。Service不是Thread,Service 是android的一种机制,当它运行的时候如果是Local Service,那么对应的 Service 是运行在主进程的 main 线程上的。如:onCreate,onStart 这些函数在被系统调用的时候都是在主进程的 main 线程上运行的。如果是Remote Service,那么对应的 Service 则是运行在独立进程的 main 线程上。

 

Activity通过IBinder与Service通信

 

       当Activity通过调用bindService(Intent service, ServiceConnection conn,int flags),我们可以得到一个Service的一个对象实例,然后我们就可以访问Service中的方法,我们还是通过一个例子来理解一下吧

MainService.java

public class MainService extends Service{

    private String TAG = "MainService";
    public ServiceBinder mBinder = new ServiceBinder(); /* 数据通信的桥梁 */

    /* 重写Binder的onBind函数,返回派生类 */
    @Override
    public IBinder onBind(Intent arg0) {
        return mBinder;
    }

    @Override
    public void onCreate() {    
        Toast.makeText( MainService.this, "Service Create...", Toast.LENGTH_SHORT).show();
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(MainService.this, "Service StartCommand", Toast.LENGTH_SHORT).show();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {   
        Toast.makeText( MainService.this, "Service Destroy", Toast.LENGTH_SHORT).show();
    }

    /* 第一种模式通信:Binder */
    public class ServiceBinder extends Binder { 

        public void startDownload() throws InterruptedException {  
            /* 模拟下载,休眠2秒 */
            Toast.makeText( MainService.this, "模拟下载2秒钟,开始下载...", Toast.LENGTH_SHORT).show();
            Thread.sleep(2);
            Toast.makeText( MainService.this, "下载结束...", Toast.LENGTH_SHORT).show();
        }
    }
}

 

MainActivity.java

public class MainActivity extends Activity {
    /* 通过Binder,实现Activity与Service通信 */
    private MainService.ServiceBinder mBinderService;
    private ServiceConnection connection = new ServiceConnection() {  
        @Override  
        public void onServiceDisconnected(ComponentName name) {  
        }  

        @Override  
        public void onServiceConnected(ComponentName name, IBinder service) {  
            mBinderService = (MainService.ServiceBinder) service;
            try {
                mBinderService.startDownload();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }  
        }  
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 

        Intent bindIntent = new Intent(MainActivity.this, MainService.class);  
        bindService(bindIntent, connection, BIND_AUTO_CREATE); 
    }
}   

 

Service通过BroadCast广播与Activity通信

 

MainService.java

public class MainService extends Service{

    private String TAG = "MainService";
    public ServiceBinder mBinder = new ServiceBinder(); /* 数据通信的桥梁 */

    /* 重写Binder的onBind函数,返回派生类 */
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {    
        Toast.makeText( MainService.this, "Service Create, Send BroadCast...", Toast.LENGTH_SHORT).show();
        SendServiceBroadCast();
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(MainService.this, "Service StartCommand", Toast.LENGTH_SHORT).show();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {   
        Toast.makeText( MainService.this, "Service Destroy", Toast.LENGTH_SHORT).show();
    }

    /* 第二种模式通信:Broadcast广播 */
    public void SendServiceBroadCast() throws InterruptedException {  
        Log.d(TAG, "ServiceThread===>>startDownload() executed===>>线程ID:"+Thread.currentThread().getId());  
        Toast.makeText( MainService.this, "Send BroadCast now...", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent();
        intent.setAction("me.homeway.servicebinder.BroadcastTest");
        intent.putExtra("value", 1000);
        sendBroadcast(intent);
        Toast.makeText( MainService.this, "Sent! Did you receive?", Toast.LENGTH_SHORT).show();
    }
}

 

BroadcastTest.java

public class BroadcastTest extends BroadcastReceiver{

    private NetworkInfo netInfo;
    private ConnectivityManager mConnectivityManager;

    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            if(extras.containsKey("value")){
                /* 这里可以做下载,发包等事件 */
                Toast.makeText( context, "收到广播 => "+extras.get("value"), Toast.LENGTH_SHORT).show();
                System.out.println("Value is:"+extras.get("value"));
            }
        }     
    }
}

 

AndroidManifest.xml

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name">

    <receiver android:name=".BroadcastTest">
        <intent-filter>
            <action android:name="me.homeway.servicebinder.BroadcastTest"></action>
        </intent-filter>
    </receiver>
    ...    
</application>


 

本文转自 http://segmentfault.com/a/1190000002400391#articleHeader0

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值