0

这是我的代码。这段代码直到昨天都运行良好。MyAlarmManager 被正确调用(我可以看到日志),但没有调用 MyReceiver。你知道为什么吗?

public class MyReceiver extends BroadcastReceiver
{ 
    @Override
    public void onReceive(Context context, Intent intent)
    {
       //It will start the application at a certain time
       Intent i = new Intent(context, Calibration.class);
       i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       context.startActivity(i);
       Log.e("MyReceiver", "MyReceiver");
    }
}

这是 AlarmManager 类

public void onCreate() 
    {   
        //Start
        Intent myIntent = new Intent(this, MyReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent,0);
        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        pref = getApplicationContext().getSharedPreferences(
                "settings", MODE_PRIVATE);
        int h=pref.getInt("hour", -1);
        int m=pref.getInt("minute", -1);
        Calendar start=null;
        Calendar start2=null;
        //If the user added a time in the options activity, the application will start at that time
        if(h!=-1 && m!=-1)
        {
            if(pendingIntent != null) 
            {
               alarmManager.cancel(pendingIntent);
            }
            Log.e("h e m", h+" "+m);
            GregorianCalendar now = new GregorianCalendar();
            start = new GregorianCalendar();
            start.set(Calendar.HOUR_OF_DAY, h);
            start.set(Calendar.MINUTE, m);
            start.set(Calendar.SECOND, 0);
            if(h>=0 && h<12)
                start.set(Calendar.AM_PM,Calendar.AM);
            if(h>=12 && h<=23)
                start.set(Calendar.AM_PM,Calendar.PM);
            if(start.after(now))
            {
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, start.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
                alarmManager.set(AlarmManager.RTC, start.getTimeInMillis(), pendingIntent);
                Log.e("MyAlarmManager", "MyAlarmManager");
            }
        }
        //Start 2
        Intent myIntent2 = new Intent(this, MyReceiver.class);
        pendingIntent2 = PendingIntent.getBroadcast(this, 2, myIntent2,0);
        int h2=pref.getInt("hour2", -1);
        int m2=pref.getInt("minute2", -1);
        //If the user added a time in the options activity, the application will start at that time
        if(h2!=-1 && m2!=-1)
        {
            if(pendingIntent3 != null) 
            {
               alarmManager.cancel(pendingIntent2);
            }
            Log.e("h2 e m2", h2+" "+m2);
            GregorianCalendar now = new GregorianCalendar();
            start2 = new GregorianCalendar();
            start2.set(Calendar.HOUR_OF_DAY, h2);
            start2.set(Calendar.MINUTE, m2);
            start2.set(Calendar.SECOND, 0);
            if(h2>=0 && h2<12)
                start2.set(Calendar.AM_PM,Calendar.AM);
            if(h2>=12 && h2<=23)
                start2.set(Calendar.AM_PM,Calendar.PM);
            if(start2.after(now))
            {
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, start2.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent3);
                alarmManager.set(AlarmManager.RTC, start2.getTimeInMillis(), pendingIntent3);
                Log.e("MyAlarmManager3", "MyAlarmManager3");
            }
        }
    }
4

0 回答 0