2014-11-17 63 views
0

有MainActivity導致警報對話框,當打開警報對話框不工作時更改音量,將OnKeyListener和現在當您按音量按鈕時處理兩次,與監聽器誰是在MainActivity過程點擊正確,幫助我瞭解OnKeyListener處理按兩次

守則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); 
} 

回答

0

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 
    } 
    ..... 
}