Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 停止处理程序线程按钮_Java_Android_Android Handler_Java Threads - Fatal编程技术网

Java 停止处理程序线程按钮

Java 停止处理程序线程按钮,java,android,android-handler,java-threads,Java,Android,Android Handler,Java Threads,每隔5秒,我想在Android应用程序中向web应用程序显示当前经纬度。如果用户登录到应用程序中,我们将通过放置按钮来使用用户日志。如果用户点击“注销”按钮,我需要通过另一个按钮(登录)停止同一活动页面上当前运行的处理程序线程 我已经做了一切,但在停止线程时,我的应用程序崩溃了 如果用户点击登录按钮,则每5秒自动发送一次数据 private void techlocation() { handler = new Handler (); locationManager = (Lo

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

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

使用
布尔值以这种方式声明
可运行

//如果处理程序应停止,则应将其设置为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


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

使用
布尔值以这种方式声明
可运行

//如果处理程序应停止,则应将其设置为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


在这里发布日志猫也说明在按b3之前注销时,您可能会遇到崩溃。这是因为处理程序尚未初始化。要处理它,只需在删除它之前检查处理程序是否为nullcallbacks@p.mathew13你完全正确。当我尝试单击按钮时,我的应用程序崩溃。谢谢兄弟。就在几天前,我收到一个错误@p.mathew13我需要初始化按钮RightPost logcat中的内部处理程序,在按下b3之前注销时,您可能会崩溃。这是因为处理程序尚未初始化。要处理它,只需在删除它之前检查处理程序是否为nullcallbacks@p.mathew13你完全正确。当我尝试单击按钮时,我的应用程序崩溃。谢谢兄弟。就在几天前,我遇到了一个错误@p.mathew13我需要初始化按钮右侧的内部处理程序
handler.removeCallbacks(runnable);