应用程序自动启动延迟

时间:2018-02-05 13:48:39

标签: java android broadcastreceiver

我想在设备开启时延迟自动启动应用程序。我尝试通过使用Handler,Timer和AlarmManager来实现这一点,但没有任何事情发生。在ACTION_BOOT_COMPLETED处的应用程序默认启动工作正常,但是当我尝试使用延迟时,我的应用程序根本不会自动启动。我认为这取决于BroadcastReciever的生命周期,但我无法找到详细的响应。如果您有任何建议,请留下。我的代码是:

BroacastReciever.java:

package com.example.micka.iportapp;

public class ActivityRunOnStartup extends BroadcastReceiver {

public SharedPreferences sPref;
private final int DEFAULT_APP_BOOT_DELAY = 100000;
private final boolean AUTO_BOOT_APP_FLAG = false;
public static SharedPreferences.Editor editor;
private int ACTUAL_BOOT_DELAY;




@Override
public void onReceive(final Context context, Intent intent) {

    sPref = context.getSharedPreferences("appPref",Context.MODE_PRIVATE);
    ACTUAL_BOOT_DELAY = sPref.getInt("sPref_delay",DEFAULT_APP_BOOT_DELAY);

    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        final Intent i = new Intent(context, BroadcastActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(),PendingIntent.FLAG_ONE_SHOT,i,0);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.ELAPSED_REALTIME,System.currentTimeMillis()+10000,pendingIntent);

               // context.startActivity(i);





                // Actions to do after 10 seconds

            /*}
        }, 5000);*/

    }
}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.micka.iportapp">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<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:enabled="true"
        android:exported="true"
        android:name=".ActivityRunOnStartup"
        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>
    <activity android:name=".BroadcastActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

我也试过这个版本:

public class ActivityRunOnStartup extends BroadcastReceiver {

public SharedPreferences sPref;
private final int DEFAULT_APP_BOOT_DELAY = 100000;
private final boolean AUTO_BOOT_APP_FLAG = false;
public static SharedPreferences.Editor editor;
private int ACTUAL_BOOT_DELAY;




@Override
public void onReceive(final Context context, Intent intent) {

    sPref = context.getSharedPreferences("appPref",Context.MODE_PRIVATE);
    ACTUAL_BOOT_DELAY = sPref.getInt("sPref_delay",DEFAULT_APP_BOOT_DELAY);

    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        final Intent i = new Intent(context, BroadcastActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                context.startActivity(i);
            }
        },10000);


    }
}

1 个答案:

答案 0 :(得分:0)

您以错误的方式创建PendingIntent,以启动您需要致电PendingIntent.getActivity而不是PendingIntent.getBroadcast的活动:

PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(),PendingIntent.FLAG_ONE_SHOT,i,0);

应该替换为:

PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(),PendingIntent.FLAG_ONE_SHOT,i,0);

如果您想要启动另一个BroadcastReceiver,则第一个变体可用。

另一个问题是设置闹钟时间,因为我记得没有必要添加当前时间:

alarmManager.set(AlarmManager.ELAPSED_REALTIME, 10000, pendingIntent);