如何从Android中另一个BroadcastReceiver的onReceive调用onReceive?

时间:2016-11-26 07:26:30

标签: android alarmmanager android-broadcastreceiver android-connectivitymanager

我正在开发一个Android应用程序,其中我需要从另一个BroadcastReceiver的onReceive(即Internet连接)调用Alarmmanager的onReceive方法。可能吗 ?或者我应该在另一个BroadcastReceiver中复制我的所有内容吗?

2 个答案:

答案 0 :(得分:1)

您可以从onReceive创建新意图以触发另一个广播接收器

@Override
public void onReceive(Context context, Intent intent) {
    Intent newIntent = new Intent("com.domain.yourboardcastreceiver");
    context.sendBroadcast(newIntent);
}

答案 1 :(得分:0)

在Internet连接广播接收器的OnReceive(..)方法中,您可以设置警报以及警报管理器将如何触发,例如:

@Override
public void onReceive(Context context, Intent intent) {
    Intent myIntent = new Intent(getBaseContext(), **AlarmReceiver**.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, myIntent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}

不要忘记在清单文件中注册alarmmanager的接收器。

希望它有所帮助!