自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

android基礎(chǔ)IntentService的使用

移動開發(fā) Android
前臺服務(wù)可以一直保持運行狀態(tài),而不會由于系統(tǒng)內(nèi)存不足的原因?qū)е卤换厥?。我們通過一個具體的案例來說明start與bind方式的service服務(wù)的生命周期的介紹。

服務(wù)的簡單說明

一、 前臺服務(wù)與IntentService:

前臺服務(wù)可以一直保持運行狀態(tài),而不會由于系統(tǒng)內(nèi)存不足的原因?qū)е卤换厥?br />  

service服務(wù)測試的準備代碼

我們通過一個具體的案例來說明start與bind方式的service服務(wù)的生命周期的介紹。項目結(jié)果如下:

一、 在MainActivity.java中做一些初始化工作,如下代碼:

  1. private final static String TAG = "MyIntentService";  
  2. private MyIntentService.MyBinder binder;  
  3.  
  4. private ServiceConnection connection = new ServiceConnection() {  
  5.     @Override  
  6.     public void onServiceConnected(ComponentName name, IBinder service) {  
  7.         binder = (MyIntentService.MyBinder) service;  
  8.         binder.sayHello(name.getClassName());  
  9.     }  
  10.  
  11.     @Override  
  12.     public void onServiceDisconnected(ComponentName name) {  
  13.         Log.i(TAG, "service disconnect: " + name.getClassName());  
  14.     }  
  15. };  
  16.  
  17. @Override  
  18. protected void onCreate(Bundle savedInstanceState) {  
  19.     super.onCreate(savedInstanceState);  
  20.     setContentView(R.layout.activity_main);  

二、 創(chuàng)建一個簡單的IntentService服務(wù)類:MyIntentService

  1. package com.example.linux.intentservicetest;  
  2.  
  3. import android.app.IntentService;  
  4. import android.content.Intent;  
  5. import android.os.Binder;  
  6. import android.os.IBinder;  
  7. import android.util.Log;  
  8.  
  9. public class MyIntentService extends IntentService {  
  10.     private final static String TAG = "MyIntentService";  
  11.     private MyBinder myBinder = new MyBinder();  
  12.  
  13.     class MyBinder extends Binder {  
  14.         public void sayHello(String name) {  
  15.             Log.i(TAG, "say hello method: " + name);  
  16.         }  
  17.  
  18.         public void sayWorld(String name) {  
  19.             Log.i(TAG, "say world method: " + name);  
  20.         }  
  21.     }  
  22.  
  23.     @Override  
  24.     public IBinder onBind(Intent intent) {  
  25.         return myBinder;  
  26.     }  
  27.  
  28.     public MyIntentService() {  
  29.         super("MyIntentService");  
  30.         Log.i(TAG, "myintent service constructor.");  
  31.     }  
  32.  
  33.     @Override  
  34.     public void onCreate() {  
  35.         Log.i(TAG, "on create.");  
  36.         super.onCreate();  
  37.     }  
  38.  
  39.     @Override  
  40.     protected void onHandleIntent(Intent intent) {  
  41.         Log.i(TAG, "handle intent: " + intent.getStringExtra("username") + ", thread: " + Thread.currentThread());  
  42.     }  
  43.  
  44.     @Override  
  45.     public void onDestroy() {  
  46.         super.onDestroy();  
  47.         Log.i(TAG, "on destroy.");  
  48.     }  
  49.  
  50.     @Override  
  51.     public int onStartCommand(Intent intent, int flags, int startId) {  
  52.         Log.i(TAG, "on start command.");  
  53.         return super.onStartCommand(intent, flags, startId);  
  54.     }  
  55.  
  56.     @Override  
  57.     public boolean onUnbind(Intent intent) {  
  58.         //默認返回false  
  59.         String username = intent.getStringExtra("username");  
  60.         Log.i(TAG, "on unbind: " + super.onUnbind(intent) + ", username: " + username);  
  61.         return true;  
  62.     }  
  63.  
  64.     @Override  
  65.     public void onRebind(Intent intent) {  
  66.         Log.i(TAG, "on rebind");  
  67.         super.onRebind(intent);  
  68.     }  

三、 創(chuàng)建一個簡單的前臺服務(wù)類:FrontService

  1. package com.example.linux.intentservicetest;  
  2.  
  3. import android.app.Notification;  
  4. import android.app.PendingIntent;  
  5. import android.app.Service;  
  6. import android.content.Intent;  
  7. import android.os.IBinder;  
  8. import android.util.Log;  
  9.  
  10. public class FrontService extends Service {  
  11.     private final static String TAG = "MyIntentService";  
  12.     public FrontService() {  
  13.         Log.i(TAG, "front service constructor");  
  14.     }  
  15.  
  16.     @Override  
  17.     public IBinder onBind(Intent intent) {  
  18.         return null;  
  19.     }  
  20.  
  21.     @Override  
  22.     public void onCreate() {  
  23.         super.onCreate();  
  24.         Notification.Builder builder = new Notification.Builder(this);  
  25.         Intent intent = new Intent(this, MainActivity.class);  
  26.         PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,  
  27.                 PendingIntent.FLAG_CANCEL_CURRENT);  
  28.  
  29.         builder.setSmallIcon(R.mipmap.ic_launcher).setTicker("ticker");  
  30.         builder.setWhen(System.currentTimeMillis()).setAutoCancel(true);  
  31.         builder.setContentTitle("content title").setContentText("content text");  
  32.         builder.setContentIntent(pendingIntent);  
  33.  
  34.         Notification notify = builder.getNotification();  
  35.  
  36.         notify.defaults = Notification.DEFAULT_ALL;  
  37.         startForeground(10, notify);  
  38.     }  

四、 在AndroidManifest.xml中注冊服務(wù)與活動

  1. <activity android:name=".MainActivity">  
  2.     <intent-filter>  
  3.         <action android:name="android.intent.action.MAIN" />  
  4.         <category android:name="android.intent.category.LAUNCHER" />  
  5.     </intent-filter>  
  6. </activity>  
  7.  
  8. <service  
  9.     android:name=".MyIntentService" 
  10.     android:exported="false">  
  11. </service>  
  12. <service  
  13.     android:name=".FrontService" 
  14.     android:enabled="true" 
  15.     android:exported="true">  
  16. </service> 

Intent服務(wù)的使用

一、 在MainActivity中創(chuàng)建方法,啟動停止服務(wù):

  1. // 開啟服務(wù)  
  2. public void startService(View view) {  
  3.     Intent intent = new Intent();  
  4.     intent.putExtra("username""linux");  
  5.     intent.setClass(MainActivity.this, MyIntentService.class);  
  6.     startService(intent);  
  7. }  
  8.  
  9. // 停止服務(wù)  
  10. public void stopService(View view) {  
  11.     Intent intent = new Intent();  
  12.     intent.setClass(MainActivity.this, MyIntentService.class);  
  13.     stopService(intent);  

二、 在MainActivity中創(chuàng)建方法,綁定解綁服務(wù):

  1. // 綁定服務(wù)  
  2. public void bindService(View view) {  
  3.     Intent intent = new Intent();  
  4.     intent.setClass(MainActivity.this, MyIntentService.class);  
  5.     intent.putExtra("username""linux");  
  6.     boolean isBind = bindService(intent, connection, Context.BIND_AUTO_CREATE);  
  7.     Log.i(TAG, "bind service: " + isBind);  
  8. }  
  9.  
  10. // 解綁服務(wù)  
  11. public void unbindService(View view) {  
  12.     Intent intent = new Intent();  
  13.     intent.setClass(MainActivity.this, MyIntentService.class);  
  14.     unbindService(connection);  

三、 運行結(jié)果

點擊start:
 

  1. 03-25 08:01:53.460 8389-8389/? I/MyIntentService: myintent service constructor.  
  2. 03-25 08:01:53.460 8389-8389/? I/MyIntentService: on create.  
  3. 03-25 08:01:53.475 8389-8389/? I/MyIntentService: on start command.  
  4. 03-25 08:01:53.477 8389-8727/? I/MyIntentService: handle intent: linux, thread: Thread[IntentService[MyIntentService],5,main]  
  5. 03-25 08:01:53.478 8389-8389/? I/MyIntentService: on destroy. 


點擊stop:無輸出
點擊bind:
 

  1. 03-25 08:02:25.421 8389-8389/? I/MyIntentService: bind service: true 
  2. 03-25 08:02:25.422 8389-8389/? I/MyIntentService: myintent service constructor.  
  3. 03-25 08:02:25.422 8389-8389/? I/MyIntentService: on create.  
  4. 03-25 08:02:25.432 8389-8389/? I/MyIntentService: say hello method: com.example.linux.intentservicetest.MyIntentService 

點擊unbind:

  1. 03-25 08:02:28.486 8389-8389/? I/MyIntentService: on unbind: false, username: linux  
  2. 03-25 08:02:28.490 8389-8389/? I/MyIntentService: on destroy. 

前臺服務(wù)的使用

一、 在MainActivity中創(chuàng)建方法,啟動前臺服務(wù):

  1. // 前臺服務(wù)的使用  
  2. public void frontService(View view) {  
  3.     Intent intent = new Intent();  
  4.     intent.setClass(MainActivity.this, FrontService.class);  
  5.     startService(intent);  

二、 運行結(jié)果: 在手機的通知欄中

 

IntentService的原理分析

一、 intentService是繼承Service的抽象方法

  1. public abstract class IntentService extends Service  

二、 intentService包含的一些字段引用如下

  1. private volatile Looper mServiceLooper;  
  2. private volatile ServiceHandler mServiceHandler;  
  3. private String mName;  
  4. private boolean mRedelivery;  
  5.  
  6. private final class ServiceHandler extends Handler {  
  7.     public ServiceHandler(Looper looper) {  
  8.         super(looper);  
  9.     }  
  10.  
  11.     @Override  
  12.     public void handleMessage(Message msg) {  
  13.         onHandleIntent((Intent)msg.obj);  
  14.         stopSelf(msg.arg1);  
  15.     }  

二、 和service一樣在啟動的時候,首先是執(zhí)行構(gòu)造方法,接著是onCreate方法,然后是onStartCommand方法,在onStartCommand中執(zhí)行了onStart方法(執(zhí)行流程在android基礎(chǔ)---->service的生命周期講過):

onCreate方法,開啟了一個線程,并且得到Looper與初始化了一個Handler
 

  1. @Override  
  2. public void onCreate() {  
  3.     // TODO: It would be nice to have an option to hold a partial wakelock  
  4.     // during processing, and to have a static startService(Context, Intent)  
  5.     // method that would launch the service & hand off a wakelock.  
  6.  
  7.     super.onCreate();  
  8.     HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");  
  9.     thread.start();  
  10.  
  11.     mServiceLooper = thread.getLooper();  
  12.     mServiceHandler = new ServiceHandler(mServiceLooper);  

onStart方法,用上述的Handler發(fā)送信息

  1. @Override  
  2. public void onStart(Intent intent, int startId) {  
  3.     Message msg = mServiceHandler.obtainMessage();  
  4.     msg.arg1 = startId;  
  5.     msg.obj = intent;  
  6.     mServiceHandler.sendMessage(msg);  

onStartCommand方法,調(diào)用onStart方法,發(fā)送信息

  1. @Override  
  2. public int onStartCommand(Intent intent, int flags, int startId) {  
  3.     onStart(intent, startId);  
  4.     return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;  
  5. }  
  6.  


***上述的Handler得到信息,調(diào)用handleMessage方法,其中有stopSelf(msg.arg1)方法,停止了服務(wù):

 

三、 這里附上service類的兩個方法,源代碼是android6.0的

在Service中的onStart方法已經(jīng)被廢棄了:
 

  1. /**  
  2.  * @deprecated Implement {@link #onStartCommand(Intent, int, int)} instead.  
  3. */ 
  4. @Deprecated  
  5. public void onStart(Intent intent, int startId) {  

在onStartCommand的方法中

  1. public int onStartCommand(Intent intent, int flags, int startId) {  
  2.     onStart(intent, startId);  
  3.     return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY;  
  4. }  
  5.  

 

責任編輯:陳琳 來源: huhx的博客
相關(guān)推薦

2024-05-09 08:25:38

AndroidServiceLooper

2016-11-14 19:15:37

Android

2023-12-06 08:26:19

Service數(shù)據(jù)庫

2024-12-03 15:50:00

Matplotlib可視化

2011-06-02 10:24:48

Android SQLite

2009-06-30 11:33:55

腳本JSP教程

2010-06-28 15:31:22

2016-05-10 11:22:13

軟件定義IT基礎(chǔ)

2016-01-26 10:51:50

2014-07-28 14:43:14

git開源

2023-07-04 08:28:27

2011-06-16 11:01:56

PHP繼承

2011-05-04 10:29:30

投影機

2010-02-03 15:59:08

Android組件

2011-06-01 13:22:25

Android Alarm

2011-05-27 11:34:53

Android preference

2022-02-03 11:23:26

計算機視覺庫OpenCV

2015-01-12 13:48:55

Android應(yīng)用組件

2010-01-26 17:42:14

Android浮點

2014-12-31 14:27:19

ToastLogLSharePrefe
點贊
收藏

51CTO技術(shù)棧公眾號