简体   繁体   English

Android按钮onClick()速度慢

[英]Android button onClick() is slow

that takes screenshot from the device screen. 从设备屏幕获取屏幕截图。 It take several seconds before and action will be perfomed and toast message with animation will appear. 这将需要几秒钟的时间,然后才能执行操作,并显示带有动画的吐司消息。 Here is my code: 这是我的代码:

    public void onClick(View v) {
            v.startAnimation(animAlpha);
            try {
                Toast.makeText(GlobalTouchService.this, "Start process", 5000).show();
                Process sh = Runtime.getRuntime().exec("su", null,null);
                OutputStream os = sh.getOutputStream();
                os.write(("/system/bin/screencap -p " + "/sdcard/img.png").getBytes("ASCII"));
                os.flush();
                os.close();
                sh.waitFor();
                Toast.makeText(GlobalTouchService.this, "Screenshot captured", 5000).show();
            } catch (IOException io){
                io.printStackTrace();
            } catch (InterruptedException ie){
                ie.printStackTrace();
            }

I can't use bitmaps here. 我不能在这里使用位图。 I don't care about slow action, but I want to display something after click immediately (because I want to know that button is clicked). 我不在乎动作缓慢,但是我想在单击后立即显示某些内容(因为我想知道该按钮已被单击)。 Any suggestions? 有什么建议么?

You can do a AsyncTask when doing lots of work. 您可以在进行大量工作时执行AsyncTask

 /// your button click event
 public void onClick(View v) {
   new TakePrintScreen ().execute();
  }


 private class TakePrintScreen extends AsyncTask<Void, String, String> {
    private ProgressDialog progress;

    @Override
    protected void onPreExecute() {
        progress = ProgressDialog.show(this, "Information",
                "Please Wait.. ", true);
    }


    @Override
    protected String doInBackground(Void... params) {
        try {
           v.startAnimation(animAlpha);           
         Process sh = Runtime.getRuntime().exec("su", null,null);
        OutputStream os = sh.getOutputStream();
        os.write(("/system/bin/screencap -p " + "/sdcard/img.png").getBytes("ASCII"));
        os.flush();
        os.close();
        sh.waitFor();
        } catch (Exception e) {
            Log.e(Tag, e.getMessage().toString());
        }
        return "";
    }

    @Override
    protected void onPostExecute(String result) {
        progress.dismiss();
    }
}

You can optimize your code and display the Toast message out of try catch block. 您可以优化代码,并在try catch块之外显示Toast消息。

 public void onClick(View v) {
            // put toast here
            Toast.makeText(GlobalTouchService.this, "Start process", 5000).show();
            v.startAnimation(animAlpha);
            try {

                Process sh = Runtime.getRuntime().exec("su", null,null);
                OutputStream os = sh.getOutputStream();
                os.write(("/system/bin/screencap -p " + "/sdcard/img.png").getBytes("ASCII"));
                os.flush();
                os.close();
                sh.waitFor();
                Toast.makeText(GlobalTouchService.this, "Screenshot captured", 5000).show();
            } catch (IOException io){
                io.printStackTrace();
            } catch (InterruptedException ie){
                ie.printStackTrace();
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM