2012-02-21 115 views
2

本教程來自android-er, 主要活動(AndroidScheduledActivity.java)啓動一個AlarmManager來重複觸發BroadcastReceiver(MyScheduledReceiver.java)。在MyScheduledReceiver的onReceive()方法中,它間接啓動另一個活動(MyScheduledActivity.java)。這樣活動(MyScheduledActivity.java)將按計劃的時間間隔開始。 現在我將使用AutoStart自動啓動,但我無法寫入AutoStartNotifyReceiver。 請你能給我一個想法如何管理它? 非常感謝!如何自動啓動AlarmManager以啓動預定活動?

主要活動,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(); 
    }}); 
    } 

} 

然後廣播接收器,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)){ 

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

    } 

} 
} 

回答

5

AutoStartNotifyReceiver extends BroadcastReceiver類是存在的,因爲設備重置時,警報會被清除。因此,在該課程的onReceive(您有問號)中,您需要重新設置警報,並使用onClick方法中第一次使用相同的代碼(當然,不包括finish()AndroidScheduledActivity

然後,你需要把下面的項在您的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多一點。我必須添加「背景」,不知道爲什麼。

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);   
     } 
    } 
}