使用计时器从服务启动活动

时间:2013-03-20 07:53:23

标签: android android-activity alarmmanager

我希望每隔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" />

0 个答案:

没有答案