0

我试图在单击按钮时显示进度条。当我测试应用程序时,它会强制关闭/停止。在添加进度条代码之前,我的应用程序运行良好。

如果这很重要,我也正在使用切换按钮,并且我希望旋转进度条不是对话框。

有什么帮助吗?

这是我的代码:

@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);
            }
        }
    });
    }
4

3 回答 3

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

于 2012-11-05T05:54:11.720 回答
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();
    }
});
于 2012-11-05T05:41:07.517 回答
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();
于 2012-11-05T06:03:34.463 回答