gpt4 book ai didi

java - Android 启动时启动应用程序

转载 作者:行者123 更新时间:2023-12-01 21:43:32 34 4
gpt4 key购买 nike

AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
{...}
<receiver
android:enabled="true"
android:exported="true"
android:name="com.example.richard.Test.StartMyServiceAtBootReciever"
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>
{...}

StartMyServiceAtBootReciever.java

    package com.example.richard.Test;

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

/**
* Created by Richard on 23/03/2016.
*/
public class StartMyServiceAtBootReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, MainActivity.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(serviceIntent);
}
}
}

详细信息:当我重新启动手机时,应用程序不会同时启动。有人可以帮助我吗?

最佳答案

如果您要启动一个 Activity,则使用

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

关于java - Android 启动时启动应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36188069/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com