【问题标题】:Android Alarm Manager with Broadcast receiver带有广播接收器的 Android 警报管理器
【发布时间】:2016-09-05 17:02:54
【问题描述】:

您好,我有我的警报管理器来显示通知。问题是一旦触发警报并显示通知,执行 MainActivity(super.onCreate) 的代码时,总是会触发通知。

这是执行警报的 MainActivity。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initAlarm();

}

private void initAlarm(){

    Calendar calendar = Calendar.getInstance();

    calendar.set(Calendar.MONTH, 8);
    calendar.set(Calendar.YEAR, 2015);
    calendar.set(Calendar.DAY_OF_MONTH, 21);

    calendar.set(Calendar.HOUR_OF_DAY, 15);
    calendar.set(Calendar.MINUTE, 45);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.AM_PM,Calendar.PM);

    //Creo un intent que ejecutara el BroadcastReceiver
    Intent myIntent = new Intent(MainActivity.this, AlarmBroadcastReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}

这里是AlarmBroadcastReceiver,它应该只在AlarmManager的时间到期时才被调用。

public class AlarmBroadcastReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startIntent = new Intent(context, AlarmService.class);
        context.startService(startIntent);
    }
}

BroadcastReceiver 启动的服务:

public class AlarmService extends Service{
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        generarNotificacion();

        return Service.START_NOT_STICKY;
    }

@Override
public IBinder onBind(Intent intent) {
    return null;
}

public void generarNotificacion(){

    Intent resultIntent = new Intent(this.getApplicationContext(), MainActivity.class);

    android.support.v4.app.NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.logo)
                    .setContentTitle(getResources().getString(R.string.app_name))
                    .setContentText(getResources().getString(R.string.texto_notificacion));

    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    this,
                    0,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );

    mBuilder.setContentIntent(resultPendingIntent);

    // Sets an ID for the notification
    int mNotificationId = 001;

    // Gets an instance of the NotificationManager service
    NotificationManager mNotifyMgr =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // Builds the notification and issues it.
    mNotifyMgr.notify(mNotificationId, mBuilder.build());
}

}

最后我将这段代码添加到 manifest.xml 中

<application
        android:allowBackup="true"
        android:icon="@mipmap/logo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

    <service android:name=".AlarmService"
        android:enabled="true" />

    <receiver android:name=".AlarmBroadcastReceiver"/>

...

</application>

【问题讨论】:

    标签: android service notifications broadcastreceiver alarmmanager


    【解决方案1】:

    很明显。您正在 onCreate 中调用 initAalarm()。要了解使用您的代码执行以下测试用例:

    假设当前日期和时间是 2015 年 9 月 20 日下午 1:00 和 警报设置为未来日期,例如当前时间后 15 分钟,即 2015 年 9 月 20 日下午 1:15
    现在要对其进行测试,您可以等待时间到达或更改系统日期时间。完成此操作后,您将看到同时触发通知。现在不要更改 initAlarm() 中的任何内容,关闭活动并再次启动它,您将再次看到通知。这背后的原因是,如果警报设置为相对于系统时间的某个过去日期,那么它会立即被触发。

    查看Alarm Manager's set method的文档

    【讨论】:

      【解决方案2】:

      您在 9 月(即第 9 个月)发布此内容
      在哪里
      你在过去的日期尝试这个是

      calendar.set(Calendar.MONTH, 8);
      

      将其更改为未来日期。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-31
        相关资源
        最近更新 更多