使用多个警报管理器即使应用程序已关闭

时间:2013-09-10 10:07:42

标签: android alarmmanager

抱歉,我的英语不好。我有一个应用程序及其触发多重警报和打开时打盹。但是当应用程序关闭时它只触发第一个警报,然后(即使我打盹),他也不会触发任何警报。我的课程:

当我拨打闹钟时:

public static void alarm_event(Context context) {
        try {

            ArrayList<actividades> A = new ArrayList<actividades>();
            A = actividades.getActivitdade_for_alarm(context);
            int i = 0;
            while (i < A.size()) {
                Intent myIntent = new Intent(context, alarm_receiver.class);
                PendingIntent pilocal = PendingIntent.getBroadcast(context,
                        100 + A.get(i).getId(), myIntent, 0);
                AlarmManager alarmManager = (AlarmManager) context
                        .getSystemService(Context.ALARM_SERVICE);
                alarmManager.cancel(pilocal);
                pilocal.cancel();
                Log.d("A_organizer Services", "Alarm detached");
                i++;
            }
            i = 0;
            while (i < A.size()) {
                // Toast.makeText(context,
                // "Time to alarme "+A.get(i).getAlarm_hora().getTimeInMillis(),
                // Toast.LENGTH_LONG).show();
                if (A.get(i).isAlarm()) {
                    PendingIntent pendingIntent;
                    final int _id = (int) System.currentTimeMillis();
                    Log.d("A_organizer Services", "Alarm atached");
                    AlarmManager alarmManager = (AlarmManager) context
                            .getSystemService(Context.ALARM_SERVICE);
                    Intent myIntent = new Intent(context, alarm_receiver.class);
                    myIntent.putExtra("Event", A.get(i));
                    pendingIntent = PendingIntent.getBroadcast(context, 100 + A
                            .get(i).getId(), myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    alarmManager.set(AlarmManager.RTC_WAKEUP, A.get(i)
                            .getAlarm_hora().getTimeInMillis(), pendingIntent);

                }
                i++;
            }
        } catch (Exception e) {
            Mensagens_Alerta.showSimple_OK_Alert("Error " + e, context);
        }

    }

我的闹钟接收器:

public class alarm_receiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        try{
            actividades A = (actividades)  intent.getSerializableExtra("Event");        
            Intent service1 = new Intent(context, Alarm_service.class);
            service1.putExtra("Event", A);
             context.startService(service1);         
        } catch (Exception e) {
            Mensagens_Alerta.showSimple_OK_Alert("Error "+e, context);
        }

    }

}

我的警报服务:

@SuppressWarnings("static-access")
    @Override
    public void onStart(Intent intent, int startId) {

        super.onStart(intent, startId);
        Intent I = new Intent(this.getApplicationContext(), dialog_alarm_activity.class);
        I.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        actividades A = (actividades) intent.getSerializableExtra("Event");
        I.putExtra("Event", A);
        I.setFlags(I.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY);
        this.startActivity(I);

    }

透明对话框Stop,Snoozed activity:

public class dialog_alarm_activity extends Activity {
    public static PendingIntent pendingIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        final actividades A = (actividades) getIntent().getSerializableExtra(
                "Event");
        SimpleDateFormat formatDate = new SimpleDateFormat("dd/MM/yyyy");
        final SimpleDateFormat formatTime = new SimpleDateFormat("HH:mm");

        //getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
           PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
           final WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
           if(pm.isScreenOn()){

           }
           else{
               wakeLock.acquire();
               Handler handler=new Handler();
               Runnable r=new Runnable()
               {
                   public void run() 
                   {
                       wakeLock.release();          
                   }
               };
               handler.postDelayed(r, 5000);                 
           }         
         new AlertDialog.Builder(dialog_alarm_activity.this)
                .setTitle(R.string.app_name)
                .setMessage(
                        "Alarm - "
                                + A.getNome()
                                + " '"
                                + formatTime.format(Calendar.getInstance()
                                        .getTime()) + "'")
                .setPositiveButton("Stop", new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub
                    }
                }).setNeutralButton("Snooze", new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub
                        Calendar C = Calendar.getInstance();
                        C.add(Calendar.MINUTE, 1);
                        C.set(Calendar.SECOND, 0);
                        Log.d("A_organizer Services",
                                "Snoozed - " + formatTime.format(C.getTime()));

                        AlarmManager alarmManager = (AlarmManager) dialog_alarm_activity.this
                                .getSystemService(Context.ALARM_SERVICE);
                        Intent myIntent = new Intent(
                                dialog_alarm_activity.this,
                                alarm_receiver.class);
                        myIntent.putExtra("Event", A);
                        pendingIntent = PendingIntent.getBroadcast(
                                dialog_alarm_activity.this, 0, myIntent,
                                PendingIntent.FLAG_UPDATE_CURRENT);
                        alarmManager.set(AlarmManager.RTC_WAKEUP,
                                C.getTimeInMillis(), pendingIntent);
                        Log.d("Alarme", "Alarme posto");
                    }
                }).show();
        super.onCreate(savedInstanceState);
    }

}

1 个答案:

答案 0 :(得分:0)

抱歉是我的错误......在“透明对话框停止,暂停活动”中我应该在点击暂停或停止后完成它!因为出现了透明的活动,并且出现了新的警报/贪睡!

现在谁知道我打开电源时如何为我的屏幕做什么,会带来一条消息(如闹钟名称或其他什么?)。