您的位置:首页 > 其它

Service启动demo

2016-12-13 22:06 399 查看
package com.test

public class BootService extends Service {

private static final String TAG = "BootService";
//receiver yzyl boot start service
private static final String SERVER_ACTION = "com.test.BootStart";
//start boot receiver
public static final String BOOT_ACTION = "com.test.BootStart";

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
if(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);
}

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: