0

友達、

アプリケーション内に AlarmManager をセットアップしました。AlarmManager は、バックグラウンド サービスを xx ごとに開始するようにスケジュールされています。ここでは 1 分です。しばらくの間、非常にうまく機能しています。しかし、頻繁にエラーが発生します: thead already started/scheduled. デストラクタを正しく使用しないかもしれないと感じています。どうぞよろしくお願いいたします。

AlarmManager を開始するアクティビティの私のコードを次に示します。

    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

私は自分の問題の解決策を見つけたと思います。まず、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;
        }

とにかくサポートをありがとう!!! 乾杯

于 2011-11-10T13:55:52.710 に答える