2012-07-11 152 views
0

我打電話從broadcastReciever我的類服務類是由於: -爲什麼AlarmMAnager不工作?

InsertIntoDB objinsert; 
ArrayList<ContactClass> resultList; 
ContactClass mContactClass =null; 

public ScheduleService() { 

    super("B'Day Checker Service"); 


} 

@Override 
protected void onHandleIntent(Intent intent) 
    { 
    objinsert = new InsertIntoDB(this); 
    Log.d("MyService", "About to execute MyTask"); 
    new MyTask().execute(); 

    } 

private class MyTask extends AsyncTask<Void, Void, ArrayList<ContactClass>> 
    { 

    @Override 
    protected void onPreExecute() { 

     super.onPreExecute(); 
     mContactClass = new ContactClass(); 
     resultList= new ArrayList<ContactClass>(); 
    } 

    @Override 
    protected ArrayList<ContactClass> doInBackground(Void... arg0) 
    { 
     List<ContactClass> contacts = objinsert.getAllContacts(); 
     for (ContactClass cn : contacts) 
     { 
      String mdob = cn.getDob(); 
      long output = dateCompare(mdob); 
      if(output==0) 
      { 
       mContactClass.setId(cn.getId()); 
       mContactClass.setName(cn.getName()); 
       mContactClass.setDob(cn.getDob()); 
       resultList.add(mContactClass); 
      } 

       Log.d("Name: ",""+resultList.size()); 
     } 
     return resultList; 
    } 

    @Override 
    protected void onPostExecute(ArrayList<ContactClass> result) { 

     super.onPostExecute(result); 
     System.out.println("posttttt"); 
     int length=0; 
     if(result!=null) 
     { 
     length = result.size(); 
     } 
     if(length!=0) 
     { 
      for(int mindex =0;mindex<length;mindex++) 
      { 
       String date = result.get(mindex).getDob(); 
       String[] arrdob =date.split("/"); 
       Calendar cal = Calendar.getInstance(); 
       cal.setTimeInMillis(System.currentTimeMillis()); 
       cal.clear(); 
       cal.set(Integer.parseInt(arrdob[2]), Integer.parseInt(arrdob[1])-1,Integer.parseInt(arrdob[0]),14,17); 

       AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
       Intent intent = new Intent(ScheduleService.this, BirthDayReciever.class); 
       PendingIntent pendingIntent = PendingIntent.getBroadcast(ScheduleService.this, 0, intent, 0); 
       //cal.add(Calendar.SECOND, 5); 
       alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent); 
      } 
     } 


    } 



} 


public long dateCompare(String date) 
{ 
    System.out.println("date---"+date); 
    String geivendate[] = date.split("/"); 
    int Day= Integer.parseInt(geivendate[0]); 
    int Month= Integer.parseInt(geivendate[1]); 
    int Year= Integer.parseInt(geivendate[2]); 

    Date currentdate = new Date(); 
    Calendar calUpdated = Calendar.getInstance(); 
    calUpdated.set(Year, Month-1, Day); 

    long updateDateMilliSec = calUpdated.getTimeInMillis(); 
    long diff = currentdate.getTime() - calUpdated.getTimeInMillis(); 
    long diffDays = (diff/(1000 * 60 * 60 * 24)); 

    return diffDays; 
} 

但它不是工作doinBackground方法之後運行在logcat中沒有應用程序崩潰只在logcat的一個例外是: -

07-11 14:16:34.444: W/MessageQueue(942): Handler{44f6ad40} sending message to a Handler on a dead thread 
07-11 14:16:34.444: W/MessageQueue(942): java.lang.RuntimeException: Handler{44f6ad40} sending message to a Handler on a dead thread 
07-11 14:16:34.444: W/MessageQueue(942): at android.os.MessageQueue.enqueueMessage(MessageQueue.java:179) 
07-11 14:16:34.444: W/MessageQueue(942): at android.os.Handler.sendMessageAtTime(Handler.java:457) 
07-11 14:16:34.444: W/MessageQueue(942): at   android.os.Handler.sendMessageDelayed(Handler.java:430) 
07-11 14:16:34.444: W/MessageQueue(942): at android.os.Handler.sendMessage(Handler.java:367) 
07-11 14:16:34.444: W/MessageQueue(942): at android.os.Message.sendToTarget(Message.java:348) 
07-11 14:16:34.444: W/MessageQueue(942): at android.os.AsyncTask$3.done(AsyncTask.java:214) 
07-11 14:16:34.444: W/MessageQueue(942): at java.util.concurrent.FutureTask$Sync.innerSet(FutureTask.java:252) 
07-11 14:16:34.444: W/MessageQueue(942): at java.util.concurrent.FutureTask.set(FutureTask.java:112) 
07-11 14:16:34.444: W/MessageQueue(942): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:310) 
07-11 14:16:34.444: W/MessageQueue(942): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
07-11 14:16:34.444: W/MessageQueue(942): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) 
07-11 14:16:34.444: W/MessageQueue(942): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) 
07-11 14:16:34.444: W/MessageQueue(942): at java.lang.Thread.run(Thread.java:1096) 

有人建議我在哪裏,我在額外錯誤感謝....

+0

您是否嘗試過其他的地方做,然後在的AsyncTask, – AAnkit 2012-07-11 09:03:36

+0

不,我只是想那...... – sandee 2012-07-11 09:07:02

+0

但是,什麼是問題,爲什麼它不工作? – sandee 2012-07-11 09:08:03

回答

1

我建議你設置報警經理在一個簡單的方法,

您不需要爲此目的使用asyncTask。 以下錯誤表示線程已經死機,並且有些事情嘗試向死線程中的處理程序發送消息。

07-11 14:16:34.444: W/MessageQueue(942): Handler{44f6ad40} sending message to a Handler on a dead thread 
      07-11 14:16:34.444: W/MessageQueue(942): java.lang.RuntimeException: Handler{44f6ad40} sending message to a Handler on a dead thread 

以下鏈接可以給你一些提示在這個問題上

link

+0

謝謝我正在嘗試它,但brodcast不是在設定的時間執行。 – sandee 2012-07-11 09:33:49

+0

http://stackoverflow.com/a/11366469/519718看到這個鏈接 – AAnkit 2012-07-11 09:36:12

+0

謝謝很多現在它的工作.. – sandee 2012-07-11 09:40:44