I tried using the sample code in this tutorial but it seems outdated and it did not work. So what changes do I have to make and to what files to have my app start automatically when Android finishes booting up?
Then create a new class yourActivityRunOnStartup (matching the android:name specified for the <receiver> element in the manifest):
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);
}
}
}
Note:
The call i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); is important because the activity is launched from a context outside the activity. Without this, the activity will not start.
Also, the values android:enabled, android:exported and android:permission in the <receiver> tag do not seem mandatory. The app receives the event without these values. See the example here.
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);
}
}
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);
}
}
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);
}
}}
我们需要 Draw Overlay 权限android 10
所以将其添加到您的第一个活动中
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
}
}
}
For Android 10 there is background restrictions.
For android 10 and all version of android follow this steps to start an app after a restart or turn on mobile
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);
}
}}
We need Draw overlay permission for android 10
so add this in your first activity
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
}
}
}
Sean 的解决方案最初对我不起作用(Android 4.2.2)。我必须向同一个 Android 项目添加一个虚拟活动,并在设备上手动运行该活动至少一次。然后 Sean 的解决方案开始工作,并在随后的重新启动后通知 BroadcastReceiver。
The Sean's solution didn't work for me initially (Android 4.2.2). I had to add a dummy activity to the same Android project and run the activity manually on the device at least once. Then the Sean's solution started to work and the BroadcastReceiver was notified after subsequent reboots.
I would like to add one point in this question which I was facing for couple of days. I tried all the answers but those were not working for me. If you are using android version 5.1 please change these settings.
If you are using android version 5.1 then you have to dis-select (Restrict to launch) from app settings.
settings> app > your app > Restrict to launch (dis-select)
Another approach is to use android.intent.action.USER_PRESENT instead of android.intent.action.BOOT_COMPLETED to avoid slow downs during the boot process. But this is only true if the user has enabled the lock Screen - otherwise this intent is never broadcasted.
发布评论
评论(10)
这阻止了我的应用程序,请执行此操作 - 它会弹出,用户选中该框以授予权限
This was preventing my app please do this - it pops and user checks the box to give permission
首先,您需要在
AndroidManifest.xml
中获得权限:此外,在
AndroidManifest.xml
中定义您的服务并侦听 BOOT_COMPLETED 操作:然后您需要定义接收器来获取 BOOT_COMPLETED 操作并启动您的服务。
现在,当手机启动时,您的服务应该正在运行。
First, you need the permission in your
AndroidManifest.xml
:Also, in your
AndroidManifest.xml
, define your service and listen for the BOOT_COMPLETED action:Then you need to define the receiver that will get the BOOT_COMPLETED action and start your service.
And now your service should be running when the phone starts up.
这是如何让 activity 在 Android 设备重新启动后开始运行:
将此代码插入到
AndroidManifest.xml 文件中> 元素(不在
元素内):然后创建一个新类
yourActivityRunOnStartup
(与android:name< /code> 指定为清单中的
元素):注意:
调用 i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 很重要,因为 Activity 是从 Activity 外部的上下文启动的。如果没有这个,活动将无法开始。
此外,
标记中的值android:enabled
、android:exported
和android:permission
似乎不是强制性的。应用程序接收没有这些值的事件。请参阅此处的示例。This is how to make an activity start running after android device reboot:
Insert this code in your
AndroidManifest.xml
file, within the<application>
element (not within the<activity>
element):Then create a new class
yourActivityRunOnStartup
(matching theandroid:name
specified for the<receiver>
element in the manifest):Note:
The call
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
is important because the activity is launched from a context outside the activity. Without this, the activity will not start.Also, the values
android:enabled
,android:exported
andandroid:permission
in the<receiver>
tag do not seem mandatory. The app receives the event without these values. See the example here.监听 ACTION_BOOT_COMPLETE 并从那里执行您需要的操作。这里有一个代码片段。
更新:
答案上的原始链接已关闭,因此根据评论,这里是链接代码,因为当链接关闭时没有人会错过代码。
AndroidManifest.xml(应用程序部分)中:
……
在
来源:https://web.archive.org/web/20150520124552/http://www.androidsnippets.com/autostart-an-application-at-bootup
Listen for the ACTION_BOOT_COMPLETE and do what you need to from there. There is a code snippet here.
Update:
Original link on answer is down, so based on the comments, here it is linked code, because no one would ever miss the code when the links are down.
In AndroidManifest.xml (application-part):
...
...
Source: https://web.archive.org/web/20150520124552/http://www.androidsnippets.com/autostart-an-application-at-bootup
此外,如果您不想修改代码,则可以使用 AutoStart 等应用程序在启动时启动 Android 应用程序:自动启动 - 无 root
Additionally you can use an app like AutoStart if you dont want to modify the code, to launch an android application at startup: AutoStart - No root
Android 10 有后台限制。
对于 Android 10 和所有版本的 Android,请按照以下步骤在重新启动或打开移动设备后启动应用程序
在 Android Manifest 中添加这两个权限
将其添加到您的应用程序标签中
添加此类以在启动时启动 Activity
我们需要 Draw Overlay 权限android 10
所以将其添加到您的第一个活动中
For Android 10 there is background restrictions.
For android 10 and all version of android follow this steps to start an app after a restart or turn on mobile
Add this two permission in Android Manifest
Add this in your application tag
Add this class to start activity when boot up
We need Draw overlay permission for android 10
so add this in your first activity
对于 flutter 用户,您可以在包文件夹中创建一个名为
MainActivityReceiver.kt
的文件。例如。android/app/src/main/kotlin/com/your_company/package
。MainActivityReceiver.kt
:修改您的
AndroidManifest.xml
文件,参考第一个答案。For flutter user, you can create a file named
MainActivityReceiver.kt
in package folder. eg.android/app/src/main/kotlin/com/your_company/package
.MainActivityReceiver.kt
:Modify your
AndroidManifest.xml
file refer to the first answer.Sean 的解决方案最初对我不起作用(Android 4.2.2)。我必须向同一个 Android 项目添加一个虚拟活动,并在设备上手动运行该活动至少一次。然后 Sean 的解决方案开始工作,并在随后的重新启动后通知 BroadcastReceiver。
The Sean's solution didn't work for me initially (Android 4.2.2). I had to add a dummy activity to the same Android project and run the activity manually on the device at least once. Then the Sean's solution started to work and the BroadcastReceiver was notified after subsequent reboots.
我想在我几天来面临的这个问题中添加一点。我尝试了所有答案,但这些答案对我不起作用。如果您使用的是 Android 5.1 版本,请更改这些设置。
如果您使用的是 Android 版本 5.1,则必须从应用程序设置中取消选择(限制启动)。
设置>应用程序>您的应用程序>限制启动(取消选择)
I would like to add one point in this question which I was facing for couple of days. I tried all the answers but those were not working for me. If you are using android version 5.1 please change these settings.
If you are using android version 5.1 then you have to dis-select (Restrict to launch) from app settings.
settings> app > your app > Restrict to launch (dis-select)
另一种方法是使用
android.intent.action.USER_PRESENT
而不是android.intent.action.BOOT_COMPLETED
以避免启动过程中的速度减慢。但只有当用户启用了锁定屏幕时,这才是true
- 否则这个意图永远不会被广播。参考博客 - Android 的 ACTION_USER_PRESENT Intent 问题
Another approach is to use
android.intent.action.USER_PRESENT
instead ofandroid.intent.action.BOOT_COMPLETED
to avoid slow downs during the boot process. But this is onlytrue
if the user has enabled the lock Screen - otherwise this intent is never broadcasted.Reference blog - The Problem With Android’s ACTION_USER_PRESENT Intent