Android的报警管理与广播接收器 [英] Android Alarm Manager with Broadcast receiver

查看:173
本文介绍了Android的报警管理与广播接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有我报警管理器显示的通知。问题是,一旦警报被触发,该通知显示,则执行MainActivity的code(super.onCreate)时,它始终触发通知。

下面是我的MainActivity执行该报警。

  @覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    initAlarm();}私人无效initAlarm(){    台历挂历= 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);    //克里奥联合国意图阙ejecutara埃尔广播接收器
    意图myIntent =新意图(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);
}

下面是这应该仅在AlarmManager的时间到期被称为AlarmBroadcastReceiver。

 公共类AlarmBroadcastReceiver扩展广播接收器{    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        意图startIntent =新意图(背景下,AlarmService.class);
        context.startService(startIntent);
    }
}

由广播接收器推出的服务:

 公共类AlarmService延伸服务{
    @覆盖
    公众诠释onStartCommand(意向意图,诠释标志诠释startId){        generarNotificacion();        返回Service.START_NOT_STICKY;
    }@覆盖
公众的IBinder onBind(意向意图){
    返回null;
}公共无效generarNotificacion(){    意图resultIntent =新意图(this.getApplicationContext(),MainActivity.class);    android.support.v4.app.NotificationCompat.Builder mBuilder =
            新NotificationCompat.Builder(本)
                    .setSmallIcon(R.mipmap.logo)
                    .setContentTitle(getResources()。的getString(R.string.app_name))
                    .setContentText(getResources()的getString(R.string.texto_notificacion));    的PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    这个,
                    0,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );    mBuilder.setContentIntent(resultPendingIntent);    //设置一个ID为通知
    INT mNotificationId = 001;    //获取NotificationManager服务的一个实例
    NotificationManager mNotifyMgr =
            (NotificationManager)getSystemService(NOTIFICATION_SERVICE);    //构建通知和问题了。
    mNotifyMgr.notify(mNotificationId,mBuilder.build());
}}

最后我加入这个code到manifest.xml中

 <应用
        机器人:allowBackup =真
        机器人:图标=@的mipmap /标志
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>    <服务机器人:名字=。AlarmService
        机器人:启用=真/>    <接收机器人:名字=。AlarmBroadcastReceiver/>...< /用途>


解决方案

这是显而易见的。您调用的onCreate initAalarm()。要了解之后,都执行测试用例与code:


可以说,当前的日期和时间是2015年9月20日下午1点到
闹钟设置为将来的日期说,当前的时间,即2015年9月20日下午1点15分后
15分钟
现在来测试它,你可以等待时间到达或更改系统日期时间。这样做,你会看到通知上触发同样的time.Now不改变initAlarm(任何),接近活动,并再次启动它后,你将再次看到通知。这背后的原因是,如果警报设置为某个过去的日期相对于系统的时间,然后它会立即被解雇。

见<一个文档href=\"http://developer.android.com/reference/android/app/AlarmManager.html#set(int,%20long,%20android.app.PendingIntent)\"相对=nofollow>报警管理器的设置方法

Hello I have my Alarm Manager to show a Notification. The problem is that once the alarm is triggered and the notification is shown, when the code of MainActivity(super.onCreate) is executed, it always triggers the notification.

Here is my MainActivity which executes the Alarm.

@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);
}

Here is the AlarmBroadcastReceiver which is supposed to be called only when the time of the AlarmManager expires.

public class AlarmBroadcastReceiver extends BroadcastReceiver{

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

The service launched by the 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());
}

}

And finally I have added this code to the 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>

解决方案

It is obvious. You are calling initAalarm() in onCreate. To understand execute following test case with your code:

Lets say current date and time is 20 Sep 2015 1:00pm and Alarm is set to future date say 15 min after current time i.e. 20 Sep 2015 1:15pm
Now to test it you can either wait for time to arrive or change system date-time. After doing this you will see notification is fired on same time.Now don't change anything in initAlarm() , close activity and again start it , you will see notification again. The reason behind this is if alarm is set to some past date with respect system time then it is immediately fired.

See documentation of Alarm Manager's set method

这篇关于Android的报警管理与广播接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆