2013-04-22 45 views
0

我讀了很多教程,但沒有得到解決方案。我想使用AlarmManager進行同步。我希望當我選擇「15分鐘後」複選框時,在系統中設置當前時間的警報(如何在數據庫中保存該特定時間,重要的是在數據庫中節省時間?)和刷新列表每15分鐘。我不知道如何綁定checkBoxes值和警報,以及如何使它成爲可能。完全卡住。如何使用PendingIntent.activity()或PendingIntent.getBroadcast使用AlarmManager?

我的代碼:

private final class syncOkClickListener implements DialogInterface.OnClickListener 
{ 
    public void onClick(DialogInterface dialog, int which) 
    { //Implement search method here 
     if(after15mint.isChecked()) 
     { 
      // Some code!!! or Alarm Manager !!! 
     // Toast.makeText(getApplicationContext(), "15 Minute checked", Toast.LENGTH_LONG).show(); 
     } 
     else 
     { 
      Toast.makeText(getApplicationContext(), "Default", Toast.LENGTH_LONG).show(); 
     } 
    } 

BroadcastReceiver的活動:

public class BroadCastReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 


} 
public void SetAlarm(Context context) 
{ 
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(context.ALARM_SERVICE); 
    Intent syncIntent = new Intent(context, BroadCastReceiver.class); 
    syncIntent.putExtra("sync", 0); 
    PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, syncIntent, 0); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000*20, pIntent); 
} 
public void CancelAlarm(Context context) 
{ 
    Intent intent = new Intent(context, BroadCastReceiver.class); 
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0); 
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    alarmManager.cancel(sender); 
} 

}

任何建議將是有益的。謝謝。

回答

0

你可以設置你的onClick代碼的報警:

public void onClick(DialogInterface dialog, int which) 
{ //Implement search method here 
    if(after15mint.isChecked()) 
    { 
     // Create alarm intent. 
     Intent intent = new Intent(this, BroadCastReceiver.class); 
     PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0); 

     // Schedule alarm. 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 
     calendar.add(Calendar.MINUTE, 15); 
     AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); 
     am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender); 
    } 
    else 
    { 
     Toast.makeText(getApplicationContext(), "Default", Toast.LENGTH_LONG).show(); 
    } 
} 

沒有要求,爲您節省了報警時間在自己的數據庫中。

關於你閱讀的許多教程沒有幫助,一個好的開始是Android SDK的ApiDemos,你會在ApiDemos/App/Alarm/AlarmController中找到工作代碼,實現一次性警報以及重複報警。

+0

嗨感謝您的回覆,廣播接收器代碼是否正常? – Shweta 2013-04-24 05:07:49