OnKeyListener处理两次按下

时间:2014-11-17 08:24:27

标签: android keylistener android-audiomanager

有MainActivity导致警报对话框,当打开警报对话框不起作用时更改音量,放入OnKeyListener,现在按下音量按钮时处理两次,与MainActivity进程中的监听器点击正确的,帮助我理解

MainActivity中的代码

    public void ShowTrack(final String smetaout,String stitleout){

    if (MediaService.isPlayingDubstep || MediaService.isPlayingLiquid || MediaService.isPlayingMain == true) {
        allbtnStop();
    }
    alert = new AlertDialog.Builder(this).create();
    alert.setTitle(stitleout);
    alert.setCanceledOnTouchOutside(false);

    LayoutInflater li = getLayoutInflater();
    View v = li.inflate(R.layout.track_view, null);
    Log.d("My Webview", "ShowTrack");
    final WebView wv = (WebView) v.findViewById(R.id.trackView);
    wv.setVisibility(View.GONE);
    playTrack = (Button) v.findViewById(R.id.playTrack);
    progress = (ProgressBar) v.findViewById(R.id.progress);
    playTrack.setVisibility(View.INVISIBLE);
    closeTrack = (Button) v.findViewById(R.id.close);
    colors_sel = mSettings.getString("colors", "1");
    if (colors_sel.equals("0")) {
        playTrack.setBackgroundResource(R.drawable.stop_button_red);

    }
    else if (colors_sel.equals("1")) {
        playTrack.setBackgroundResource(R.drawable.stop_button_green);

    }
    else if (colors_sel.equals("2")) {
        playTrack.setBackgroundResource(R.drawable.stop_button_blue);

    }

    progress.setVisibility(View.VISIBLE);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.loadUrl(smetaout);
    wv.setWebViewClient(new WebViewClient() {
    @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (!loadingFinished) {
                redirect = true;
            }
            loadingFinished = false;
            view.loadUrl(url);
            Log.d("shouldOverrideUrlLoading", url);

            MediaService.releaseMP();

            return true;
        }

        @Override
            public void onPageFinished(WebView view, String url) {
                if(!redirect){
                    loadingFinished = true;
                    Log.d("onPageFinished", "loadingFinished = true;");
                }

                if(loadingFinished && !redirect){
                    //HIDE LOADING IT HAS FINISHED
                    if (loadtrack == true){
                        myUri = Uri.parse(url);
                        context = getApplicationContext();
                        at = (ActionTask) new ActionTask().execute();           




                    playTrack.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {
                            colors_sel = mSettings.getString("colors", "1");
                            if (MediaService.isPlayingTrack) {
                                MediaService.pauseT();
                                if (colors_sel.equals("0")) {
                                    playTrack.setBackgroundResource(R.drawable.play_button_red);
                                } else if (colors_sel.equals("1")) {
                                    playTrack.setBackgroundResource(R.drawable.play_button_green);
                                } else if (colors_sel.equals("2")) {
                                    playTrack.setBackgroundResource(R.drawable.play_button_blue);
                                }
                            }else{
                                visibleProgressTrack();
                                MediaService.startT();
                                if (colors_sel.equals("0")) {
                                    playTrack.setBackgroundResource(R.drawable.stop_button_red);
                                } else if (colors_sel.equals("1")) {
                                    playTrack.setBackgroundResource(R.drawable.stop_button_green);
                                } else if (colors_sel.equals("2")) {
                                    playTrack.setBackgroundResource(R.drawable.stop_button_blue);
                                }
                            }

                                Log.i("ShowTrack","isPlayingTrack="+MediaService.isPlayingTrack);
                            }
                        }); 
                    }
                    loadtrack = !loadtrack;
                } else{
                    redirect = false; 
                }
        }
    });




    alert.setView(v);

    closeTrack.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            MediaService.isPlayingTrack = false;
            if (MediaService.mediaPlayer != null) {
                if (at != null){
                at.cancel(false);
                }
                MediaService.stopT();
                MediaService.releaseMP();
            }
            alert.dismiss();
        }
    });
    alert.setOnKeyListener(new DialogInterface.OnKeyListener() {

        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

            if (keyCode == KeyEvent.KEYCODE_BACK) {
                MediaService.isPlayingTrack = false;
                if (MediaService.mediaPlayer != null) {
                    if (at != null){
                    at.cancel(false);
                    }
                    MediaService.stopT();
                    Log.i("Key","Back");
                    MediaService.releaseMP();

                }
                dialog.dismiss();
                return true;
            } else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP){ 
                int index = seekbar.getProgress();
                seekbar.setProgress(index + 1); 
                Log.i("onKey","seekbar="+index);
                return true; 
            } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
                int index = seekbar.getProgress(); 
                seekbar.setProgress(index - 1); 
                Log.i("onKey","seekbar="+index);
                return true; 
            }
            return onKeyDown(keyCode, event);

        }
    });
    alert.show();
}

public static void initBar(SeekBar bar, final int stream) {
      bar.setMax(am.getStreamMaxVolume(stream));
      bar.setProgress(am.getStreamVolume(stream));

      bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        public void onProgressChanged(SeekBar bar, int progress,
                                      boolean fromUser) {
          am.setStreamVolume(stream, progress, AudioManager.FLAG_PLAY_SOUND);
          am.setStreamVolume(stream, progress, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
        }

        public void onStartTrackingTouch(SeekBar bar) {
          // no-op
        }

        public void onStopTrackingTouch(SeekBar bar) {
          // no-op
        }


      });

    }

public boolean onKeyDown(int keyCode, KeyEvent event) {
    //ProgressBar seekbar = (SeekBar)findViewById(R.id.seekBar1);
    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP){ 
        int index = seekbar.getProgress(); 
        seekbar.setProgress(index + 1); 
        Log.i("onKeyDown","seekbar="+index);
        return true; 
    } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
        int index = seekbar.getProgress(); 
        seekbar.setProgress(index - 1); 
        Log.i("onKeyDown","seekbar="+index);
        return true; 
    } else if (keyCode == KeyEvent.KEYCODE_BACK){
        if (exit==false)
            Toast.makeText(this, R.string.exit, Toast.LENGTH_SHORT).show();
        Handler handler = new Handler(); 
        handler.postDelayed(new Runnable() { 
             public void run() { 
                  exit = false;
             } 
        }, 3000);
        if (exit) {
            stopService();
            unregisterReceiver(myReceiver);
            CallReceiver.state = false;
            MainActivity.this.finish();
        }
        exit = true;
        return true;

    }
    return super.onKeyDown(keyCode, event); 
}

1 个答案:

答案 0 :(得分:1)

onKeyListener检测到按键按下并按下键。这两个事件在onKeyListener中触发,并导致事件触发两次。

仅检查按键事件...似乎您没有在onKey方法中检测到此类事件。你的onKey方法中的这样的东西应该可以防止双击

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (event.getAction()==KeyEvent.ACTION_UP) {
         // your logic now goes in here
    }
    .....
}