更改活动的重复启动时间

时间:2013-11-01 17:19:58

标签: java android startup

我使用这些课程每天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中的启动时间(我仍然必须执行此类),但现在我不知道如何用新的启动时间替换旧的启动时间。你能救我吗?

1 个答案:

答案 0 :(得分:0)

使用AlarmManager的{​​{1}}方法删除旧的启动时间关联:

cancel(PendingIntent intent)

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