1

私のアプリでは、10秒ごとにユーザーの位置を取得するタイマータスクを実装するサービスがあります。

私が行き詰まっているのは、このサービスとタイマーを1〜2分後に停止したいということです。

私はその論理を後回しにすることができません。私を助けてください。

public class TimeService extends Service {
// constant
public static final long NOTIFY_INTERVAL = 20 * 1000; // 10 seconds

// run on another Thread to avoid crash
private Handler mHandler = new Handler();
// timer handling
private Timer mTimer = null;
int i=0;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    // cancel if already existed
    if(mTimer != null) {
        mTimer.cancel();
    } else {
        // recreate new
        mTimer = new Timer();
    }
    // schedule task
    mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
}

class TimeDisplayTimerTask extends TimerTask {

    @Override
    public void run() {
        // run on another thread
        mHandler.post(new Runnable() {
             @Override
            public void run() {
                // display toast
                Toast.makeText(getApplicationContext(), getDateTime(),
                        Toast.LENGTH_SHORT).show();
                new LongOperation().execute("");
            }
         });
    }

    private String getDateTime() {
        // get date time in custom format
        SimpleDateFormat sdf = new SimpleDateFormat("[yyyy/MM/dd - HH:mm:ss]");
        return sdf.format(new Date());
    }
 }

private class LongOperation extends AsyncTask<String, Void, String> {
            @Override
    protected String doInBackground(String... params) {
        System.out.println("bgnotification Long operation doinbackground called----> "); 
        return "";
    }      

    @Override
    protected void onPostExecute(String result) {
        System.out.println("bgnotification Long operation post execute called----> ");
        if(i<3){ 
            GPSTracker mGPS = new GPSTracker(getApplicationContext());
         onLocationChanged(mGPS);i++;
         Toast.makeText(getApplicationContext(), "HEllo Post execute called",
    }
    @Override
    protected void onPreExecute() {
        System.out.println("bgnotification Long operation pre execute called----> ");
    }
    @Override
    protected void onProgressUpdate(Void... values) {
    }
}


public void onLocationChanged(GPSTracker track) {
    // Getting latitude
    double latitude = track.getLatitude();
    // Getting longitude
    double longitude = track.getLongitude();
     System.out.println( latitude);
     System.out.println(     longitude);
     Geocoder geocoder = new Geocoder(this, Locale.getDefault());
        try
     {
         List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
         Log.e("Addresses","-->"+addresses);              
     }
     catch (IOException e)
     {
         e.printStackTrace();

     }
   }
}
4

3 に答える 3

3

@Wishy サービスとタイマー タスクを特定の時間に停止するには、1 回実行する他のタイマー タスクを作成する必要があり、実行時間は指定したものになります。run メソッドでは、mTimer.purge(); と書く必要があります。mTimer.cancel() および stopService(newIntent(class.this,yourservice.class));

于 2013-08-07T05:47:49.860 に答える
0
  1. サービス クラスの onDestroy() メソッドを次のようにオーバーライドします。

    @Override public void onDestroy() { super.onDestroy(); if (mTimer != null) { mTimer.cancel(); } }

  2. 次のように、サービスを停止したい場所で stopService() を呼び出します。

stopService(serviceIntent);

それで全部です。

于 2016-03-23T06:43:33.850 に答える