2014-10-08 125 views
0

我有一個IntentService每30秒被一個警報觸發... 第一個警報由用戶設置。當廣播收到第一個警報時,它會調用IntentService,IntentService將啓動另一個將在30秒內觸發的警報。 但它總是給的我很多這logcat的:發送消息給處於死線程的處理程序

10-08 14:32:43.433: W/MessageQueue(23819): Handler{40568030} sending message to a Handler on a dead thread 
10-08 14:32:43.433: W/MessageQueue(23819): java.lang.RuntimeException: Handler{40568030} sending message to a Handler on a dead thread 
10-08 14:32:43.433: W/MessageQueue(23819): at android.os.MessageQueue.enqueueMessage(MessageQueue.java:196) 
10-08 14:32:43.433: W/MessageQueue(23819): at android.os.Handler.sendMessageAtTime(Handler.java:457) 
10-08 14:32:43.433: W/MessageQueue(23819): at android.os.Handler.sendMessageDelayed(Handler.java:430) 
10-08 14:32:43.433: W/MessageQueue(23819): at android.os.Handler.sendMessage(Handler.java:367) 
10-08 14:32:43.433: W/MessageQueue(23819): at android.location.LocationManager$ListenerTransport.onLocationChanged(LocationManager.java:193) 
10-08 14:32:43.433: W/MessageQueue(23819): at android.location.ILocationListener$Stub.onTransact(ILocationListener.java:58) 
10-08 14:32:43.433: W/MessageQueue(23819): at android.os.Binder.execTransact(Binder.java:336) 
10-08 14:32:43.433: W/MessageQueue(23819): at dalvik.system.NativeStart.run(Native Method) 

這是我IntentService:

public class ServiceGPS extends IntentService{ 

    static String APP = "ACORDENOPONTO"; 

    private static volatile PowerManager.WakeLock lockStatic=null; 

    synchronized private static PowerManager.WakeLock getLock(Context context) { 
     if (lockStatic == null) { 
      PowerManager mgr= 
      (PowerManager)context.getSystemService(Context.POWER_SERVICE); 

      lockStatic=mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, APP); 
      lockStatic.setReferenceCounted(true); 
     } 

     return(lockStatic); 
    } 

    public ServiceGPS() { 
     super("ServiceGPS"); 

    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     PowerManager.WakeLock lock=getLock(this.getApplicationContext()); 

     if (!lock.isHeld() || (flags & START_FLAG_REDELIVERY) != 0){ 
      lock.acquire(); 
     } 

     super.onStartCommand(intent, flags, startId); 
     return(START_REDELIVER_INTENT); 

    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 

     params.getCharSequence("tickerText") 
     NotificationManager.criaNotification(this, params.getCharSequence("tickerText") 
               , params.getCharSequence("title") 
               , params.getCharSequence("message") 
               , params.getInt("id") 
               , (30)); 
     } 

     PowerManager.WakeLock lock=getLock(this.getApplicationContext()); 

     if(lock.isHeld()){ 
      lock.release(); 
     } 

     MonitoraGPS.completeWakefulIntent(intent); 
    } 

    public void tocaAlarmeVibraTudo(Context context){ 
     Intent intent = new Intent(context, TelaDespertou.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(intent); 
    } 

添加NotificationManager代碼:

public class NotificationManager{ 
    static AlarmManager alarme; 

    public static void criaNotification(Context context, CharSequence tickerText, CharSequence title, 
      CharSequence message, int id, float segundos){ 

     alarme = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 

     Intent abreMonitora = new Intent("MONITORAR"); 

     Bundle params = new Bundle(); 

     params.putCharSequence("tickerText", tickerText); 
     params.putCharSequence("title", title); 
     params.putCharSequence("message", message); 
     params.putInt("id", id); 

     abreMonitora.putExtras(params); 

     PendingIntent vaiFazer = PendingIntent.getBroadcast(context, id, abreMonitora, 
                 PendingIntent.FLAG_CANCEL_CURRENT); 

     long minute = minuteToMilli(segundos); 

     alarme.set(AlarmManager.RTC_WAKEUP, 
       System.currentTimeMillis() + minute, 
       vaiFazer); 

     Log.d("setou", "alarme" + segundos); 

     NotificationUtil.criaNotification(context, tickerText, title, message, id, intent); 

    } 

    public static long minuteToMilli(float minute){ 
     long quaseMilli = (long) (minute * 1000); 
     return quaseMilli; 
    } 

    public static void cancelaAlarme(Context context, int id){ 
     alarme = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
     PendingIntent operation = PendingIntent.getBroadcast(context, id, 
                 new Intent("MONITORAR"), 
                 PendingIntent.FLAG_CANCEL_CURRENT); 
     alarme.cancel(operation); 
    } 

} 

回答

0

有幾件事情需要考慮可以幫助您擺脫錯誤並獲得更清晰的代碼:

  • 應該使用AlarmManager來安排警報
  • ServiceGps可以由WakefulBroadcastReceiver啓動,因此您不必自己處理喚醒鎖。
  • 如果目標低於19,您可以在AlarmManager中使用setRepeating ...方法獲益。

**不要忘記您的Manifest中的權限。

希望它有幫助。

+0

重複不總是相同的......「NotificationManager」只有類的名稱,因爲它處理警報並向用戶發送通知...是的,我從WakefulBroadcastReceiver ... – 2014-10-08 17:59:20

+0

我假設你的* NotificationManager *是由於方法調用而定製的。你可以發佈該代碼嗎? – 2014-10-08 18:04:07

+0

新增'NotificationManager'代碼 – 2014-10-08 18:08:57

相關問題