Android 启动完成后自动启动应用程序

发布于 2024-12-28 02:31:40 字数 568 浏览 1 评论 0原文

我想制作一个在其设置中具有自动启动选项的应用程序。我在我的应用程序中进行了从 PreferenceActivity 派生的设置活动,并为自动启动选项提供了 CheckBoxPreference。如果启用自动启动选项,我的应用程序应该在手机启动完成时启动。如果自动启动选项被禁用,那么它不应在启动完成时启动。

为了实现这一目标,我实现了 BroadcastReceiver 的派生类,它接收 BOOT_COMPLETED 意图,在 AndroidManifest.xml 中声明接收器,并在 AndroidManifest.xml 中授予权限。

在应用程序中还有一个 Application 的派生类并从应用程序派生类的 onCreate 方法启动服务。 如果我在 AndroidManifest.xml 中声明接收器,则在启动完成后调用我的应用程序的 onCreate,然后调用 BroadcastReceiver 的 onReceive 方法。

现在的问题是,无论自动启动是启用还是禁用,我的应用程序都会在每次启动完成时启动。禁用自动启动时是否可以不启动应用程序?

I want to make an application which has auto start option in its settings. I have made Settings activity in my application which is derived from PreferenceActivity and give CheckBoxPreference for auto start option. If auto start option is enabled my application should start when booting of phone is completed. And if auto start option is disabled then it should not start on boot completed.

To achieve this I have implemented derived class of BroadcastReceiver which receives BOOT_COMPLETED intent, declare receiver in AndroidManifest.xml and also give permission in AndroidManifest.xml.

In application also there is a derived class of Application and start service also from the onCreate method of application derived class. If I declare receiver in AndroidManifest.xml then after booting completed onCreate of my application called and after that onReceive method of BroadcastReceiver called.

Now the problem is that my application starts on boot completed every time whether auto start is enabled or disabled. Is it possible to not start application when auto start is disabled ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

家住魔仙堡 2025-01-04 02:31:40

您可以使用共享首选项为 isAutoStartEnabled 存储一个布尔值,并在 BroadcastReciver 中检查该值,仅在为 true 时才触发 Intent。

就您而言,问题不在于您是否收到广播,而在于谁收到广播。祝你好运..

我希望它有帮助..

You can use the shared preference to store a Boolean value for isAutoStartEnabled, and check this value in the BroadcastReciver, fire an intent only if it's true.

In your case, the problem is not whether you receive the broadcast but who receives the broadcast. Best of luck..

I hope it helps..

陌伤浅笑 2025-01-04 02:31:40

我认为从Android 3.1起开始,接收BOOT_COMPLETED意图的BroadcastReceiver将无法工作。用户必须通过与其交互来唤醒应用程序。

因此,启动设备后,所有第三方应用程序都会停止。

对于当前您的应用程序,您可以使用 SharedPreferences 自动启动您的应用程序。

更新:仅适用于低于 3.1 的 Android 版本,对于更高版本,它可以工作,但您在设备上启动完成后,用户必须与应用程序进行交互

您需要使用具有 android.intent.action.BOOT_COMPLETED 意图的 BroadcastReceiver

将以下内容添加到您的清单文件中:

<receiver android:name="App_Receiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

实现 BoradcastReciever 的 App_Receiver 类。实现 onReceive() 方法并从您的应用启动您最喜欢的活动。

public void onReceive(Context context, Intent intent) {
    // make sure you receive "BOOT_COMPLETED"
// Here isAutoStartEnabled check sharedPreferences for Auto Start flag
if ( isAutoStartEnabled ) {

    if ((intent.getAction() != null) && (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")))
    {
        // Start the service or activity 
    }
}

I think from Android 3.1 onwards your BroadcastReceiver which receives BOOT_COMPLETED intent its not going to work. User have to in-wake the application by interacted with it.

So, After booting the device all third party application are lying as a stop.

And for currently your application you can use SharedPreferences for Auto-Start your application..

UPDATE: (Only for Android version below 3.1 for higher version it works but you have to user interaction with your application after boot completed on device)

You need to use a BroadcastReceiver with android.intent.action.BOOT_COMPLETED intent.

Add following to your manifest file:

<receiver android:name="App_Receiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

App_Receiver class implementing BoradcastReciever. Implement the onReceive() method and start your favorite activity from your app.

public void onReceive(Context context, Intent intent) {
    // make sure you receive "BOOT_COMPLETED"
// Here isAutoStartEnabled check sharedPreferences for Auto Start flag
if ( isAutoStartEnabled ) {

    if ((intent.getAction() != null) && (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")))
    {
        // Start the service or activity 
    }
}
笔芯 2025-01-04 02:31:40

您必须在清单中添加使用权限android.permission.RECEIVE_BOOT_COMPLETED

You have to add the uses-permission android.permission.RECEIVE_BOOT_COMPLETED in your Manifest.

深居我梦 2025-01-04 02:31:40
final SharedPreferences sharedPreferences = getSharedPreferences("Application", MODE_PRIVATE);
        boolean isAutoStartEnabled = sharedPreferences.getBoolean("isAutoStartEnabled", false);

        if ( isAutoStartEnabled ) {
            startActivity(new Intent());
        } 

我希望这对你有帮助

final SharedPreferences sharedPreferences = getSharedPreferences("Application", MODE_PRIVATE);
        boolean isAutoStartEnabled = sharedPreferences.getBoolean("isAutoStartEnabled", false);

        if ( isAutoStartEnabled ) {
            startActivity(new Intent());
        } 

I hope this helps you

薄情伤 2025-01-04 02:31:40

以下代码对我有用:

public class BootCompleteReceiver extends BroadcastReceiver {
    public static final String PREFS_NAME = "MyPrefsFile";  

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Log.d("boot completed", "boot completed caught");
            Boolean autoRestart = false;
            SharedPreferences sp = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
            autoRestart = sp.getBoolean("autoRestart", false);

            if (autoRestart){

                Log.d("boot completed", "auto restart true");

                Intent i = new Intent(context, WelcomeScreen.class);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);

            }
        }
    }

}

The following code works for me:

public class BootCompleteReceiver extends BroadcastReceiver {
    public static final String PREFS_NAME = "MyPrefsFile";  

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Log.d("boot completed", "boot completed caught");
            Boolean autoRestart = false;
            SharedPreferences sp = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
            autoRestart = sp.getBoolean("autoRestart", false);

            if (autoRestart){

                Log.d("boot completed", "auto restart true");

                Intent i = new Intent(context, WelcomeScreen.class);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);

            }
        }
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文