0

朋友们,

我在我的应用程序中设置了一个 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;
}
4

2 回答 2

0

当您启动服务时,即使应用程序被销毁,它也会在后台运行。您在何时何地致电警报管理器???但是,如果您经常调用服务,我认为您会出现内存泄漏或类似情况...

于 2011-11-04T08:41:54.630 回答
0

我想我找到了自己的问题的解决方案。首先,我通过启动广播接收器绕过了这个问题。但这不是所描述问题的答案。

这是一个解决方案:

                 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;
        }

总之谢谢支持!!!干杯

于 2011-11-10T13:55:52.710 回答