如何在启动时启动/启动应用程序 Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10428510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to start/ launch application at boot time Android
提问by Marco Gallella
I would like to launch my app when my tablet starts, so that the main activity of my app is the first thing that the user see when they start the tablet.
I've read about LauncherActivity but I don't understand how to use it.
Can anyone help me with suggestions, links or tutorials for this?
Is LauncherActivity the best way or are there alternatives?
我想在平板电脑启动时启动我的应用程序,以便我的应用程序的主要活动是用户在启动平板电脑时看到的第一件事。
我已阅读有关 LauncherActivity 的内容,但我不明白如何使用它。
任何人都可以帮我提供建议、链接或教程吗?
LauncherActivity 是最好的方法还是有其他选择?
回答by vishesh chandra
These lines of code may be helpful for you...
这些代码行可能对您有所帮助...
Step 1: Set the permission in AndroidManifest.xml
第一步:在AndroidManifest.xml中设置权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Step 2: Add this intent filter in receiver
第 2 步:在接收器中添加此意图过滤器
<receiver android:name=".BootReceiver">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Step 3: Now you can start your application's first activity from onReceive
method of Receiver class
第 3 步:现在您可以从onReceive
Receiver 类的方法开始您的应用程序的第一个活动
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
回答by Gunnar Karlsson
If you want to start the app when the tablets starts, you need to listen to the BOOT_COMPLETED action and react to it. BOOT_COMPLETED is a Broadcast Action that is broadcast once, after the system has finished booting. You can listen to this action by creating a BroadcastReceiverthat then starts your launch Activity when it receives an intent with the BOOT_COMPLETED action.
如果您想在平板电脑启动时启动应用程序,您需要监听 BOOT_COMPLETED 操作并对其做出反应。BOOT_COMPLETED 是一个广播动作,在系统完成启动后广播一次。你可以通过创建一个BroadcastReceiver来监听这个动作,当它收到一个带有 BOOT_COMPLETED 动作的意图时,它会启动你的启动活动。
Add this permission to your manifest:
将此权限添加到您的清单中:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Create a Custom BroadcastReceiver in your project:
在您的项目中创建自定义广播接收器:
public class MyBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent i = new Intent(context, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
Then modify your manifest file by adding the BroadCastReceiver to the Manifest:
然后通过将 BroadCastReceiver 添加到清单来修改您的清单文件:
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
回答by Had?i Lazar Pe?i?
Answer by @vishesh chandra is correct. But on some device doesn't work because app was installed on external storage by default. Please ensure that you specify
@vishesh chandra 的回答是正确的。但是在某些设备上不起作用,因为默认情况下应用程序安装在外部存储上。请确保您指定
android:installLocation="internalOnly"
android:installLocation="internalOnly"
otherwise you will not receive any Boot Complete actions if the app is installed in the SD card. Add this into application tag in manifest.xml file and it will work.
否则,如果应用程序安装在 SD 卡中,您将不会收到任何启动完成操作。将此添加到 manifest.xml 文件中的应用程序标记中,它将起作用。
Usage:
用法:
<application
android:name=".Data.ApplicationData"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:installLocation="internalOnly"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
<!--activities, services...-->
</application>
回答by Manish Yadav
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)
设置>应用程序>您的应用程序>限制启动(取消选择)
please see picture.
请看图片。