啟動遠端service的兩種方式 顯示和隱式

2021-07-13 17:18:19 字數 1575 閱讀 3810

1、顯式啟動

service端

android:exported= "true">

客戶端:

public static final string name_gesture_detector_remote_service = "com.easyliu.watchgesture.service.gesturedetectorservice" ;

public static final string package_gesture_detector_remote_service = "com.easyliu.watchgestureremoteservice" ;

//啟動服務

intent startintent = new intent ();

componentname componentname = new componentname(

package_gesture_detector_remote_service ,

name_gesture_detector_remote_service);

startintent .setcomponent (componentname );

startservice( startintent) ;

//繫結服務

intent startintent = new intent ();

componentname componentname = new componentname(

package_gesture_detector_remote_service ,

name_gesture_detector_remote_service);

startintent .setcomponent (componentname );

bindservice( startintent, mconnection, context.bind_auto_create) ;

2、隱式啟動

service端:

需要設定乙個action,我們可以把action的名字設定成service的全路徑名字。在這種情況下android:exported預設為true。

客戶端:

需要同時設定action和package,記住之設定action會報錯,必須同時設定其包名。

intent startintent = new intent ();

startintent .setaction ("com.easyliu.watchgesture.service.gesturedetectorservice" );

startintent .setpackage (package_gesture_detector_remote_service);

startservice( startintent) ;

android官網推薦用顯式的方式啟動service。

關於service詳細介紹,請看:

Activity 啟動Service兩種方式與區別

activity 啟動service兩種方式 startservice bindservice startservice intent 1.定義乙個類繼承service 2.在manifest.xml檔案中配置該service 3.使用context的startservice intent 方法啟動...

Service 兩種啟動方式

service的生命週期service的生命週期方法比activity少一些,只有oncreate,onstart,ondestroy 我們有兩種方式啟動乙個service,他們對service生命週期的影響是不一樣的。1通過startservice service會經歷oncreate onsta...

Service 兩種啟動方式

service的生命週期service的生命週期方法比activity少一些,只有oncreate,onstart,ondestroy 我們有兩種方式啟動乙個service,他們對service生命週期的影響是不一樣的。1通過startservice service會經歷oncreate onsta...