I tried using the sample code in this link 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?
我尝试在这个链接中使用样例代码,但它似乎过时了,而且不起作用。那么,当Android完成启动时,我需要做哪些修改,以及哪些文件可以自动启动?
7 个解决方案
#1
273
First, you need the permission in your AndroidManifest.xml
:
首先,您需要在AndroidManifest.xml中获得许可:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Also, in yourAndroidManifest.xml
, define your service and listen for the BOOT_COMPLETED action:
同时,在yourAndroidManifest。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>
Then you need to define the receiver that will get the BOOT_COMPLETED action and start your service.
然后需要定义接收方,接收方将获得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);
}
}
}
And now your service should be running when the phone starts up.
现在你的服务应该在电话启动时运行。
#2
87
This is how to make an activity start running after android device reboot:
以下是如何在android设备重新启动后让活动启动:
Insert this code in your AndroidManifest.xml
file, within the <application>
element (not within the <activity>
element):
将此代码插入到您的AndroidManifest中。xml文件,在
<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>
Then create a new class yourActivityRunOnStartup
(matching the android:name
specified for the <receiver>
element in the manifest):
然后创建一个新的类yourActivityRunOnStartup(匹配清单中为
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.
注意:调用i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);之所以重要,是因为活动是从活动之外的上下文启动的。没有这个,活动就不会开始。
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.
此外,
#3
48
Listen for the ACTION_BOOT_COMPLETE and do what you need to from there. There is a code snippet here.
监听ACTION_BOOT_COMPLETE,然后做你需要做的事情。这里有一个代码片段。
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):
在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
#4
9
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应用程序:自动启动——没有根。
#5
2
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 4.2.2)。我必须向同一个Android项目添加一个虚拟活动,并至少在设备上手动运行一次活动。然后Sean的解决方案开始工作,并在重新启动后通知了BroadcastReceiver。
#6
0
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.
我想在这个问题上增加一点,我已经面临了好几天了。我试了所有的答案,但那些对我不起作用。如果您正在使用android版本5.1,请更改这些设置。
If you are using android version 5.1 then you have to dis-select (Restrict to launch) from app settings.
如果您正在使用android版本5.1,那么您必须从应用程序设置中取消选择(限制启动)。
settings> app > your app > Restrict to launch (dis-select)
>应用>应用>限制启动(dis-select)
#7
-1
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.
另一种方法是使用android.intent.action.USER_PRESENT而不是android.intent.action.BOOT_COMPLETED,以避免在引导过程中出现慢下来。但只有当用户启用了锁屏时,才会出现这种情况——否则这个意图永远不会被广播。
Reference blog - The Problem With Android’s ACTION_USER_PRESENT Intent
参考博客——Android的ACTION_USER_PRESENT意图的问题
#1
273
First, you need the permission in your AndroidManifest.xml
:
首先,您需要在AndroidManifest.xml中获得许可:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Also, in yourAndroidManifest.xml
, define your service and listen for the BOOT_COMPLETED action:
同时,在yourAndroidManifest。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>
Then you need to define the receiver that will get the BOOT_COMPLETED action and start your service.
然后需要定义接收方,接收方将获得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);
}
}
}
And now your service should be running when the phone starts up.
现在你的服务应该在电话启动时运行。
#2
87
This is how to make an activity start running after android device reboot:
以下是如何在android设备重新启动后让活动启动:
Insert this code in your AndroidManifest.xml
file, within the <application>
element (not within the <activity>
element):
将此代码插入到您的AndroidManifest中。xml文件,在
<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>
Then create a new class yourActivityRunOnStartup
(matching the android:name
specified for the <receiver>
element in the manifest):
然后创建一个新的类yourActivityRunOnStartup(匹配清单中为
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.
注意:调用i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);之所以重要,是因为活动是从活动之外的上下文启动的。没有这个,活动就不会开始。
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.
此外,
#3
48
Listen for the ACTION_BOOT_COMPLETE and do what you need to from there. There is a code snippet here.
监听ACTION_BOOT_COMPLETE,然后做你需要做的事情。这里有一个代码片段。
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):
在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
#4
9
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应用程序:自动启动——没有根。
#5
2
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 4.2.2)。我必须向同一个Android项目添加一个虚拟活动,并至少在设备上手动运行一次活动。然后Sean的解决方案开始工作,并在重新启动后通知了BroadcastReceiver。
#6
0
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.
我想在这个问题上增加一点,我已经面临了好几天了。我试了所有的答案,但那些对我不起作用。如果您正在使用android版本5.1,请更改这些设置。
If you are using android version 5.1 then you have to dis-select (Restrict to launch) from app settings.
如果您正在使用android版本5.1,那么您必须从应用程序设置中取消选择(限制启动)。
settings> app > your app > Restrict to launch (dis-select)
>应用>应用>限制启动(dis-select)
#7
-1
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.
另一种方法是使用android.intent.action.USER_PRESENT而不是android.intent.action.BOOT_COMPLETED,以避免在引导过程中出现慢下来。但只有当用户启用了锁屏时,才会出现这种情况——否则这个意图永远不会被广播。
Reference blog - The Problem With Android’s ACTION_USER_PRESENT Intent
参考博客——Android的ACTION_USER_PRESENT意图的问题