我通过AlarmManager调用broadcastReceiver它对我来说很好.
但
这次我需要什么
我想创建多个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); }
总结 以上是内存溢出为你收集整理的android – 为广播接收器创建多个AlarmManager?全部内容,希望文章能够帮你解决android – 为广播接收器创建多个AlarmManager?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)