android实现开机自动启动Service或app
2016-03-23 10:16
483 查看
第一步:首先创建一个广播接收者,重构其抽象方法 onReceive(Context context, Intent intent),在其中启动你想要启动的Service或app。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootBroadcastReceiver extends BroadcastReceiver {
//重写onReceive方法
@Override
public void onReceive(Context context, Intent intent) {
//后边的XXX.class就是要启动的服务
Intent service = new Intent(context,XXXclass);
context.startService(service);
Log.v("TAG", "开机自动服务自动启动.....");
//启动应用,参数为需要自动启动的应用的包名
Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
context.startActivity(intent );
}
}
第二步:配置xml文件,在receiver接收这种添加intent-filter配置
<receiver android:name="BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
第三步:添加权限 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootBroadcastReceiver extends BroadcastReceiver {
//重写onReceive方法
@Override
public void onReceive(Context context, Intent intent) {
//后边的XXX.class就是要启动的服务
Intent service = new Intent(context,XXXclass);
context.startService(service);
Log.v("TAG", "开机自动服务自动启动.....");
//启动应用,参数为需要自动启动的应用的包名
Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
context.startActivity(intent );
}
}
第二步:配置xml文件,在receiver接收这种添加intent-filter配置
<receiver android:name="BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
第三步:添加权限 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
相关文章推荐
- Android屏蔽Home键、屏蔽下拉菜单最有效的办法(部分手机无效)
- Android ViewPager+Fragment滑动选项卡,tab点击选项卡
- Application的onCreate方法被调用多次的问题
- 详解Android App中创建ViewPager组件的方法
- python AttributeError: 'module' object has no attribute 'setdefaultencoding'
- android自定义PopWindow底部显示
- 获取spring的ApplicationContext几种方式
- android中handler用法总结
- iOS开发~CocoaPods使用详细说明
- RN组件之ViewPagerAndroid
- Android 6.0 运行时权限处理完全解析
- android安全——混淆技术小摘
- Android初探之QQ登录界面
- Mapped Statements collection does not contain value for
- 关于微信公众号jsapi支付传值的问题
- 【转载】Android ActionBar完全解析
- 为cordova生成的android应用签名
- Android获取应用版本号与版本名称
- 这段时间学了不少的知识,从今天开始来写一些android方面的文章。
- Androidstudio代码混淆