我希望每隔10分钟从服务中调用我的主要活动。我该怎么做呢?
我尝试使用AlarmManager
。我使用以下代码触发了MyAppReceiver
:
private void setNextSchedule()
{
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getBroadcast(this, 0,new Intent(this, MyAppReciever.class), 0);
long duration = 20000;
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(), duration, pi);
}
并在onStartCommand()
服务中调用它,但它无效。
public class MyAppReciever extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
Toast.makeText(context,"in myAppreceiver",Toast.LENGTH_LONG).show();
context.startActivity(new Intent(context, MainActivity.class));
}
}
在清单中
<receiver android:name=".MyAppReciever" />