3

本教程来自android-er,主要activity(AndroidScheduledActivity.java)启动一个AlarmManager来重复触发BroadcastReceiver(MyScheduledReceiver.java)。在 MyScheduledReceiver 的 onReceive() 方法中,它间接启动了另一个活动(MyScheduledActivity.java)。这样活动(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)){

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

  }

 }
}
4

2 回答 2

7

你的AutoStartNotifyReceiver extends BroadcastReceiver班级在那里,因为当设备重置时警报会被清除。所以,在onReceive这个类中(你有问号的地方)你需要用finish()你第一次onClickAndroidScheduledActivity.

然后,您需要将以下条目放入您的系统中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!

于 2012-02-21T23:52:43.337 回答
1

谢谢,它有效。只需要再改进一下我的java。我必须添加“上下文”不知道为什么。

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);           
        }
    }
}
于 2012-02-22T11:10:02.930 回答