2016-02-12 49 views
0

在qr掃描器閱讀器中,點擊一個按鈕後,它將打開相機,它將掃描結果。然後它會使用onactivityresult方法顯示結果。 如何從onActivityResult方法得到結果,並在sharedpreferences中使用它?下面是我的代碼:如何從共享首選項的onActivityResult方法得到結果

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); 
    if (result != null) { 
     String scanContent = result.getContents(); 
     customerSno.setText(" " + scanContent); 
    } else { 
     Toast.makeText(getApplicationContext(),"Cancelled", Toast.LENGTH_LONG).show(); 
+1

第一件事IntentIntegrator是什麼?其次你不能直接從內置攝像頭獲得qr代碼內容 –

回答

0

要保存結果SharedPreference

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 
SharedPreferences.Editor editor = prefs.edit(); 
editor.putString("result", scanContent); 
editor.commit(); 

爲了取回SharedPreference

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 
String result = prefs.getString("result", "No saved result"); 

context是你的活動範圍內。如果此代碼處於任何活動狀態,只需使用this

+0

無需在onactivityResult方法中寫入任何東西 – pruthvi

+0

你想要將'onActivityResult'中得到的結果保存到'SharedPreference'中,對嗎? – Rohit5k2

+0

yes的確如此 – pruthvi

0
 @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if(requestCode==IntentIntegrator.REQUEST_CODE){ 
      IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); 
      if (result != null) { 
       String scanContent = result.getContents(); 
       customerSno.setText(" " + scanContent); 

       SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
       SharedPreferences.Editor editor = prefs.edit(); 
       editor.putString("result", scanContent); 
       editor.commit(); 

      } else { 
       Toast.makeText(getApplicationContext(),"Cancelled", Toast.LENGTH_LONG).show(); 
      } 
     } 
     }