BroadcastReceiver 动态注册

AlarmManger和BroadcastReciever执行定时任务,在连网时可正常运行,但无网络或wifi、移动数据同时开时,reciever接收不到信息。

添加 alarmReceiver.onReceive(context,intent);  后问题解决

执行定时任务

        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        IntentFilter filter = new IntentFilter();// 动态注册
        filter.addAction("tPhoto");
        registerReceiver(alarmReceiver, filter);//注册
        Intent intent = new Intent();
        intent.setAction("tPhoto");
        nowInfo.setText(CONST.nowExe);
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * CONST.inVal, pi);// 马上开始,**分钟触发一次
        alarmReceiver.onReceive(context,intent);//接收

接收

BroadcastReceiver alarmReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if ("tPhoto".equals(intent.getAction())) {
                   nowInfo.setText("开始运行任务");     
                } else {//sysTime >= stopTime                  
                    nowInfo.setText("暂停等待");
                }
        }
 };

你可能感兴趣的:(android,AlarmManager)