0

我正在从 broadcastReciever 调用服务类我的类是:-

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)

任何人都建议我在哪里错了,谢谢......

4

1 回答 1

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

下面的链接可以给你一些关于这个问题的提示

关联

于 2012-07-11T09:25:15.677 回答