2012-05-08 177 views
0

我在android應用程序中工作,我想禁用我的android應用程序中的後退按鈕。我將它重定向到一個url,並且我想在那裏禁用返回按鈕。請幫幫我。禁用後退按鈕

這裏是毫安代碼

public class PrepareRequestTokenActivity extends Activity { 

    final String TAG = getClass().getName(); 

    private OAuthConsumer consumer; 
    private OAuthProvider provider; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     try { 
      setContentView(R.layout.twitter); 
      ProgressDialog dialog = ProgressDialog.show(PrepareRequestTokenActivity.this, "", 
        "Loading. Please wait...", true); 
      this.consumer = new CommonsHttpOAuthConsumer(
        Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET); 
      this.provider = new CommonsHttpOAuthProvider(Constants.REQUEST_URL, 
        Constants.ACCESS_URL, Constants.AUTHORIZE_URL); 
     } catch (Exception e) { 
      Log.e(TAG, "Error creating consumer/provider", e); 
     } 

     Log.i(TAG, "Starting task to retrieve request token."); 
     new OAuthRequestTokenTask(this, consumer, provider).execute(); 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if ((keyCode == KeyEvent.KEYCODE_BACK)) { 
      Log.d(this.getClass().getName(), "back button pressed"); 
     } 
     return super.onKeyDown(keyCode, event); 
    } 


    /** 
    * Called when the OAuthRequestTokenTask finishes (user has authorized the 
    * request token). The callback URL will be intercepted here. 
    */ 
    @Override 
    public void onNewIntent(Intent intent) { 
     super.onNewIntent(intent); 
     SharedPreferences prefs = PreferenceManager 
       .getDefaultSharedPreferences(this); 

     final Uri uri = intent.getData(); 
     if (uri != null 
       && uri.getScheme().equals(Constants.OAUTH_CALLBACK_SCHEME)) { 
      Log.i(TAG, "Callback received : " + uri); 
      Log.i(TAG, "Retrieving Access Token"); 
      new RetrieveAccessTokenTask(this, consumer, provider, prefs) 
        .execute(uri); 


      setResult(Activity.RESULT_OK); 

      finish(); 
     } 
    } 




    public class RetrieveAccessTokenTask extends AsyncTask<Uri, Void, Void> { 

     private Context context; 
     private OAuthProvider provider; 
     private OAuthConsumer consumer; 
     private SharedPreferences prefs; 

     public RetrieveAccessTokenTask(Context context, OAuthConsumer consumer, 
       OAuthProvider provider, SharedPreferences prefs) { 
      this.context = context; 
      this.consumer = consumer; 
      this.provider = provider; 
      this.prefs = prefs; 
     } 

     /** 
     * Retrieve the oauth_verifier, and store the oauth and 
     * oauth_token_secret for future API calls. 
     */ 
     @Override 
     protected Void doInBackground(Uri... params) { 
      final Uri uri = params[0]; 
      final String oauth_verifier = uri 
        .getQueryParameter(OAuth.OAUTH_VERIFIER); 

      try { 
       provider.retrieveAccessToken(consumer, oauth_verifier); 

       final Editor edit = prefs.edit(); 
       edit.putString(OAuth.OAUTH_TOKEN, consumer.getToken()); 
       edit.putString(OAuth.OAUTH_TOKEN_SECRET, 
         consumer.getTokenSecret()); 
       edit.commit(); 

       String token = prefs.getString(OAuth.OAUTH_TOKEN, ""); 
       String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, ""); 

       consumer.setTokenWithSecret(token, secret);        

       Log.i(TAG, "OAuth - Access Token Retrieved"); 

      } catch (Exception e) { 
       Log.e(TAG, "OAuth - Access Token Retrieval Error", e); 
      } 

      return null; 
     } 

    } 
+2

參見[http://stackoverflow.com/questions/5673358/how-to-disable-back-button-in-android][1] – vikky

回答

0

@Override 公共無效onBackPressed(){// 沒做什麼。 }

2

覆蓋的onBackPressed方法

@Override 
public void onBackPressed() { 
    return; 
} 

需要API級別5或更高

1

避免調用父行爲,即在你的重寫方法刪除super.onBackPressed()

@Override 
public void onBackPressed() { 
    return; 
}