如何添加进度条或看起来像类似之一的android [英] how to add progress bar or look like similar one in android
本文介绍了如何添加进度条或看起来像类似之一的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在启动画面我已经扫描活动BLE devices.when的是扫描后,我将获得名单上的点击具体项目将其移动到设备控制activity.when移动到它,我想添加进度条/像显示< STRONG>连接... ,然后下面是我的code在设备的控制活动。
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));
}
}
};
和下面是我connected_alert()方法。
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();
}
不过,虽然这样做,我得到error.it是无法建立处理程序中的线程并没有所谓的活套。prepare()
推荐答案
试试这个
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();
}
}
这篇关于如何添加进度条或看起来像类似之一的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文