Android 开机自启动Service

1、修改AndroidManifest.xml文件

// 添加接收开机广播的权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

// 注册接收开机广播的receiver
<receiver android:name=".BootBroadcastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <category android:name="android.intent.category.LAUNCHER"/>
     </intent-filter>
 </receiver>

//注册需要启动的Service
<service
    android:name=".TestService"
    android:exported="true"
    android:process="com.test.service">
    <intent-filter>
        <action android:name="com.test.Service" />
    </intent-filter>
</service>


2、recerver中启动service

public class BootBroadcastReceiver extends BroadcastReceiver {
    static final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION_BOOT)){
            Intent mintent = new Intent(context, TestService.class);
            context.startService(mintent);
        }   
    }
}


3、去掉该服务APP的桌面图标

正常APP安装后,在Launcher中会显示图标,由于我们的应用是个后台服务,所以不需要显示图标,不显示桌面图标有两种方式

第一种

去掉Manifest文件中的该属性

<activity
    android:name=".MainActivity"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>
</activity>


备注
这种做法在调试时,不能通过编辑器直接运行,需要编译成APK,再手动安装到设备中。

第二种

在activity的标签中添加

<activity
    android:name=".MainActivity"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        // "com.****.****"为应用包名
        <data android:scheme="com.****.****"/>
    </intent-filter>
</activity>


备注
这种做法在调试时,可以直接在编辑器中运行,相对方便一些,两种方式均可以隐藏桌面图标。

4、将APP放到/system/app目录下

在Android3.1之后,系统为了加强安全性控制,应用程序安装后或是(设置)应用管理中被强制关闭后处于stopped状态,在这种状态下接收不到任何广播。对于android3.1以后版本,如果要应用接收开机广播有两种方法:
a).将应用预置到/system/app/目录。
b).安装应用后先启动一次。(应用只要启动过一次,就不处于stopped状态)

如果你看到了这里,觉得文章写得不错就给个赞呗?
更多Android进阶指南 可以扫码 解锁更多Android进阶资料


在这里插入图片描述
敲代码不易,关注一下吧。ღ( ´・ᴗ・` )

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 9中,应用程序不能自动启动,除非用户明确地启用了自动启动权限。这是为了保护用户的隐私和设备的性能。 如果您需要在设备启动时自动运行脚本,可以考虑创建一个后台服务,并在启动时启动该服务。以下是一个简单的示例: 1. 创建一个后台服务类: ``` public class MyService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { // 在此处编写需要自动运行的脚本 return START_STICKY; } @Override public IBinder onBind(Intent intent) { return null; } } ``` 2. 在AndroidManifest.xml文件中声明该服务: ``` <service android:name=".MyService" android:enabled="true" android:exported="false" /> ``` 3. 在应用程序的启动Activity或Application类中启动该服务: ``` Intent intent = new Intent(this, MyService.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(intent); } else { startService(intent); } ``` 请注意,为了在Android 9及更高版本中正常工作,您需要启动一个前台服务。这可以通过调用startForegroundService()代替startService()来实现。 在启动服务时,您还需要请求自动启动权限。这可以通过向用户显示一个对话框并要求他们手动启用此权限来实现。以下是一个简单的示例: ``` if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { Intent intent = new Intent(); String packageName = getPackageName(); PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE); if (!pm.isIgnoringBatteryOptimizations(packageName)) { intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); intent.setData(Uri.parse("package:" + packageName)); startActivity(intent); } } ``` 请注意,这只是一个示例,您需要根据您的应用程序需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值