如何在android中添加进度条或类似的进度条

时间:2014-04-14 05:55:25

标签: android bluetooth-lowenergy

在启动画面之后我有扫描活动,扫描后我将获得ble设备列表。当点击特定项目时,它会移动到设备控制活动。当移动到它时,我想添加进度条/显示< strong>正在连接...... 以下是设备控制活动中的代码。

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

        }

    }
};

以下是我的connected_alert()方法。

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

}

但在执行此操作时我得到了错误。无法在未调用Looper.prepare()的线程内创建处理程序

1 个答案:

答案 0 :(得分:0)

试试这个

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

    }

  }