【问题标题】:stopping a handler thread button停止处理程序线程按钮
【发布时间】:2019-06-10 10:13:24
【问题描述】:

每隔 5 秒,我想在 Android 应用程序中获取 Web 应用程序的当前纬度经度。如果用户通过放置按钮登录到我们使用用户日志的应用程序。如果用户点击注销按钮,我需要通过不同的按钮(登录)停止当前在同一活动页面上运行的处理程序线程

我已经做了所有事情,但是在停止线程时我的应用程序崩溃了

如果用户点击登录按钮,它会自动每 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);


}

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

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

【问题讨论】:

  • 在这里也发布 logcat
  • 如果您在按 b3 之前退出,您可能会遇到崩溃。发生这种情况是因为您的处理程序尚未初始化。要处理它,只需在删除其回调之前检查您的处理程序是否为空
  • @p.mathew13 你完全正确。当我尝试单击按钮时,我的应用程序崩溃了。多谢兄弟 。就在几天前,我遇到了一个错误。
  • @p.mathew13 我需要在按钮右侧初始化处理程序

标签: java android android-handler java-threads


【解决方案1】:

要开始可运行并每 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);
        }
    }
};

调用此按钮以在按钮 click() 上启动此 runnable

// start runnable
mHandler.post(runnable);

最后用它来停止可运行

handler.removeCallbacks(runnable);

【讨论】:

    猜你喜欢
    • 2016-10-26
    • 2019-01-26
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多