如何自动启动AlarmManager以启动预定活动?

3
这篇教程来源于android-er,主活动(AndroidScheduledActivity.java)启动AlarmManager来触发广播接收器(MyScheduledReceiver.java)的重复执行。在MyScheduledReceiver的onReceive()方法中,它间接地启动另一个活动(MyScheduledActivity.java),使得该活动会在预定的时间间隔内启动。 现在我想使用AutoStart来自动启动,但我不知道如何编写AutoStartNotifyReceiver,请问您能否给我一些建议? 非常感谢! 主活动AndroidScheduledActivity.java:
public class AndroidScheduledActivity extends Activity {

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      Button buttonStart = (Button)findViewById(R.id.start);
      buttonStart.setOnClickListener(new Button.OnClickListener(){

  @Override
  public void onClick(View arg0) {
    Intent myIntent = new Intent(getBaseContext(),
      MyScheduledReceiver.class);

    PendingIntent pendingIntent
     = PendingIntent.getBroadcast(getBaseContext(),
       0, myIntent, 0);

    AlarmManager alarmManager
      = (AlarmManager)getSystemService(ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 10);
    long interval = 60 * 1000; //
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
      calendar.getTimeInMillis(), interval, pendingIntent);
    finish();
  }});
  }

}

然后是 BroadcastReceiver,MyScheduledReceiver.java

public class MyScheduledReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {

 Intent scheduledIntent = new Intent(context, MyScheduledActivity.class);
 scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 context.startActivity(scheduledIntent);
}
}

我的问题是AutoStartNotifyReceiver:

public class AutoStartNotifyReceiver extends BroadcastReceiver {

 private final String BOOT_COMPLETED_ACTION = "android.intent.action.BOOT_COMPLETED";

 @Override
 public void onReceive(Context context, Intent intent) {

  if(intent.getAction().equals(BOOT_COMPLETED_ACTION)){

????????????????????

  }

 }
}
2个回答

7
你的AutoStartNotifyReceiver类继承自BroadcastReceiver,因为当设备重置时会清除闹钟。所以,在这个类的onReceive方法中(你用问号标记的地方),你需要再次设置相同代码的闹钟(当然不包括finish()),就像在AndroidScheduledActivity的onClick方法中第一次设置一样。
然后,你需要在Manifest中添加以下条目,让系统知道在启动时要启动你的AutoStartNotifyReceiver:
<receiver android:name=".AutoStartNotifyReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
</receiver>

除了在 Manifest 中的权限外:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

现在,这一切都是假设你只有一个警报并且每次只设置一个警报。如果不是这种情况,那么就会变得更加复杂。但基于你提供的少量信息,我的解决方案应该可以满足你的要求。
另外,由于你是新来的,请注意:当有人对问题提供了充分的答案时,提问者(也就是你)通过点击答案旁边的复选框接受答案。这样回答者就会得到积分。欢迎来到SO!

1

谢谢,它起作用了。只需再提高一下我的Java水平。我必须添加“context”,但不知道为什么。

public class AutoStartNotifyReceiver extends BroadcastReceiver {
    private final String BOOT_COMPLETED_ACTION = "android.intent.action.BOOT_COMPLETED";
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(BOOT_COMPLETED_ACTION)){

             Intent myIntent = new Intent(context, MyScheduledReceiver.class);
             PendingIntent pendingIntent = PendingIntent.getBroadcast(context,  0, myIntent, 0);

             AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
             Calendar calendar = Calendar.getInstance();
             calendar.setTimeInMillis(System.currentTimeMillis());
             calendar.add(Calendar.SECOND, 10);
             long interval = 60 * 1000;
             alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pendingIntent);           
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接