Service启动demo

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值