how to add progress bar or look like similar one i

2019-01-29 08:09发布

问题:

after splash screen i have scan activity in that after scan i will get list of ble devices.when on click on particular item it moves to device control activity.when move to it i want to add progress bar/something like displaying connecting... and the following is my code in device control activity.

              private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
            // Show all the supported services and characteristics on the user interface.
              connected_alert();

              check_forservices(mBluetoothLeService.getSupportedGattServices());
        }  else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
            mConnected = false;
            updateConnectionState(R.string.disconnected);
             invalidateOptionsMenu();
             create_alert();
        } else   if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
            mConnected = true;
            updateConnectionState(R.string.connected);
            invalidateOptionsMenu();
        }  else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
            displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));

        }

    }
};

and the following is my connected_alert() method.

                     public void connected_alert(){

             progressDialog = ProgressDialog.show(DeviceControlActivity.this, "", "Connecting...");

                new Thread() {

                @Override
                public void run() {

                try{
                 mBluetoothLeService.connect(mDeviceAddress);

                     sleep(1000);
                     if(true)
                     connection_alert();

                } catch (Exception e) {

                Log.e("tag", e.getMessage());

                }

                // dismiss the progress dialog

                progressDialog.dismiss();

                }

                }.start();

}

but while doing this i got error.it is Can't create handler inside thread that has not called Looper.prepare()

回答1:

try this

public class MainActivity extends Activity {



ProgressDialog barProgressDialog;

Handler updateBarHandler;



@Override

public void onCreate(Bundle savedInstanceState) {



    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);



    updateBarHandler = new Handler();

}



public void launchRingDialog(View view) {

    final ProgressDialog ringProgressDialog = ProgressDialog.show(MainActivity.this, "Please wait ...", "Connecting", true);

    ringProgressDialog.setCancelable(true);

    new Thread(new Runnable() {
        @Override
        public void run() {

            try {

                // Here you should write your time consuming task...

                // Let the progress ring for 10 seconds...

                Thread.sleep(10000);

            } catch (Exception e) {



            }

            ringProgressDialog.dismiss();

        }

    }).start();

}



public void launchBarDialog(View view) {

    barProgressDialog = new ProgressDialog(MainActivity.this);



    barProgressDialog.setTitle("Please Wait..");

    barProgressDialog.setMessage("Connecting ...");

    barProgressDialog.setProgressStyle(barProgressDialog.STYLE_HORIZONTAL);

    barProgressDialog.setProgress(0);

    barProgressDialog.setMax(20);

    barProgressDialog.show();



    new Thread(new Runnable() {

        @Override

        public void run() {

            try {



                // Here you should write your time consuming task...

                while (barProgressDialog.getProgress() <= barProgressDialog.getMax()) {



                    Thread.sleep(2000);



                    updateBarHandler.post(new Runnable() {



                        public void run() {



                            barProgressDialog.incrementProgressBy(2);


                          }


                      });



                    if (barProgressDialog.getProgress() == barProgressDialog.getMax()) {



                        barProgressDialog.dismiss();



                    }

                }

            } catch (Exception e) {

            }

        }

    }).start();

    }

  }