简体   繁体   English

在Android中启动后自动启动应用程序

[英]Auto start application after boot completed in Android

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. 我在我的应用程序中创建了一个从PreferenceActivity派生的Settings活动,并为自动启动选项提供CheckBoxPreference。 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. 为了实现这一点,我实现了BroadcastReceiver的派生类,它接收BOOT_COMPLETED意图,在AndroidManifest.xml中声明接收器,并在AndroidManifest.xml中授予权限。

In application also there is a derived class of Application and start service also from the onCreate method of application derived class. 在应用程序中,还有一个派生类的Application和启动服务也来自应用程序派生类的onCreate方法。 If I declare receiver in AndroidManifest.xml then after booting completed onCreate of my application called and after that onReceive method of BroadcastReceiver called. 如果我在AndroidManifest.xml中声明接收器,那么在启动我的应用程序的onCreate之后调用,然后调用BroadcastReceiver的onReceive方法。

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 ? 禁用自动启动时是否可以不启动应用程序?

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. 您可以使用共享首选项为isAutoStartEnabled存储布尔值,并在BroadcastReciver中检查此值,仅在它为true时触发intent。

In your case, the problem is not whether you receive the broadcast but who receives the broadcast. 在您的情况下,问题不在于您是否收到广播,而是接收广播的人。 Best of luck.. 祝你好运..

I hope it helps.. 我希望它有帮助..

您必须在Manifest中添加uses-permission android.permission.RECEIVE_BOOT_COMPLETED

I think from Android 3.1 onwards your BroadcastReceiver which receives BOOT_COMPLETED intent its not going to work. 我认为从Android 3.1开始你的BroadcastReceiver接收BOOT_COMPLETED意图它不会工作。 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.. 对于您目前的应用程序,您可以使用SharedPreferences自动启动您的应用程序。

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 ) 更新:( 仅适用于3.1以下的Android版本,对于更高版本,它可以工作,但您必须在设备启动完成后与您的应用程序进行用户交互

You need to use a BroadcastReceiver with android.intent.action.BOOT_COMPLETED intent. 您需要使用具有android.intent.action.BOOT_COMPLETED意图的BroadcastReceiver

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 . App_Receiver类实现BoradcastReciever Implement the onReceive() method and start your favorite activity from your app. 实现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 
    }
}

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);

            }
        }
    }

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

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

I hope this helps you 我希望这可以帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM