android – 为广播接收器创建多个AlarmManager?

我通过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(),intent1,PendingIntent.FLAG_UPDATE_CURRENT);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        notification.setLatestEventInfo(this.getApplicationContext(),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性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...