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
相关文章推荐
- linux-15基础命令之-用户与用户组(useradd,userdel,usermod,passwd,groupadd)
- 管道命令和xargs的区别
- 使用jsonpath解析json内容
- leetcode-68. Text Justification
- C++中const类型(二)----常指针和常引用
- Oracle查询表空间使用情况
- Swift中函数的默认值
- RxJava使用入门(二)
- 第十五周项目五链接字符串
- 那些年踩过华为手机的坑——相同的代码不同的效果
- Google Chrome调试js入门
- Codevs 2756 树上的路径
- EJB中的@ejb注解
- 练习题答案
- 进制转换
- 安卓辅助功能 滴滴自动刷单
- 基于手机传感器数据使用 CNN 识别用户行为的 Tensroflow 实现
- MWeb自动发布到博客园
- 怎么让浮动的元素和定位的元素水平垂直居中
- LeetCode 165. Compare Version Numbers