源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-04-28 06:21 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android实现开机自动启动Service或app的方法
本文实例讲述了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>

第三步:添加权限
[url=http://www.1sucai.cn/Special/367.htm]Android编程之activity操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/402.htm]Android数据库操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/410.htm]Android开发入门与进阶教程[/url]》、《[url=http://www.1sucai.cn/Special/423.htm]Android资源操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/325.htm]Android文件操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/375.htm]Android视图View技巧总结[/url]》及《[url=http://www.1sucai.cn/Special/124.htm]Android控件用法总结[/url]》 希望本文所述对大家Android程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部