Service启动demo

来源:互联网 发布:服务器常用端口 编辑:程序博客网 时间:2024/06/01 09:59
package com.testpublic class BootService extends Service {private static final String TAG = "BootService";//receiver yzyl boot start serviceprivate static final String SERVER_ACTION = "com.test.BootStart";//start boot receiver public static final String BOOT_ACTION = "com.test.BootStart";@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubif(intent == null) return super.onStartCommand(intent, flags, startId);; String action = intent.getAction();Log.i(TAG, "onStartCommand::action : " + action);if(SERVER_ACTION.equals(action)) {boolean isBootStart = ToolUtils.isBootStart(this);Log.i(TAG, "onStartCommand::isBootStart : " + isBootStart);if(isBootStart) {sendBootCompletedReceiver(this);}}return super.onStartCommand(intent, flags, startId);}@Overridepublic IBinder onBind(Intent arg0) {// TODO Auto-generated method stubreturn null;}private void sendBootCompletedReceiver(Context ctx) {if(ctx == null) return;Intent intent = new Intent(BOOT_ACTION);ctx.sendBroadcast(intent);}}//在manifest里声明service和action名字 <service            android:name="com.test.BootService"            android:exported="true" >            <intent-filter>                <action android:name="com.test.BootStart" />            </intent-filter>        </service>//调用的地方  public static void startBootervice(Context mContext) {        if (!AppPkgUtil.isAppInstalled(mContext, JiLinEdog)) {            return;        }        ExternalAppManager.enableSingleApp(mContext, JiLinEdog);        String pkgName = "com.test;   //待启动的包名         String pkgClassName = "com.test.BootService"; //待启动的service名字        Intent explicitIntent = new Intent();        ComponentName comp = new ComponentName(pkgName, pkgClassName);        explicitIntent.setComponent(comp);        explicitIntent.setAction(ACTION_START_TUZHISERVICE);        try {            mContext.startService(explicitIntent);        } catch (Exception e) {            e.printStackTrace();        }    } 


二、调试的打开service的方法

adb shell am startservice -n com.hdsc.edog/.BootService -a com.hdsc.edog.BootStart



0 0
原创粉丝点击