停止处理程序线程按钮

stopping a handler thread button

每 5 秒我想在 Android 应用程序中将当前纬度经度发送到 Web 应用程序。如果用户登录到我们通过放置按钮使用用户日志的应用程序。如果用户点击注销按钮,我需要通过不同的按钮(登录)

在同一 activity 页面上停止处理程序线程当前 运行

我已完成所有操作,但在停止线程时我的应用程序崩溃了

如果用户点击登录按钮,它会每 5 秒自动发送一次数据

private void techlocation() {
    handler = new Handler ();

    locationManager = (LocationManager) User_deatils .this.getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE); // Choose your accuracy requirement.
    locationManager.getBestProvider(criteria, true);
    if (ActivityCompat.checkSelfPermission(User_deatils.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(User_deatils.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }

    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 8000, 10, (LocationListener) User_deatils .this);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 8000, 10, (LocationListener) User_deatils .this);

    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);


    if (location != null) {
        lat = location.getLatitude();
        lon = location.getLongitude();
    }
    handler.postDelayed(runLocation, 5000);


}

这是运行nable线程向服务器端发送数据

public Runnable runLocation = new Runnable() {
        @Override
        public void run() {
            latitude = String.valueOf(lat);
            longitude = String.valueOf(lon);
            arrLat.add(latitude);
            arrLng.add(longitude);
 User_deatils.this.handler.postDelayed(User_deatils.this.runLocation, 50000);
            handler.removeCallbacksAndMessages(this);
            Log.e("msg", "new" + handler);
    }
}

登录按钮

b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               .// SaveButtonState("b3");
                  techlocation(); // calling handler threads

            }
        });

注销按钮

b4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            handler.removeCallbacksAndMessages(runLocation);
  }
        });

要启动 runnable 并每 5 秒执行一次工作,请执行此操作。

boolean 这样声明 runnable

// 如果处理程序应该停止,则应设置为 true 的标志

 boolean mStopHandler = false;
private Handler mHandler;

//initialize runnable in oncreate()
mHandler=new Handler();

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        // do your stuff here 

        if (!mStopHandler) {
            mHandler.postDelayed(this, 5000);
        }
    }
};

Call this to start this runnable on button click()

// start runnable
mHandler.post(runnable);

And finally use this to stop runnable

handler.removeCallbacks(runnable);