精品伊人久久大香线蕉,开心久久婷婷综合中文字幕,杏田冲梨,人妻无码aⅴ不卡中文字幕

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Android之Service學習篇一:Service啟動方式之startService
     1.  在同一個應用任何地方調用 startService() 方法就能啟動 Service 了,然后系統會回調 Service 類的 onCreate() 以及 onStart() 方法。這樣啟動的 Service 會一直運行在后臺,直到 Context.stopService() 或者 selfStop() 方法被調用。另外如果一個 Service 已經被啟動,其他代碼再試圖調用 startService() 方法,是不會執行 onCreate() 的,但會重新執行一次 onStart() 。
 
      2. 另外一種 bindService() 方法的意思是,把這個 Service 和調用 Service 的客戶類綁起來,如果調用這個客戶類被銷毀,Service 也會被銷毀。用這個方法的一個好處是,bindService() 方法執行后 Service 會回調上邊提到的 onBind() 方發,你可以從這里返回一個實現了 IBind 接口的類,在客戶端操作這個類就能和這個服務通信了,比如得到 Service 運行的狀態或其他操作。如果 Service 還沒有運行,使用這個方法啟動 Service 就會 onCreate() 方法而不會調用 onStart()。
 
區別概況為:
    startService() 的調用者與服務沒有聯系,即使調用者退出了,服務仍然運行,而bindService() 的調用者與服務綁在一起,調用者一旦退出了,服務也隨即終止掉。www.2cto.com
 
這里用一個實例(使用startService()方法來啟動)來講解一下service的聲明周期和使用方法:
 
首先編寫一個類繼承Service這個基類,重寫里面的方法,然后在Activity中調用startService()和stopService()來啟動和停止服務。
運行界面:
 
工程目錄結構:
 
ExampleService.java
[html] 
package com.service.activity;  
  
import android.app.Service;  
import android.content.Intent;  
import android.os.IBinder;  
import android.util.Log;  
  
public class ExampleService extends Service{  
    private static final String TAG = "ExampleService";   
  
    @Override  
    public void onCreate() {  
        Log.i(TAG, "ExampleService-onCreate");  
        super.onCreate();  
    }  
  
    @Override  
    public void onStart(Intent intent, int startId) {  
        Log.i(TAG, "ExampleService-onStart");  
        super.onStart(intent, startId);  
    }  
  
    @Override  
    public int onStartCommand(Intent intent, int flags, int startId) {  
        //執行文件的下載或者播放等操作  
        Log.i(TAG, "ExampleService-onStartCommand");  
        /*  
         * 這里返回狀態有三個值,分別是:  
         * 1、START_STICKY:當服務進程在運行時被殺死,系統將會把它置為started狀態,但是不保存其傳遞的Intent對象,之后,系統會嘗試重新創建服務;  
         * 2、START_NOT_STICKY:當服務進程在運行時被殺死,并且沒有新的Intent對象傳遞過來的話,系統將會把它置為started狀態,  
         *   但是系統不會重新創建服務,直到startService(Intent intent)方法再次被調用;  
         * 3、START_REDELIVER_INTENT:當服務進程在運行時被殺死,它將會在隔一段時間后自動創建,并且最后一個傳遞的Intent對象將會再次傳遞過來。  
         */  
        return super.onStartCommand(intent, flags, startId);  
    }  
  
    @Override  
    public IBinder onBind(Intent intent) {  
        Log.i(TAG, "ExampleService-onBind");  
        return null;  
    }  
      
    @Override  
    public void onDestroy() {  
        Log.i(TAG, "ExampleService-onDestroy");  
        super.onDestroy();  
    }  
  
}  
 
MainActivity.java
[html] 
package com.service.activity;  
  
import android.app.Activity;  
import android.content.Intent;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
  
public class MainActivity extends Activity implements OnClickListener{  
    private static final String TAG = "MainActivity";   //日志輸出標志  
    private Button btnStartService;  
    private Button btnStopService;  
      
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        btnStartService = (Button)findViewById(R.id.btnStartService);  
        btnStopService = (Button)findViewById(R.id.btnStopService);  
        btnStartService.setOnClickListener(this);  
        btnStopService.setOnClickListener(this);  
    }  
    //點擊事件處理監聽器  
    @Override  
    public void onClick(View v) {  
        Intent intent = new Intent(MainActivity.this,ExampleService.class);  
        switch(v.getId()){  
        case R.id.btnStartService:  
            startService(intent);  
            break;  
        case R.id.btnStopService:  
            stopService(intent);  
            break;  
        default:  
            break;  
        }  
    }  
}  
 
最后在AndroidManifest.xml中對service進行聲明,它跟Activity同一級,都寫在Application標簽里面:
<service android:name=".ExampleService"/>
 
創建完運行
 
在運行點擊"啟動service"之后(第一次啟動service),我們可以查看LogCat控制臺輸出的日志如下:
這個時候我們點擊"返回",Activity被干掉了,但是我們的服務仍然在運行,可以查看Setting-->Application-->Running Service,截圖如下:
 
然后回到我們的Activity,再次點擊啟動Service,控制臺輸出日志如下:
onCreate()方法沒有被調用,說明它并沒有重新被創建。
然后我們點擊停止Service,輸出日志如下:
本站僅提供存儲服務,所有內容均由用戶發布,如發現有害或侵權內容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Android Service 服務(一)
一個Android Service小例子
Android中Service與IntentService的使用比較
android service 學習(上)
16、從頭學Android之Service初步一
一個帖子學會Android開發四大組件
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服

主站蜘蛛池模板: 武城县| 枣庄市| 鹰潭市| 江川县| 将乐县| 黔西县| 睢宁县| 六盘水市| 新郑市| 林甸县| 齐河县| 大田县| 涡阳县| 嘉祥县| 白银市| 繁昌县| 新安县| 镇安县| 苗栗县| 故城县| 清水河县| 永嘉县| 仙居县| 阿克| 禄丰县| 三江| 永靖县| 定结县| 临夏市| 新兴县| 开封县| 天津市| 宁城县| 体育| 临夏县| 高青县| 徐水县| 永德县| 资中县| 临海市| 棋牌|