活动结束得太慢了

时间:2013-08-29 06:20:40

标签: android android-activity activity-finish

在我的活动'A'中,我正在使用计时器和媒体播放器来播放音频。我的应用程序正在设置活动说活动'B',用于选择音频播放模式。我正在使用startActivityForResult()并从活动B发送resultCode。问题是活动B花费了太多时间来完成。因为B中的音频在B完成之前开始。怎么办呢???

我的活动B callig代码是:

Intent intent = new Intent(context, Setting.class);
        startActivityForResult(intent, Cover.SETTING_REQ);

我的活动B有以下代码:

public class Setting extends Activity {
    Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        context = this;
        final RelativeLayout layout = (RelativeLayout) findViewById(R.id.setting_layout);
        Display display = getWindowManager().getDefaultDisplay();
        final int width = display.getWidth();
        final int height = display.getHeight();
        int[] modes = { R.drawable.settingspageiphone, R.drawable.readtome,
                R.drawable.readwithpause, R.drawable.icanreadit };
        Bitmap bm = BitmapLib.prepairBitmap(context, modes[Read.mode], width,
                height);
        layout.setBackgroundDrawable(new BitmapDrawable(bm));
        layout.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                double x = event.getX();
                double y = event.getY();
                double H = layout.getHeight();
                double W = layout.getWidth();
                if (H * 320 > W * 480) {
                    H = W * 480 / 320;
                    y = y - (layout.getHeight() - H) / 2;
                } else {
                    W = H * 320 / 480;
                    x = x - (layout.getWidth() - W) / 2;
                }
                x = x / W * 320;
                y = y / H * 480;
                if (x > 50 && x < 270) {
                    if (y > 150 && y < 190) {
                        // read to me
                        Bitmap bm = BitmapLib.prepairBitmap(context,
                                R.drawable.readtome, width, height);
                        layout.setBackgroundDrawable(new BitmapDrawable(bm));
                        Read.mode=1;
                        setResult(1);
                        finish();
                        return false;
                    }
                    if (y > 230 && y < 270) {
                        // read with pause
                        Bitmap bm = BitmapLib.prepairBitmap(context,
                                R.drawable.readwithpause, width, height);
                        layout.setBackgroundDrawable(new BitmapDrawable(bm));
                        Read.mode=2;
                        setResult(2);
                        finish();
                        return false;
                    }
                    if (y > 320 && y < 360) {
                        // i can read it
                        Bitmap bm = BitmapLib.prepairBitmap(context,
                                R.drawable.icanreadit, width, height);
                        layout.setBackgroundDrawable(new BitmapDrawable(bm));
                        Read.mode=3;
                        setResult(3);
                        finish();
                        return false;
                    }
                    /*if (y > 430 && y < 470) {
                        setResult(-1);
                        finish();
                        return false;
                    }*/
                }
                return false;
            }
        });
    }

    public void cancle(View v)
    {
        setResult(-1);
        finish();
    }
}
活动A中的

OnActivityResult是:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Cover.SETTING_REQ) {
            if (resultCode != -1) {
                Cover.MODE = resultCode;
                update = true;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

它会,因为你在UI线程上进行位图处理,你应该使用不同的线程进行这类工作,阅读Displaying Bitmaps Efficiently