android 背景執行方法 IntentService、Service

此方法建議不要長期使用timer長期執行,因為手機螢幕關閉後呈待機狀態timer會停止運作,用sleep(1000)也是會停止計時,想做長期定時通知請看我做定時通知那篇

IntentService是Service的簡易版,已內建一個thread,如果只是想用一個thread就好這個非常適合,建立IntentService比建立Service簡單。

IntentService、Service我做的實驗目前情況:
  1. IntentService在完全關閉activity後會停止(就是關掉手機佔存在背景activity)
  2. Service在完全關閉activity後也會停止,但是能透過重新啟動service的方式重新啟動,可是service的程式就要重新執行一遍。

service 的生命週期


建立class方式


在manifest.xml裡會有

<service
    android:name=".MyService"
    android:enabled="true"
    android:exported="true" />

在activity啟動service方式

//open intentservice
Intent intent = new Intent(this, NotificationService.class);
Log.d(TAG, "service start");
startService(intent);

Service

public class MyService extends Service {

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                
                //...要做的事在這執行...

            }
        }.start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //service關閉後重開方式↓
        startService(new Intent(this, MyService.class));
    }
}

IntentService

public class MyIntentService extends IntentService {
    
    public MyIntentService () {
        super("MyIntentService ");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.v(TAG, "onHandleIntent start");

        //...要做的事在這執行...
    }

    /**
     * Handle action Foo in the provided background thread with the provided
     * parameters.
     */
    private void handleActionFoo(String param1, String param2) {
        // TODO: Handle action Foo
        throw new UnsupportedOperationException("Not yet implemented");
    }

    /**
     * Handle action Baz in the provided background thread with the provided
     * parameters.
     */
    private void handleActionBaz(String param1, String param2) {
        // TODO: Handle action Baz
        throw new UnsupportedOperationException("Not yet implemented");
    }
}



參考自:
官方:
https://developer.android.com/guide/components/services.html?hl=zh-tw
https://developer.android.com/reference/android/app/IntentService.html
https://developer.android.com/reference/android/app/Service.html
其他:
http://givemepass-blog.logdown.com/posts/303090-how-to-use-the-service
https://xnfood.com.tw/android-service/
http://givemepass.blogspot.tw/2015/10/intentservice.html
http://blog.csdn.net/xiao__gui/article/details/11579087
http://www.jianshu.com/p/8a3c44a9173a
http://registerboy.pixnet.net/blog/post/30538333-android-service-%E4%B9%8B%E4%BA%8C%28intentservice%29

留言

這個網誌中的熱門文章

C# 模擬鍵盤滑鼠控制電腦

python nn 聲音辨識 -1 傅立葉轉換

android 定時通知(永久長期的) 本篇只講AlarmManager使用

python pyautogui 簡介

python opencv 基本讀取、轉換、顯示、儲存等