当前位置: 移动技术网 > 移动技术>移动开发>Android > Android实现开机自动启动Service或app的方法

Android实现开机自动启动Service或app的方法

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例讲述了android实现开机自动启动service或app的方法。分享给大家供大家参考,具体如下:

第一步:首先创建一个广播接收者,重构其抽象方法 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" />

更多关于android相关内容感兴趣的读者可查看本站专题:《android编程之activity操作技巧总结》、《android数据库操作技巧总结》、《android开发入门与进阶教程》、《android资源操作技巧汇总》、《android文件操作技巧汇总》、《android视图view技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网