简体   繁体   English

重新启动后自动启动应用程序?

[英]Autostart Application after reboot?

I want my application starts when I rebooted my device. 我希望在重新启动设备后启动应用程序。 I have this code for autostart.but it doesn't work on all device, ps support me. 我有用于自动启动的这段代码。但是它不适用于所有设备,ps支持我。

  if (Build.BRAND.equalsIgnoreCase("xiaomi")) {

        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
        startActivity(intent);


    } else if (Build.BRAND.equalsIgnoreCase("Letv")) {

        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity"));
        startActivity(intent);

    } else if (Build.BRAND.equalsIgnoreCase("Honor")) {

        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"));
        startActivity(intent);

    } else if (Build.BRAND.equalsIgnoreCase("OPPO")) {
        try {

            Intent i = new Intent(Intent.ACTION_MAIN);
            i.setComponent(new ComponentName("com.oppo.safe", "com.oppo.safe.permission.floatwindow.FloatWindowListActivity"));
            startActivity(i);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                Log.e("Device", Build.BRAND);
                Intent intent = new Intent("action.coloros.safecenter.FloatWindowListActivity");
                intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.floatwindow.FloatWindowListActivity"));
                startActivity(intent);
            } catch (Exception ee) {

                ee.printStackTrace();
                try {
                    Log.e("Device", Build.BRAND);
                    Intent i = new Intent("com.coloros.safecenter");
                    i.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.sysfloatwindow.FloatWindowListActivity"));
                    startActivity(i);
                } catch (Exception e1) {

                    e1.printStackTrace();
                }
            }

        }
    }

it works on a specified device like xiaomi,honor.. but it doesn't work on lava 它可以在小米,荣耀等指定设备上运行。.但是在熔岩上不起作用

Services can't start from reboot since Oreo. 从Oreo开始,服务无法从重新启动启动。 In order to start a service you need to start your service from an AlarmManager 为了启动服务,您需要从AlarmManager启动服务。

class BootDeviceReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
         val action = intent.action
         val message = "BootDeviceReceiver onReceive, action is $action"
         Log.e(TAG, message)
         if (Intent.ACTION_BOOT_COMPLETED == action) {
             startServiceDirectly(context)
         }
    }

    private fun startServiceDirectly(context: Context) {
         val startServiceIntent = Intent(context, YOUR_SERVICE_CLASS::class.java)
         startServiceIntent.action = START_SERVICE_REBOOT
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
             context.startForegroundService(startServiceIntent)
         } else {
             context.startService(startServiceIntent)
         }
    }
}

You manifest should be like: 您的清单应类似于:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="YOUR_PACKAGE">

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <receiver
            android:name=".receiver.BootDeviceReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service
            android:name=".service.YOUR_SERVICE_CLASS"
            android:enabled="true"
            android:exported="true" />

        <activity
            android:name=".ui.MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

This code kill and then start you application from Launch activity after 100ms: 此代码将杀死并在100毫秒后从启动活动中启动您的应用程序:

public static void doRestart(Context c) {
        try {
            //check if the context is given
            if (c != null) {
                //fetch the packagemanager so we can get the default launch activity
                // (you can replace this intent with any other activity if you want
                PackageManager pm = c.getPackageManager();
                //check if we got the PackageManager
                if (pm != null) {
                    //create the intent with the default start activity for your application
                    Intent mStartActivity = pm.getLaunchIntentForPackage(
                            c.getPackageName()
                    );
                    if (mStartActivity != null) {
                        mStartActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        //create a pending intent so the application is restarted after System.exit(0) was called.
                        // We use an AlarmManager to call this intent in 100ms
                        int mPendingIntentId = 223344;
                        PendingIntent mPendingIntent = PendingIntent
                                .getActivity(c, mPendingIntentId, mStartActivity,
                                        PendingIntent.FLAG_CANCEL_CURRENT);
                        AlarmManager mgr = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
                        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
                        System.exit(0);
                    } else {
                        Log.d("RSD", "Was not able to restart application, mStartActivity null");
                    }
                } else {
                    Log.d("RSD", "Was not able to restart application, PM null");
                }
            } else {
                Log.d("RSD", "Was not able to restart application, Context null");
            }
        } catch (Exception ex) {
            Log.d("RSD", "Was not able to restart application");
        }
    }

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

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