使用服务设置警报时未接通onReceive

时间:2014-04-22 21:08:35

标签: android service alarmmanager

我一直在按照一些教程创建一个警报,当onRecieve()方法触发时,我使用TextToSpeech API然后发言。在这样做时,我发现在扩展BroadcastReciever时我无法初始化TextToSpeech,但发现了一个使用Service而不是BroadcastReciever的教程。

问题是onRecieve()方法永远不会被调用,并且没有显示错误表明警报没有正确设置。

以下是我在MainActivity类中设置闹钟的方法。

protected void setAlarm() {

      Long time = timeTillAlarm;
      Log.v(TAG, "Time til Alarm = " + time);

    // create an Intent and set the class which will execute when Alarm triggers, here we have
    // given AlarmReciever in the Intent, the onRecieve() method of this class will execute when alarm triggers 
    Intent intentAlarm = new Intent(MainActivity.this, AlarmReciever.class);
    PendingIntent pIntent = PendingIntent.getService(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);

    // create the object
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    //set the alarm for particular time
    alarmManager.set(AlarmManager.RTC_WAKEUP, time, pIntent);


}

以下是我的AlarmReciever类:

public class AlarmReciever extends Service implements TextToSpeech.OnInitListener{
 private static final String TAG = "DEBUG";
 private TextToSpeech tts;
 private String spokenText;

 public AlarmReciever()
 {

 }

 public void onCreate() {
     tts = new TextToSpeech(this, this);
     spokenText = "Hello Kyle. Wake up now, it is a lovely day!";
     Log.v(TAG, "Alarm Text: " + spokenText);
     //speakOut();

 }

 public void onReceive(Context context, Intent intent)
 {  
    Log.v(TAG, "Recieved Alarm"); 
    speakOut();
 }

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

  @Override
     public void onInit(int status) {
      Log.v(TAG, "TTS Initialised");
         if (status == TextToSpeech.SUCCESS) {
             int result = tts.setLanguage(Locale.UK);
             if (result != TextToSpeech.LANG_MISSING_DATA && result != TextToSpeech.LANG_NOT_SUPPORTED) {
                // Log.v(TAG, "SPEAKING IN INIT");
                  //  tts.speak(spokenText, TextToSpeech.QUEUE_FLUSH, null);
             }
         }

     }

  @Override
    public void onDestroy() {
        // Don't forget to shutdown tts!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

  private void speakOut() {
        Log.v(TAG, "SPEAKING");
        tts.speak(spokenText, TextToSpeech.QUEUE_FLUSH, null);
    } }

非常感谢任何有关此问题的帮助:)

1 个答案:

答案 0 :(得分:0)

可能出错的一些事情:

  1. 您是否在Android Manifest.xml中声明了AlarmReceiver? (拼写正确吗?) <receiver android:name=”path.to.package.AlarmReceiver”></receiver>

  2. 您是否设置了正确的请求代码? 那个花了我几个小时:在请求你的广播时,你必须提供一个请求代码。 Altought我还没有发现它的使用,尝试将其设置为任意随机数。 e.g。

      

    PendingIntent pIntent = PendingIntent.getService(this,564864421,intentAlarm,PendingIntent.FLAG_UPDATE_CURRENT);

    当我将它设置为0时,它确实在一个示例项目中工作(只有一个活动和警报),但是当我将它集成到真实交易(15个活动,服务等)时,它没有&# 39;不再工作了。 requestCode的目的解释为here

  3. 确保您的服务足够清醒: AlarmManager只调用onReceive,但没有获得唤醒锁定 - 您必须自己处理。我在这里使用WakefulBroadcastReceiver,因为它就是这样。如果你使用它(或唤醒锁定),请确保你在清单中有这个:<uses-permission android:name="android.permission.WAKE_LOCK" />

  4. 最后,这是我的工作示例:

    public class Alarm extends WakefulBroadcastReceiver {
    
        private AlarmManager alarmMgr;
        private PendingIntent alarmIntent;
    
        @Override
        public void onReceive(Context context, Intent intent) {   
            Intent service = new Intent(context, AlarmService.class);
            Log.d("AlarmService", "Alarm received");
    
            // Start the service, keeping the device awake while it is launching.
            startWakefulService(context, service);
        }
    
        @SuppressLint("NewApi")
        public void setAlarm(Context context, long inMillis) {
            boolean alarmUp = PendingIntent.getBroadcast(context, 0, new Intent(context, Alarm.class), PendingIntent.FLAG_NO_CREATE) != null;
            if (alarmUp)
                Log.d("AlarmService", "Alarm is already up");
            else
                Log.d("AlarmService", "Alarm is NOT active");
    
            alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(context, Alarm.class);
            alarmIntent = PendingIntent.getBroadcast(context, 5468461, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            alarmMgr.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+inMillis, alarmIntent);
    
            Log.d("AlarmService", "alarm set");
        }
    
        public void cancelAlarm(Context context) {
            // If the alarm has been set, cancel it.
            if (alarmMgr!= null) {
                alarmMgr.cancel(alarmIntent);
            }
            Log.d("AlarmService", "alarm canceled");    
        }
    }
    

    <receiver android:name="path.to.Alarm" />