0

我每天 20:00 使用这些课程开始我的活动。

public class MainActivity extends Activity 
{
    private PendingIntent pendingIntent;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 20);
    calendar.set(Calendar.MINUTE, 00);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.AM_PM,Calendar.PM);

    Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
    alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);

    }
}

这是接收器类:

public class MyReceiver extends BroadcastReceiver
{ 
    @Override
    public void onReceive(Context context, Intent intent)
    {
       Intent service1 = new Intent(context, MyApp.class);
       context.startService(service1);

     }
}

现在我想让用户更改 MyApp.class 中的启动时间(我仍然必须做这个类),但现在我不知道如何用新的启动时间替换旧的启动时间。你能帮助我吗?

4

1 回答 1

0

使用AlarmManager'scancel(PendingIntent intent)方法删除旧的启动时间关联:

boolean alarmExists = (PendingIntent.getBroadcast(context, 0, pendingIntent, PendingIntent.FLAG_NO_CREATE) != null);

// remove any pre-existing alarms associated with this Intent
if (alarmExists) {
   alarmManager.cancel(pendingIntent)
}

然后,您可以使用相同的逻辑重新创建重复警报。

于 2013-11-01T19:00:56.440 回答