我尝试使用本教程中的示例代码,但它似乎过时了,而且不起作用。所以我必须做出什么改变,什么文件有我的应用程序自动启动时,Android完成启动?
当前回答
此外,如果你不想修改代码,你可以使用像AutoStart这样的应用程序来启动一个android应用程序:AutoStart -没有根
其他回答
我想在这个问题上补充一点,这是我几天来一直面临的问题。我试了所有的答案,但都不管用。如果你使用的是安卓5.1版本,请更改这些设置。
如果你使用的是android 5.1版本,那么你必须从应用程序设置中取消选择(限制启动)。
>应用>你的应用>限制启动(取消选择)
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绘制覆盖权限
所以在你的第一个活动中加入这个
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
}
}
}
首先,你需要AndroidManifest.xml中的权限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
同样,在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>
然后需要定义接收方,它将获得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);
}
}
}
现在,当电话启动时,您的服务应该正在运行。
监听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
对于flutter用户,可以创建一个名为MainActivityReceiver的文件。Kt在包文件夹。如。android / app / src / main /芬兰湾的科特林/ 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文件。
推荐文章
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件
- 修改抽射超时时间
- 如何通过数据从第二个活动到第一个活动时按回?——安卓
- 如何在android中获得当前前景活动上下文?
- 如何在Android中获取当前日期?
- 获取Android设备名称
- 在WebView中上传文件
- 加载HTML文件到WebView
- Android:为什么视图没有maxHeight ?