Java AlarmManager没有';行不通
这是我的密码。直到昨天,这个代码仍然运行良好。正确调用了MyAlarmManager(我可以看到日志),但没有调用MyReceiver。你知道为什么吗Java AlarmManager没有';行不通,java,android,android-activity,alarmmanager,Java,Android,Android Activity,Alarmmanager,这是我的密码。直到昨天,这个代码仍然运行良好。正确调用了MyAlarmManager(我可以看到日志),但没有调用MyReceiver。你知道为什么吗 public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //It will start the application at a
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");
}
}
}
public void onCreate()
{
//开始
Intent myIntent=新的Intent(this,MyReceiver.class);
pendingIntent=pendingIntent.getBroadcast(this,0,myIntent,0);
AlarmManager AlarmManager=(AlarmManager)getSystemService(报警服务);
pref=getApplicationContext().GetSharedReferences(
“设置”,模式(私人);
inth=pref.getInt(“小时”,-1);
int m=pref.getInt(“分钟”,-1);
日历开始=空;
日历开始时间2=空;
//如果用户在“选项”活动中添加了时间,则应用程序将在该时间启动
if(h!=-1&&m!=-1)
{
如果(PendingEvent!=null)
{
alarmManager.cancel(挂起事件);
}
Log.e(“hem”,h+“”+m);
GregorianCalendar now=新的GregorianCalendar();
开始=新的GregorianCalendar();
开始.设置(日历.小时/天,小时);
开始.设置(日历.分钟,m);
开始。设置(日历秒,0);
如果(h>=0&&h=12&&h=0&&h=12&&h2=12&&h2)你想在Android 4.4中运行代码吗?avd有Android 4.0,我的设备有Android 2.1(直到昨天还运行得很好)。我觉得一切都很好。你对代码或Android清单文件做了任何更改吗?还有,pendingIntent3
?没有问题。编码愉快!!!