2016-05-17 80 views
0

我想弄清楚鬧鐘定時器是如何工作的,以便在用戶選擇應用中的預定義時間時觸發事件。首先,我只想顯示敬酒,以便我可以清楚地看到該應用程序正在工作。但是當我運行該應用程序並設置時間爲10秒時,處理我的意圖的類似乎永遠不會被調用。觸發鬧鐘定時器的Android顯示屏吐司

我在Main中使用了Log.d,當按鈕被點擊時,我可以看到它被正確記錄。但是這個事件在選定的時間沒有發生。

這是當單擊按鈕並在控制檯中顯示Log.d時觸發的功能。

public void scheduleAlarm() 
    { 
     Long time = System.currentTimeMillis() + 10000; 
     Log.d("logs", "This is running in the main act"); 
     Intent intentAlarm = new Intent(this, affirmationSchedule.class); 

     AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

     alarmManager.set(AlarmManager.RTC_WAKEUP, time, PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT)); 
     Toast.makeText(this, "Alarm Has Been Scheduled", Toast.LENGTH_LONG).show(); 
    } 

這是處理的代碼時,報警時間已到

public class affirmationSchedule extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d("logs", "This function is running"); 
     Toast.makeText(context, "this is a toast working.", Toast.LENGTH_LONG).show(); 
    } 
} 

Log.d never displays. the toast in this class never displays. 

這使我相信我沒有正確地創建我的對象將運行的類。

這就是我在清單中註冊接收者的方式。

<receiver 
     android:name="com.wuno.wunoaffirmations" 
     android:enabled="true" 
     android:exported="true" > 
     <intent-filter> 
      <action android:name="com.wuno.BroadcastReceiver" /> 
     </intent-filter> 
    </receiver> 

任何想法?

這可能是相關的,

後,我按一下按鈕,原來的土司消失。這在控制檯彈出。

05-16 23:10:11.989 14242-14268/com.wuno.wunoaffirmations E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb4015c60 

但不到十秒鐘。更像5. AlarmManager設置爲10秒。

+1

你可以編輯你的原始問題。無論如何,''元素上的'name'屬性必須是您的'BroadcastReceiver'類名。其他一切看起來都不錯,但你並不需要'',或'enabled'或'exported'屬性。 –

+0

如果您的Receiver類位於主源文件夾中(即您的主「Activity」通常位於其中),那麼它只會是'。您可能還想使用'setExact()'方法,至少在測試時。從KitKat開始,'set()'方法是不精確的,並且可能會有所不同。 –

+0

好吧,有一種方法可以用adb來檢查你的應用程序的警報,但是如果你的''scheduleAlarm()'方法正在運行,那麼你的警報應該設置得很好。不知道。一切似乎都很好。嘗試清理和重建項目,並重新啓動您正在測試的設備或模擬器。哦,並確保您的''位於清單中的'標籤內。 –

回答

0

這怎麼我用我的一個項目中報警經理。基本上,我遵循谷歌的應用程序代碼中的一些代碼。所以在這裏。我希望這能幫到您。

如何使用它?那麼只需創建AlramReciver的實例,然後設置它。

private AlarmReceiver alarmReceiver = new AlarmReceiver(); 
alramReceiver.setAlram(); 

這是設置alram接收器的助手類。

public class AlarmReceiver extends WakefulBroadcastReceiver { 
    private static AlarmManager alarmManager; 
    private static PendingIntent alarmIntent; 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     /* 
     * If your receiver intent includes extras that need to be passed along to the 
     * service, use setComponent() to indicate that the service should handle the 
     * receiver's intent. For example: 
     * 
     * ComponentName comp = new ComponentName(context.getPackageName(), 
     *  MyService.class.getName()); 
     * 
     * // This intent passed in this call will include the wake lock extra as well as 
     * // the receiver intent contents. 
     * startWakefulService(context, (intent.setComponent(comp))); 
     * 
     * In this example, we simply create a new intent to deliver to the service. 
     * This intent holds an extra identifying the wake lock. 
     */ 
     Intent service= new Intent(context, AlarmService.class); 
     startWakefulService(context,service); 
    } 

    /** 
    *set the alram 
    * @param context 
    */ 
    public void setAlarm(Context context){ 
     alarmManager=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
     Intent intent = new Intent(context, AlarmReceiver.class); 
     alarmIntent =PendingIntent.getBroadcast(context,0,intent,0); 
     alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), 1000 * 60, alarmIntent); 

     ComponentName receiver = new ComponentName(context, BootReceiver.class); 
     PackageManager pm = context.getPackageManager(); 

     pm.setComponentEnabledSetting(receiver, 
       PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 
       PackageManager.DONT_KILL_APP); 
    } 

    /** 
    * cancels the alram 
    * @param context 
    */ 
    public void cancelAlarm(Context context){ 
     // If the alarm has been set, cancel it. 
     if (alarmManager!= null) { 
      alarmManager.cancel(alarmIntent); 
     } 
     // Disable {@code SampleBootReceiver} so that it doesn't automatically restart the 
     // alarm when the device is rebooted. 
     ComponentName receiver = new ComponentName(context, BootReceiver.class); 
     PackageManager pm = context.getPackageManager(); 

     pm.setComponentEnabledSetting(receiver, 
       PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 
       PackageManager.DONT_KILL_APP); 
    } 
} 

這是用來bootReceiver類時,你的設備熄滅,在這裏再次開啓

public class BootReceiver extends BroadcastReceiver { 
    AlarmReceiver alarmReceiver = new AlarmReceiver(); 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) 
     { 
      alarmReceiver.setAlarm(context); 
     } 
    } 
} 

這是意圖服務類,你必須寫你的邏輯爲您的應用程序。

公共類AlarmService擴展IntentService {

public AlarmService() { 
    super("AlarmService"); 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    //Write the logice here 
    AlarmReceiver.completeWakefulIntent(intent); // this tell if the related work is complete then system tracks for another alram. 
} 

最後你必須讓你的清單的變化。

<service android:name="AlarmService" /> 
    <receiver android:name="AlarmReceiver" /> 
    <receiver android:name="BootReceiver" /> 

我希望這會幫助你至少。 P.s你不必發表兩次相同的問題。

+0

感謝這個人,這似乎是最終遊戲的理想選擇。 – wuno

+0

嘿,男人一切都對我有意義接受這部分。 private AlarmReceiver alarmReceiver = new AlarmReceiver(); alarmReceiver.setAlarm();你介意添加一些關於這方面的細節嗎? – wuno

+0

你將不得不使用這段代碼,在你想從哪裏開始alram並設置它的任何活動中。然後,您可以通過創建同一個類的實例並調用cancelalram()來取消任何課程或活動中的相同alram() –

0
public void scheduleAlarm() 
    { 
     Calendar cal = Calendar.getInstance(); 
     Log.d("logs", "This is running in the main act"); 
     Intent intentAlarm = new Intent(this, affirmationSchedule.class); 

     AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

     alarmManager.set(AlarmManager.RTC_WAKEUP, cal + 10000, PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT)); 
     Toast.makeText(this, "Alarm Has Been Scheduled", Toast.LENGTH_LONG).show(); 
    } 

廣播接收機

public class affirmationSchedule extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d("logs", "This function is running"); 
     Toast.makeText(context, "this is a toast so this shit is fucking working.", Toast.LENGTH_LONG).show(); 
    } 
} 

在清單

<receiver 
     android:name="com.wuno.affirmationSchedule" 
     android:enabled="true" 
     android:exported="true" > 
     <intent-filter> 
      <action android:name="com.wuno.BroadcastReceiver" /> 
     </intent-filter> 
    </receiver>