我想每 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" />