我通过AlarmManager调用BroadcastReceiver它对我来说很好. 但 这次我需要什么 我想创建多个AlarmManager,它调用相同的BroadcastReceiver类. 这个过程的方法是什么? 您可以通过使用与所使用的Pendin
但
这次我需要什么
我想创建多个AlarmManager,它调用相同的BroadcastReceiver类.
这个过程的方法是什么?
您可以通过使用与所使用的PendingIntent不同的请求代码设置警报来实现.确保每个警报都有唯一的请求代码.然后用一个广播接收器接收它们.要记住的另一件事是,如果设备重新启动,则待处理的意图不再存在.因此,您必须使用另一个接收器来确定设备是否重新启动.您必须保存唯一的请求代码,然后当设备重新启动时,您必须使用相同的请求代码恢复警报.希望能帮助到你.
编辑:
广播接收器
package com.your.package; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String Noti_title = intent.getExtras().getString("title"); String Noti_message = intent.getExtras().getString("notes"); int Noti_code = intent.getExtras().getInt("code"); Log.d("AlarmReciever", Noti_title + " " + Noti_message); Intent myIntent = new Intent(context, NotificationService.class); myIntent.putExtra("title", Noti_title); myIntent.putExtra("notes", Noti_message); myIntent.putExtra("code", Noti_code); context.startService(myIntent); } }
服务:
package com.your.package; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; import android.os.IBinder; public class NotificationService extends Service { private NotificationManager mManager; @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { super.onCreate(); } @SuppressWarnings({ "static-access", "deprecation" }) @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); String Noti_title = intent.getExtras().getString("title"); String Noti_message = intent.getExtras().getString("notes"); int Noti_Code = intent.getExtras().getInt("code"); mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE); Intent intent1 = new Intent(this.getApplicationContext(), MainActivity.class); Notification notification = new Notification(R.drawable.ic_launcher , Noti_title , System.currentTimeMillis()); intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT); notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.setLatestEventInfo(this.getApplicationContext(),Noti_title , Noti_message , pendingNotificationIntent); notification.vibrate = new long[] { 100L, 100L, 200L, 500L }; mManager.notify(Noti_Code , notification); try { Uri notification_uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification_uri); r.play(); } catch (Exception e) {} } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } }
用法:
public void SetAlarm(Calendar calendar, int reqCode) { String dateName = idea.getText().toString(); String dateNote = note.getText().toString(); Log.d("SetAlarm Texts", "Date : " + dateName + " Note: " + dateNote); Intent myIntent = new Intent(mActivity, AlarmReceiver.class); myIntent.putExtra("title", "Her : " + dateName); myIntent.putExtra("notes", dateNote); myIntent.putExtra("code", reqCode); PendingIntent pendingIntent = PendingIntent.getBroadcast(mActivity, reqCode, myIntent, 0); AlarmManager alarmManager = (AlarmManager) mActivity .getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent); }