AlarmManager不会重复意图

时间:2015-08-06 12:25:50

标签: android android-intentservice

这是我的BootBroadcast

public class BootBroadcast extends BroadcastReceiver {

    public static final String FILE_NAME = "BootBroadcast.java";
    private static final long REPEAT_TIME_TO_CHECK_EXPENSES = 1000 * 60; //1 minute

@Override
    public void onReceive(Context context, Intent intent) {
        Log.v(FILE_NAME, "in onReceive");
        AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        Intent service1 = new Intent(context, ISCheckExpenses.class);
        Log.v(FILE_NAME, "starting service1");
        context.startService(service1);

        PendingIntent pending1 = PendingIntent.getBroadcast(context, 0, service1, PendingIntent.FLAG_UPDATE_CURRENT);
        Calendar cal1 = Calendar.getInstance();
        cal1.add(Calendar.SECOND, 600);
        service.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(), REPEAT_TIME_TO_CHECK_EXPENSES, pending1);
    }
}

这是我的ISCheckExpenses

public class ISCheckExpenses extends IntentService {
public static final String FILE_NAME = "ISCER";
private int recurringExpensesNotificationID = 001;
private int recurringExpensesMultiplier = 123;

public ISCheckExpenses(){
        super(ISCheckExpenses.class.getName());
    }

@Override
    public void onCreate() {
        super.onCreate(); 
    }

@Override
    protected void onHandleIntent(Intent intent){

        Log.d(FILE_NAME, "IntentService Started!");

     //do some processing and send a notification to the user
    String subject, content;
    subject = getString(R.string.recurring_expenses_notification_due_subject);
                content = getApplicationContext().getString(R.string.recurring_expenses_notification_due_content);
     sendNotification(recurringExpensesNotificationID,1,subject,content);
    }

    private void sendNotification(int notificationID, int notificationType, String subject, String content){
        Intent myIntent = new Intent(this, MainActivity.class);
        int randomID = 1;

            myIntent.putExtra("intentCaller","expensesRecurring");
            randomID = recurringExpensesMultiplier * requestID;

        //just to make sure the random ID is non-negative
        if (randomID < 0){
            randomID = randomID * -1;
        }
        myIntent.setAction(Intent.ACTION_VIEW);
        myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pIntent = PendingIntent.getActivity(getBaseContext(), randomID, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder n  = new NotificationCompat.Builder(this)
                .setContentTitle(subject)
                .setContentText(content)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pIntent)
                .setAutoCancel(true);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        n.build().flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(notificationID, n.build());
    }
}

这两个文件已经在我的AndroidManifest.xml

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<service
            android:name=".ISCheckExpenses"
            android:exported="false"
            android:icon="@mipmap/ic_launcher"
            android:enabled="true" />
<receiver android:name=".BootBroadcast" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
</application>

重启设备后几秒钟,将显示通知。然后我点击通知清除它。我的应用程序假设在1分钟后重新显示通知吗?但它并没有。我希望看到&#34; IntentService开始!&#34;我的logcat每1分钟..但没有出现? &#34; IntentService已开始!&#34;只显示一次。

我在这里缺少什么?感谢

顺便说一下,我在KitKat和Lollipop上测试过它。

3 个答案:

答案 0 :(得分:1)

问题是您正在尝试使用Service为广播启动PendingIntent。您需要让闹钟向另一个BroadcastReceiver发送广播,因为您希望唤醒设备,然后让该接收器启动您的Service。您还可能需要协调唤醒锁定以确保设备保持足够长的清醒状态。

答案 1 :(得分:1)

您获得第一个&#34; IntentService已启动!&#34;因为您从BootReceiver显式调用了startService(),但是你的警报管理器设置为在10分钟后触发,而不是在第一次出现后1分钟,因为你这样做:

Calendar cal1 = Calendar.getInstance();
cal1.add(Calendar.SECOND, 600);
service.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(), REPEAT_TIME_TO_CHECK_EXPENSES, pending1);

即。您将600秒(10分钟)添加到当前时间,并将其用于AlarmManager首次启动的时间。我只是更改了BroadcastReceiver,而不是仅仅更改您在600到60秒之间添加的时间,而不是调用startService()并设置第一次调用,我不是只需将当前时间(以毫秒为单位)传递到AlarmManager.setInexactRepeating()的{​​{1}}时间,以便立即触发,然后以60秒的间隔触发 - 即将triggerAtMillis方法更改为:

onReceive()

您还应该记住,当您使用setInexactRepeating()时,警报可能会延迟最多60秒,如文档中所述。

答案 2 :(得分:0)

我的错误是我误解了BroadcastReceiver和AlarmManager应该工作。我没有像上面那样在onReceive中执行AlarmManager,而是创建了另一个调用新Intent的函数,并将调用活动作为Context传递,并将BroadcastReceiver作为类传递。

以下是我的工作BroadcastReceiver的样子:

public class BootBroadcast extends BroadcastReceiver {

    //private static final long REPEAT_TIME_TO_CHECK_EXPENSES = 1000 * 30;
    //private static final long REPEAT_TIME_TO_ANALYZE_LOCATION = 1000 * 30;

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

        int what = intent.getIntExtra("what",0);

        if (what == 1) {
            Intent intentService1 = new Intent(context, ISCheckRecurringItemsAndCC.class);
            context.startService(intentService1);
        }else if (what == 2) {
            Intent service2 = new Intent(context, ISCheckLocationAndRating.class);
            context.startService(service2);
        }else{
            //should only reach here when the device boots
            Intent intentService1 = new Intent(context, ISCheckRecurringItemsAndCC.class);
            context.startService(intentService1);

            Intent service2 = new Intent(context, ISCheckLocationAndRating.class);
            context.startService(service2);
        }
    }

    public void setAlarm(Context context){
        Log.v(FILE_NAME,"inside setAlarm");
        AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

        Intent myIntent = new Intent(context, BootBroadcast.class);
        myIntent.putExtra("what", 1);
        PendingIntent myPendingIntent = PendingIntent.getBroadcast(context, 111, myIntent, 0);
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), REPEAT_TIME_TO_CHECK_EXPENSES, myPendingIntent);

        myIntent = new Intent(context, BootBroadcast.class);
        myIntent.putExtra("what", 2);
        myPendingIntent = PendingIntent.getBroadcast(context, 222, myIntent, 0);
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), REPEAT_TIME_TO_ANALYZE_LOCATION, myPendingIntent);

    }
}

上面的代码现在对我来说非常合适。