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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

与时俱进   2021-03-22 我要评论
想了解Android实现开机自动启动Service或app的方法的相关内容吗,与时俱进在本文为您仔细讲解Android开机自动启动Service或app的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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>

第三步:添加权限

复制代码 代码如下:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们