Home > Blockchain >  Is there any chance to simply call method in new thread?
Is there any chance to simply call method in new thread?

Time:02-16

my problem is that when I run the android app, it takes a minute for this method to run if the printer is offline. I would like to run it on another thread in the background and the original thread could run uninterrupted. Thanks for any advice, happy codding!

public class MainActivity extends WebView {
public PrinterManager printerManager;
public final int REQUEST_COARSE_LOCATION_PERMISSIONS = 52;
private JSPlugin plugin;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(mBluetoothReceiver, filter);
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case REQUEST_COARSE_LOCATION_PERMISSIONS: {
            if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //printerManager.connectDefaultPrinter(0);
                //printerManager.connectDefaultPrinter(1);

                printerManager.connectAll(); <----- This method call i want to do in new thread
            } else {
                Toast.makeText(this,
                        "NO Bluetooth RIGHTS",
                        Toast.LENGTH_LONG).show();
            }
            return;
        }
    }
}

CodePudding user response:

You could simply create a Thread object and put your method into it.
Here the code that should work for your case:

public class MainActivity extends WebView {
    public PrinterManager printerManager;
    public final int REQUEST_COARSE_LOCATION_PERMISSIONS = 52;
    private JSPlugin plugin;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
        registerReceiver(mBluetoothReceiver, filter);
    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case REQUEST_COARSE_LOCATION_PERMISSIONS: {
                if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    ConnectingThread connectingThread = new ConnectingThread();
                    connectingThread.start();
                } else {
                    Toast.makeText(this, "NO Bluetooth RIGHTS", Toast.LENGTH_LONG).show();
                }
                return;
            }
        }
    }

    private class ConnectingThread extends Thread {
    
        public ConnectingThread() {
        }
    
        public void run() {
            // You can set a low priority, if you want, using:
            //Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
            printerManager.connectAll();
        }
    }
}

Basing on your management needs, you can decide to place the definition of your connectingThread in a different position.

  • Related