Android 手机启动时如何启动我的应用程序?

问题描述 投票:0回答:10

我尝试使用示例代码在本教程中,但它似乎已经过时并且不起作用。那么我需要进行哪些更改以及哪些文件才能让我的应用程序在 Android 完成启动时自动启动?

android broadcastreceiver startup
10个回答
339
投票

首先,您需要获得您的

AndroidManifest.xml
的许可:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

此外,在您的

AndroidManifest.xml
中,定义您的服务并监听 BOOT_COMPLETED 操作:

<service android:name=".MyService" android:label="My Service">
    <intent-filter>
        <action android:name="com.myapp.MyService" />
    </intent-filter>
</service>

<receiver
    android:name=".receiver.StartMyServiceAtBootReceiver"
    android:label="StartMyServiceAtBootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

然后您需要定义接收器来获取 BOOT_COMPLETED 操作并启动您的服务。

public class StartMyServiceAtBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent serviceIntent = new Intent(context, MyService.class);
            context.startService(serviceIntent);
        }
    }
}

现在您的服务应该在手机启动时运行。


129
投票

这是如何让 activity 在 Android 设备重启后开始运行:

将此代码插入到您的

AndroidManifest.xml
文件中的
<application>
元素内(不是
<activity>
元素内):

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<receiver
    android:enabled="true"
    android:exported="true" 
    android:name="yourpackage.yourActivityRunOnStartup"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

</receiver>

然后创建一个新类

yourActivityRunOnStartup
(与清单中为
android:name
元素指定的
<receiver>
匹配):

package yourpackage;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class yourActivityRunOnStartup extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Intent i = new Intent(context, MainActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }

}

注意: 调用

i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
很重要,因为该活动是从该活动之外的上下文启动的。如果没有这个,活动将无法开始。

此外,

android:enabled
标签中的值
android:exported
android:permission
<receiver>
似乎不是强制性的。应用程序接收没有这些值的事件。请参阅示例此处


71
投票

聆听 ACTION_BOOT_COMPLETE 并从那里执行您需要的操作。这里有一个代码片段

更新:

答案上的原始链接已关闭,因此根据评论,这里是链接代码,因为当链接关闭时没有人会错过代码。

在AndroidManifest.xml(应用程序部分)中:

<receiver android:enabled="true" android:name=".BootUpReceiver"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

        <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
</receiver>

...

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

...

public class BootUpReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
                Intent i = new Intent(context, MyActivity.class);  //MyActivity can be anything which you want to start on bootup...
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);  
        }

}

来源:https://web.archive.org/web/20150520124552/http://www.androidsnippets.com/autostart-an-application-at-bootup


9
投票

此外,如果您不想修改代码,您可以使用像 AutoStart 这样的应用程序,在启动时启动 Android 应用程序:AutoStart - No root


7
投票

Android 10 有后台限制。

对于 Android 10 和所有版本的 Android,请按照以下步骤在重新启动或打开手机后启动应用程序

在Android Manifest中添加这两个权限

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

将此添加到您的应用程序标签中

<receiver
        android:name=".BootReciever"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
</receiver>

添加此类以在启动时启动活动

public class BootReciever extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    if (Objects.equals(intent.getAction(), Intent.ACTION_BOOT_COMPLETED)) {
        Intent i = new Intent(context, SplashActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
 }}

我们需要 Android 10 的 Draw Overlay 权限

所以将其添加到您的第一个活动中

 private fun requestPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.canDrawOverlays(this)) {
            val intent = Intent(
                Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + this.packageName)
            )
            startActivityForResult(intent, 232)
        } else {
            //Permission Granted-System will work
        }
    }
}

2
投票

Sean 的解决方案最初对我不起作用(Android 4.2.2)。我必须向同一个 Android 项目添加一个虚拟活动,并在设备上手动运行该活动至少一次。然后 Sean 的解决方案开始工作,并在后续重新启动后通知 BroadcastReceiver。


2
投票

对于 flutter 用户,您可以在包文件夹中创建一个名为

MainActivityReceiver.kt
的文件。例如。
android/app/src/main/kotlin/com/your_company/package

MainActivityReceiver.kt

package com.your_company.package

import android.content.BroadcastReceiver
import android.content.Context;
import android.content.Intent;

class MainActivityReceiver: BroadcastReceiver() {
  override fun onReceive(context: Context, intent: Intent) {
    if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
      val i = Intent(context, MainActivity::class.java)
      i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
      context.startActivity(i)
    }
  }
}

参考第一个答案修改您的

AndroidManifest.xml
文件。


0
投票

screenshot

我想在我几天来面临的这个问题上补充一点。我尝试了所有答案,但这些答案对我不起作用。如果您使用的是 Android 5.1 版本,请更改这些设置。

如果您使用的是 Android 5.1 版本,则必须从应用程序设置中取消选择(限制启动)。

设置>应用程序>您的应用程序>限制启动(取消选择)


0
投票

另一种方法是使用

android.intent.action.USER_PRESENT
而不是
android.intent.action.BOOT_COMPLETED
以避免启动过程中速度减慢。但这仅适用于用户启用了锁定屏幕的情况 - 否则此意图永远不会被广播。

参考博客 -

Android ACTION_USER_PRESENT Intent 的问题


0
投票

true

© www.soinside.com 2019 - 2024. All rights reserved.