我正在尝试在单击按钮时显示进度条。当我测试应用程序时,它强制关闭/停止。我的应用程序在添加进度条代码之前正常工作。
我也在使用切换按钮,如果这很重要,我希望旋转进度条不是对话框。
任何帮助?
这里是我的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonFlashlight);
Context context = this;
PackageManager pm = context.getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Log.e("err", "Device has no camera!");
return;
}
camera = Camera.open();
final Parameters p = camera.getParameters();
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if (isLighOn) {
progressBar1.setVisibility(View.INVISIBLE);
final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.drawable.click);
mp1.start();
Log.i("info", "torch is turn off!");
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLighOn = false;
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 1.0f;
getWindow().setAttributes(params);
} else {
progressBar1.setVisibility(View.VISIBLE);
final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.drawable.click);
mp1.start();
Log.i("info", "torch is turn on!");
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isLighOn = true;
WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 0.05f;
getWindow().setAttributes(params);
}
}
});
}
答案 0 :(得分:0)
尝试这种方式,我不确定它会对你有所帮助
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final ProgressDialog progress = ProgressDialog.show(THENAMEOFYOURACTIVITYCLASS.this,
ProgressTitle, ProgressMessage, true, false);
new Thread(new Runnable() {
public void run() {
loadFeed();
progress.cancel();
}
}).start();
}
});
Be careful about what loadFeed(statutory); do. Because now is working inside a THREAD and Threads con not modify UI, If this is the case then you should do something like this:
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final ProgressDialog progress = ProgressDialog.show(THENAMEOFYOURACTIVITYCLASS.this,
ProgressTitle, ProgressMessage, true, false);
new Thread(new Runnable() {
public void run() {
loadFeed(); //just load data and prepare the model
runOnUiThread(new Runnable() {
@Override public void run() {
//here you can modify UI here
//modifiying UI element happen here and at the end you cancel the progress dialog
progress.cancel();
}
}); // runOnUIthread
}
}).start();
}
});
答案 1 :(得分:0)
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(R.style.NewDialog);
progressDialog.setMessage("Loading...");
将以下内容粘贴到styles.xml中 新对话:
<style name="NewDialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowTitleStyle">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:background">@android:color/transparent</item>
</style>
如果你想表明你可以使用progressDialog.show()并隐藏你可以使用progressDialog.dismiss()。
答案 2 :(得分:0)
尝试以下代码...
handler1 = new Handler()
{
@Override
public void handleMessage(Message msg)
{
switch(msg.what)
{
case 1:
static_class.digi_pd = ProgressDialog.show(Create_Digitizing_Job.this, "Loading...", "Please Wait..", true,false);
static_class.digi_pd.setCancelable(false);
break;
case 2:
static_class.digi_pd.dismiss();
break;
}
}
};
thread1 = new Thread()
{
@Override
public void run()
{
try
{
handler1.sendEmptyMessage(1);
// write your code here....
handler1.sendEmptyMessage(3);
handler1.sendEmptyMessage(2);
}
catch (Exception e)
{
Log.w("thread error...",""+e);
//e.printStackTrace();
}
}
};
thread1.start();