开机自动启动app,后台保持运行Service
2017-07-04 15:59
344 查看
Android启动时,会发出一个系统广播 ACTION_BOOT_COMPLETED,它的字符串常量表示为 “android.intent.action.BOOT_COMPLETED”
开机自启动程序,只需要“捕捉”到这个消息再启动你的程序即可,我们要做的是接收这个消息,并实现一个BroadcastReceiver。
1 xml 配置
在AndroidManifest.xml中Application节点内,添加自定义的广播类:
[html] view plain copy
print?
在AndroidManifest.xml中manifest节点内,添加开机启动权限:
2 自定义广播类 BootReceiver
[java] view plain copy
print?
3 Activity
[java] view plain copy
print?
4 运行结果
重启手机后,自动弹出启动的程序:
开机自启动程序,只需要“捕捉”到这个消息再启动你的程序即可,我们要做的是接收这个消息,并实现一个BroadcastReceiver。
1 xml 配置
在AndroidManifest.xml中Application节点内,添加自定义的广播类:
[html] view plain copy
print?
<receiver android:name=".BootReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </receiver>
在AndroidManifest.xml中manifest节点内,添加开机启动权限:
2 自定义广播类 BootReceiver
[java] view plain copy
print?
public class BootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { // boot Intent intent2 = new Intent(context, MainActivity.class); // intent2.setAction("android.intent.action.MAIN"); // intent2.addCategory("android.intent.category.LAUNCHER"); intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent2); } } }
3 Activity
[java] view plain copy
print?
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
4 运行结果
重启手机后,自动弹出启动的程序:
相关文章推荐
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- Android 开机启动, service 自动运行。
- android如何实现开机自动启动Service或app
- android开机自动启动Service或app
- android如何实现开机自动启动Service或app
- 开机启动一个服务Service,启动后没有界面后台暗暗运行
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- 让Java程序作为linux的Daemon后台运行 和 使用Java Service Wrapper将java程序作为linux服务并且开机自动启动
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app