我之前完成活动后,我的闹钟不会触发。 当我留在活动中时,警报工作正常。 这是代码:
AlarmManager alarms = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
final BroadcastReceiver receiver_daily = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "DAILY BONUS");
String title = getResources().getString(R.string.playreminder_daily_title);
String text = String.format(getResources().getString(R.string.playreminder_daily_text), getResources().getInteger(R.integer.daily_bonus_coins));
showDailyBonusNotification(title, text);
unregisterReceiver(this);
Account.setBonusAvailable(true, getApplicationContext());
}
};
registerReceiver(receiver_daily, new IntentFilter("com.doopy.numbers.ACTION_PLAYREMINDER_DAILY"));
PendingIntent operation = PendingIntent.getBroadcast(getApplicationContext(), RQC_BROADCAST, new Intent("com.doopy.numbers.ACTION_PLAYREMINDER_DAILY"), 0);
alarms.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime()+DAILY_BONUS_TIME, operation);
addCoinsAnimated(Account.getCoins(getApplicationContext()), getResources().getInteger(R.integer.daily_bonus_coins), 500, true, getApplicationContext());
Account.setBonusAvailable(false, getApplicationContext());
mGetBonusLayout.setVisibility(View.GONE);
我也注意到我收到了这个泄漏警告:
android.app.IntentReceiverLeaked:Activity com.doopy.numbers.GameOverActivity泄露了最初在这里注册的IntentReceiver com.doopy.numbers.GameOverActivity$5@41c33780。你是否错过了对unregisterReceiver()的调用?
我不希望在活动结束后取消注册我的警报/被销毁,因为它应该触发现在可用的每日奖励的通知,尽管应用程序可能没有运行。
答案 0 :(得分:0)
关于泄漏警告,您可以在清单中定义接收器,如此
<receiver android:enabled=["true" | "false"]
android:exported=["true" | "false"]
android:icon="drawable resource"
android:label="string resource"
android:name="string"
android:permission="string"
android:process="string" >
. . .
</receiver>
这会给你预期的行为。 Reference here
一个例子:
<receiver android:name="MyReceiver" >
<intent-filter>
<action android:name="com.doopy.numbers.ACTION_PLAYREMINDER_DAILY" />
</intent-filter>
</receiver>