距离上篇文章竟然快一年了。这次是想明确service一些比较重要的点。
至于什么是service,我也不想多去讨论,我只想清晰确认这么几个问题:
1、service的生命周期到底如何?
2、Activity如何让service做事?
3、service与thread之间有没有关系?
4、远程service是什么东西?
5、AIDL的使用?
6、前台service?
如果需要图,可以百度,好多。我这里直接运行代码打log。
操作顺序是:
startService(intent),service相应执行的是oncreate() ------> onStartCommand() ------> onStart()。
stopService(intent),service相应执行的是onDestroy()。
startService(intent),service相应执行的是oncreate() ------> onStartCommand() ------> onStart()。
startService(intent),service相应执行的是onStartCommand() ------> onStart()。
stopService(intent),service相应执行的是onDestroy()。
这里可见,onCreate() 只会执行一次,即service第一次被启动的时候,在没有destroy之前,继续启动onCreate()不会再执行。
private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub DebugLog("onServiceConnected()"); } };
public boolean bindService(Intent service, ServiceConnection conn, int flags)
public void unbindService(ServiceConnection conn)
public class MyBinder extends Binder{ public void startDownload(){ DebugLog("start download..."); } //获取当前Myservice实例 public MyService getService(){ return MyService.this; } }这个类里面有一个startDownload()方法,模拟下载。
public IBinder onBind(Intent intent) { // TODO Auto-generated method stub DebugLog("onBind()"); return new MyBinder(); }
MyService.MyBinder myBinder = (MyBinder)service; myBinder.startDownload();
private BroadcastReceiver myReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String string = intent.getStringExtra("main"); DebugLog("string:"+string); } };
IntentFilter filter = new IntentFilter(); filter.addAction("com.fleur.mybroadcast"); registerReceiver(myReceiver, filter); DebugLog("registerReceiver success");在service中的onDestroy()函数中反注册:
unregisterReceiver(myReceiver);
case R.id.button8: Intent intent5 = new Intent("com.fleur.mybroadcast"); intent5.putExtra("main", "this broadcast is from mainActivity"); sendBroadcast(intent5); break;
public interface OnProgressListener { public void onProgress(int progress); }
//更新进度的回调接口 private OnProgressListener onProgressListener; /** * 注册回调接口的方法 * @param onProgressListener */ public void setOnProgressListener(OnProgressListener onProgressListener) { this.onProgressListener = onProgressListener; }然后再Service里面声明一个方法,依然模拟下载:
public void startDownload(){ new Thread(){ @Override public void run() { while(progress < MAX_PROGRESS){ progress += 5; //进度发生变化时告诉调用方 if(onProgressListener!=null){ onProgressListener.onProgress(progress); } try { sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }.start(); }
public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub DebugLog("onServiceConnected()"); myBinder = (MyBinder) service; myService = myBinder.getService(); myService.setOnProgressListener(new OnProgressListener() { @Override public void onProgress(int progress) { // TODO Auto-generated method stub progressBar.setProgress(progress); } }); }
//然后自己写一个button控制,使用得到的MyService对象调用startDownload()方法,实现回调。
myService.startDownload();
<service android:name="com.fleur.service.MyRemoteService" android:process=":remote"> <intent-filter > <action android:name="com.fleur.service.MyRemoteService"/> </intent-filter>添加了一句话:android:process=":remote"
package com.fleur.service; interface MyAIDLService{ String toUpperCase(String str); }
import com.fleur.service.MyAIDLService.Stub;
Stub mBinder = new Stub() { @Override public String toUpperCase(String str) throws RemoteException { // TODO Auto-generated method stub if(str!=null){ return str.toUpperCase(); } return null; } };其实stub是aidl文件里的,它继承了Binder实现了我们自己写的接口。
private MyAIDLService myAIDLService; private ServiceConnection conn = new ServiceConnection( ) { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub myAIDLService = MyAIDLService.Stub.asInterface(service); try { String upperStr = myAIDLService.toUpperCase("hello world"); DebugLog("upperStr = "+upperStr); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } };这样在去bindService就OK了。实现了不同进程间的通信。
Notification notification = new Notification(R.drawable.ic_launcher, "Android_Component", System.currentTimeMillis()); Intent notificationIntent = new Intent(this,MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "title of notification", "content of notification", pendingIntent); startForeground(1, notification);
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/u014470702/article/details/47617571