2012-03-11 61 views
0

任何人都可以告訴我如何在Android應用程序中創建一個從當前時間開始的鬧鐘,並在特定時間內每10分鐘重複一次?在特定時間內發出警報Android應用程序

例如,根據不同情況,警報應該重複的時間爲2小時,另一種情況可能僅爲1:30小時等等。

有沒有辦法做到這一點?

其實我用我把它從網絡代碼,如下圖所示:

公共類MyAlarmService延伸服務{

@Override 
    public void onCreate() { 

     // TODO Auto-generated method stub 

    } 

    @Override 
    public IBinder onBind(Intent intent) { 

     // TODO Auto-generated method stub 

     Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG) 
       .show(); 

     return null; 

    } 

    @Override 
    public void onDestroy() { 

     // TODO Auto-generated method stub 

     super.onDestroy(); 

     Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG) 
       .show(); 

    } 

    @Override 
    public void onStart(Intent intent, int startId) { 

     // TODO Auto-generated method stub 

     super.onStart(intent, startId); 

     Toast.makeText(this, "...", Toast.LENGTH_LONG) 
     .show(); 

    } 

    @Override 
    public boolean onUnbind(Intent intent) { 

     // TODO Auto-generated method stub 

     Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG) 
       .show(); 

     return super.onUnbind(intent); 

    } 

} 

回答

0

您可以使用一個BroadcastReceiver和的PendingIntent。首先聲明的廣播接收器:

public class EventAlarmReceiver extends BroadcastReceiver { 

private long alarmTime; 

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

    //declare the new time for the alarm, you can use a time stamp, for example in the next hour 
    alarmTime = Calendar.getInstance().getTimeInMillis()+(60*60000); 

      //set an intent to the Receiver 
    Intent intent = new Intent(context, EventAlarmReceiver.class); 

      //set an PendingIntent to the Intent 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
       getApplicationContext(), 234324243, intent, 0); 

      //add to the System Alarm Manager 
    AlarmManager alarmManager = (AlarmManager) getSystemService(Activity.ALARM_SERVICE); 
    alarmManager.set(AlarmManager.RTC, alarmTime, pendingIntent); 
} 

}

,並開始在活動報警:

Intent intent = new Intent(context, EventAlarmReceiver.class); 

     PendingIntent pendingIntent = PendingIntent.getBroadcast(
       getApplicationContext(), 234324243, intent, 0); 

     AlarmManager alarmManager = (AlarmManager) getSystemService(Activity.ALARM_SERVICE); 
     alarmManager.set(AlarmManager.RTC, time, pendingIntent);