2012-03-27 20 views
6

Sto facendo un gioco e ho una pagina delle opzioni che attiva o disattiva la musica. Voglio ignorare il BackButton in modo che torna alla home page, capirete perché quando si vede il mio codice:Android: Modifica delle azioni del pulsante Indietro

public class OptionsActivity extends Activity { 

private boolean isMyServiceRunning(String serviceCanonicalClassName) { 
    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); 
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { 
     if (serviceCanonicalClassName.equals(service.service.getClassName())) { 
      return true; 
     } 
    } 
    return false; 
} 

Intent i; // Handles MyMusicService.java 


protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.options); 


    final TextView tSound = (TextView) findViewById(R.id.textView2); 

    final Button saveBtn = (Button) findViewById(R.id.optSaveBtn); 
    final Button tblBtn = (Button) findViewById(R.id.tableBtn); 

    i=new Intent(this, MyMusicService.class); 

    final ToggleButton soundOption = (ToggleButton) findViewById(R.id.soundPref); 


    boolean musicPlays = isMyServiceRunning(MyMusicService.class.getCanonicalName()); 


    soundOption.setChecked(musicPlays); 


    if(musicPlays==true){ 

     tSound.setText("On"); 
    } 

    if(musicPlays==false) { 

     tSound.setText("Off"); 
    } 


    soundOption.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      // Perform action on clicks to control sound being on and off. 
      if(soundOption.isChecked()) { 

       Toast.makeText(OptionsActivity.this, "Music on.", Toast.LENGTH_SHORT).show(); 
       startService(i); 
       Intent i = new Intent(OptionsActivity.this, OptionsActivity.class); 
       startActivity(i); 

      } 

      else { 

       if(stopService(i)==true){ 

        soundOption.setChecked(false); 
        stopService(i); 
        Toast.makeText(OptionsActivity.this, "Music off.", Toast.LENGTH_SHORT).show(); 
        Intent i = new Intent(OptionsActivity.this, OptionsActivity.class); 
        startActivity(i); 

       } 
      } 
     } 

    }); 



    tblBtn.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      Intent tblView = new Intent(OptionsActivity.this, SQLView.class); 
      startActivity(tblView); 

     } 
    }); 



    saveBtn.setOnClickListener(new View.OnClickListener(){ 

     public void onClick(View v) { 



      switch (v.getId()){ 

      case R.id.optSaveBtn: //Determine what will happen when the user presses the "Submit button". 
       boolean optionsWork = true; 
       try{ 

        String sound = tSound.getText().toString(); 

        optionsDB entry = new optionsDB(OptionsActivity.this); //Creating a new instance of MasterMind game 
        entry.open(); 
        entry.createOptionEntry(sound); //Passing both strings 
        entry.close(); 

       }catch (Exception e){ //Creating an error message if for some reason the app cannot transfer data to the Database. 

        Toast.makeText(OptionsActivity.this, "Error", Toast.LENGTH_SHORT).show(); 
       } 

       finally { //Creating an AlertDialog box when the user presses the Submit button. 

        if (optionsWork){ 

         Toast.makeText(OptionsActivity.this, "Settings Saved", Toast.LENGTH_SHORT).show(); 

        } 

       } 

       break; 

      } 
     } 
    }); 
} 

}

Qualsiasi aiuto sarebbe fantastico grazie.

Aggiornamento: Aggiunto questo, ancora non funziona:

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
     // do something on back. 
     return true; 
    } 
    Intent i = new Intent(OptionsActivity.this, MainActivity.class); 
    startActivity(i); 
    return super.onKeyDown(keyCode, event); 
} 

risposta

6

Override onKeyDown() di attività, qui è possibile gestire vari tasti o possibile ignorare onBackPresses() troppo

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
     // do something on back. 
     return true; 
    } 

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

Grazie per il tuo commento, l'ho provato, ma continuo a ricevere Force Close non appena arrivo alla classe Options. Qualche idea? –

+1

traccia il tuo Logcat e vedi dove sta arrivando esattamente l'errore? –

+0

Ah ok, beh non si blocca più ... Non fa niente ora haha. Potete verificare la mia risposta rivista per favore? Problema –

4
@Override 
public void onBackPressed() { 
    ............. 
} 
8

Usa la funzione onBackPressed() per sovrascrivere l'azione del pulsante Indietro. Ecco alcuni esempi di codice:

 @Override 
     public void onBackPressed() 
      { 
      Intent intent = new Intent(this,ABC.class); 
      startActivity(intent); 
      } 

Scrivi l'azione desiderata all'interno della funzione onBackPressed().

Problemi correlati