错误线程已经启动/计划

时间:2011-11-04 07:30:01

标签: android android-service android-alarms

我在我的应用程序中设置了一个AlarmManager。 AlarmManager计划每xx启动一次后台服务,此处为1分钟。它的工作一段时间很好。但是我很快就得到了一个错误:thead已经开始/预定了。 我觉得我可能不会使用析构函数。 非常感谢您的支持。

继承我启动AlarmManager的Activity的代码

    PendingIntent pi;
    AlarmManager mgr;
    mgr=(AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);

    Intent i=new Intent(DataCollectionActivity.this, HUJIDataCollectionService.class);

     pi = PendingIntent.getService(DataCollectionActivity.this, 0, i, 0);
         ........  
    if (viewId == R.id.b_startService) {


        mgr.cancel(pi);

        mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() , 1* 60* 1000, pi);}

            ........
    if (viewId == R.id.b_stopService) {


        mgr.cancel(pi);}

并且是我服务的重要代码:

                      private Runnable LocationUpdateTimerTask = new Runnable() {
    public void run() {
        Log.i(ctx.getString(R.string.app_name),
                "HUJIDataCollectionService, 1 LocationUpdateTimerTask, start");
        setuplistenerandrequestupdates();
        mHandler.removeCallbacks(LocationUpdateTimerTask);

    }
};


            private Runnable SendDataStopLocationUpdatesTimerTask = new Runnable() {
    public void run() {

        sendDataToServer();



        mHandler.removeCallbacks(SendDataStopLocationUpdatesTimerTask);

        ServiceIntervalTimerTask.cancel();
        Intent service = new Intent(ctx, HUJIDataCollectionService.class);
        stopService(service);

    }
};


            private TimerTask ServiceIntervalTimerTask = new TimerTask() {
    @Override
    public void run() {


        // remove old timer updates
        mHandler.removeCallbacks(LocationUpdateTimerTask);
        mHandler.removeCallbacks(SendDataStopLocationUpdatesTimerTask);
        // Start TimerTasks delayed
        mHandler.postDelayed(LocationUpdateTimerTask, 1000);
        mHandler.postDelayed(SendDataStopLocationUpdatesTimerTask,
                conf_LocationUpdatePeriodInSec * 1000);


    }

};


            @Override
public void onDestroy() {
    super.onDestroy();



    startDataCollectionServiceIntervallTimer.cancel();

    startDataCollectionServiceIntervallTimer = null;
    // Remove all kinds of updates
    mHandler.removeCallbacks(LocationUpdateTimerTask);
    mHandler.removeCallbacks(SendDataStopLocationUpdatesTimerTask);


}


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



    startDataCollectionServiceIntervallTimer = new Timer(
            "HUJIDataCollectionServiceStartTimer");

    startDataCollectionServiceIntervallTimer.schedule(ServiceIntervalTimerTask,
            1000L, conf_sampleTimeInMin * 60 * 1000L);

    mHandler = new Handler();

    return START_STICKY;
}

2 个答案:

答案 0 :(得分:0)

当您启动服务时,即使应用程序被销毁,它也会在后台运行。您何时何地致电您的闹钟管理器?但是,如果你经常打电话给我认为你会有内存泄漏或类似的事情......

答案 1 :(得分:0)

我想我找到了解决问题的方法。 首先,我通过启动Broadcastreceiver绕过了这个问题。但这不是所述问题的答案。

这是一个解决方案:

                 public void pause(){           


        while(true){    

            try {                           // goes through this thread until our thread died
                ourthread.join();        //Blocks the current Thread (Thread.currentThread()) until the receiver finishes its execution and dies.
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   
            break;
        }

感谢您的支持! 干杯