2013-11-22 92 views
1

我一直在嘗試使用AlarmManager.RTC_WAKEUP來喚醒我的設備來播放一些聲音。 這裏的報警調度:AlarmManager.RTC_WAKEUP沒有喚醒設備?

Intent intent = new Intent(Main.this, OneShotAlarm.class); 
PendingIntent sender = PendingIntent.getBroadcast(Main.this, 0, intent, 0); 

// I want the alarm to go off 60 seconds from now. 
Calendar calendar = Calendar.getInstance(); 
calendar.setTimeInMillis(System.currentTimeMillis()); 
calendar.add(Calendar.SECOND, 60); 

// Schedule the alarm! 
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); 
am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), sender); 

的OneShotAlarm廣播接收器:

public class OneShotAlarm extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     Toast.makeText(context, "OneShotAlarm Broadcast invoked", 1000).show(); 

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

     context.startService(intent); 
    } 
} 

的服務:

public class ServiceClassToRun extends Service { 
    protected static final int Rnd = 0; 

    public void onStart(Intent intent, int startId) { 
    //do some business here 
} 

最後,我的表現:當設備處於喚醒狀態

<receiver android:name=".OneShotAlarm" android:process=":remote" /> 
<service android:name=".ServiceClassToRun "/> 

我的服務工作正常。 任何想法,不勝感激。有沒有其他人經歷過這樣的 ?提前致謝。

+0

你有寫的所有權限的例子嗎? –

回答

0

您的服務 - ServiceClassToRun將在您設置警報1分鐘後調用。但您不會在onStart()方法中做任何事情時知道它,也可以重寫onBind和onCreate方法。

您可以實現通知或播放聲音時服務的OnStart方法被稱爲

你可以問,如果您有任何疑問。

+0

感謝您的快速回答。事實上,我沒有顯示那部分代碼,因爲我的問題會變得冗長。當設備處於喚醒狀態時,我的服務工作正常。 –

+0

那問題是什麼?使用通知以及鬧鈴 –

1
PendingIntent sender = PendingIntent.getBroadcast(Main.this, 0, intent, 0); 

變化

PendingIntent sender = PendingIntent.getBroadcast(Main.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

,或者,如果你錯過警報的權限,不是添加這些權限

<uses-permission android:name="android.permission.WAKE_LOCK"/> 
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/> 

讓我知道,如果這對你

+0

Is幫助我在鬧鈴觸發時間打開手機。 –

1

工作我懷疑這對作者是否仍然有用,但也許對其他人有用。 這個問題的原因可能是你沒有獲得任何喚醒鎖。當OneShotAlarm.onReceive被調用時,系統爲你保留一個喚醒鎖。你通過發送一個意向來開始一項服務。但在OneShotAlarm.onReceive返回後,沒有任何持續的喚醒鎖,設備進入睡眠狀態。因此,只有在您手動喚醒設備時纔會提供您的意圖。

你可以看到這裏激活鎖定https://github.com/yuriykulikov/AlarmClock/blob/develop/src/com/better/alarm/model/AlarmsService.java

最好的問候, 尤里