0

我正在开发一个 android 应用程序,如果时间保存在数据库中,它每 12 小时显示一次通知。所以每次在数据库中输入或编辑数据时,我都会取消当前的alarmmanager并重新启动一个新的,这样我就不会错过一个。同样在重新启动时,我已经调用了警报管理器。在广播接收器上,检查数据库是否有条目,如果找到,则会设置通知并自动打开应用程序。

因此,当我通过手动更改日期来测试应用程序时,应用程序按预期工作。重新启动应用程序也可以工作。但是如果我让应用程序闲置近 14 小时,则通知未设置,但如果我打开应用程序并暂停它之后设置通知。

这就是我所说的警报管理器。意图alarmintent = new Intent(context, package.Alarm_Manager.class);

    alarmintent.putExtra("note","Notify");
    sender = PendingIntent.getBroadcast(context , 0 , alarmintent , PendingIntent.FLAG_CANCEL_CURRENT | Intent.FILL_IN_DATA);            
    alarm_manger = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    alarm_manger.cancel(sender);
    Calendar cal = Calendar.getInstance();
    long now = cal.getTimeInMillis();
    alarmintent = new Intent(context, package.Alarm_Manager.class);
    alarmintent.putExtra("note","Notification");
    sender = PendingIntent.getBroadcast(context , 0 , alarmintent , PendingIntent.FLAG_CANCEL_CURRENT | Intent.FILL_IN_DATA);            
    alarm_manger = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarm_manger.setRepeating(AlarmManager.RTC_WAKEUP, now, AlarmManager.INTERVAL_HALF_DAY, sender);

这是广播接收器

@Override
public void onReceive(Context context, Intent intent)
{
       NotificationManager manger = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
       Calendar cal = Calendar.getInstance();
       date = (int)(cal.getTimeInMillis()/1000);
       Notification notification = new Notification(R.drawable.vlcsnap_396460 , "Notify" , System.currentTimeMillis());
       PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
       notification.setLatestEventInfo(context, "App", "Notify" , contentIntent);
       notification.flags = Notification.FLAG_INSISTENT;
       manger.notify( 0 , notification);
   }
4

2 回答 2

0

在看过您的 Alarm_Manager 代码后,我认为直接在您的 BroadcastReceiver 对象中执行此操作是非法的。报价

如果此 BroadcastReceiver 是通过标签启动的,则对象在从此函数返回后不再存在。

我相信除了创建一个由您的 BroadcastReceiver 通知的服务之外别无他法,并确保该服务setLatestEventInfo()以自身 ( this) 作为上下文进行调用。

当您的应用程序运行时异步广播失败的原因可能是提供给广播接收器的上下文仅在您的应用程序未运行时对广播接收器的调用期间存在。因此,仅在您的 BroadcastReceiver 与临时上下文一起死亡后运行的通知服务缺少有效的上下文。

当你的应用程序运行时,Broadcast 可能会与你的 Activity 或 Application 对象一起作为 Context,而这在 Notification manager 运行时仍然有效。

希望这可以帮助。

更新:一个`IntentService`会做。您不希望为此提供全职服务。

更新 2:一些片段。

<service android:name=".MyIntentService" android:label="@string/my_intent_service_name" />

public final class MyIntentService extends IntentService {
    public MyIntentService() {
    super("service name");
    // set any properties you need
    }
    @Override
    public void onCreate() {
        super.onCreate();
        // do init, e.g. get reference to notification service
    }
    @Override
    protected void onHandleIntent(Intent intent) {
    // handle the intent
        // ...especially:
        notification.setLatestEventInfo(this, "App", "Notify" , contentIntent);
        // ...
    }
}

public final class MyAlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        context.startService(new Intent(context, MyIntentService.class));
    }
}
于 2013-01-24T10:09:01.003 回答
0

alarm_manager.cancel(sender);如果你设置了你不需要打电话PendingIntent.FLAG_CANCEL_CURRENT.

您的来电

alarm_manger.setRepeating(AlarmManager.RTC_WAKEUP, now, AlarmManager.INTERVAL_HALF_DAY, sender);

将立即触发警报,因为当您设置警报时,现在已经过去了。

我建议你使用

now + DateUtils.HOUR_IN_MILLIS / 2 

对于 triggerAtMillis 参数

您是否尝试将其安排为更小的间隔?它会被触发吗?

于 2013-01-24T10:03:00.150 回答