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

时间:2018-08-21 11:46:54

标签: android android-service autostart

我希望在重新启动设备后启动应用程序。 我有用于自动启动的代码,但不适用于所有设备,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();
                }
            }

        }
    }

它可以在小米,荣耀等指定设备上运行。.但是在熔岩上则不能运行

2 个答案:

答案 0 :(得分:0)

自奥利奥以来,服务无法从重新启动启动。 为了启动服务,您需要从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)
         }
    }
}

您的清单应类似于:

<?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>

答案 1 :(得分:0)

此代码杀死并在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");
        }
    }