2016-02-12 87 views
-1

我是Android的初學者...在qr掃描器中點擊按鈕後,它將掃描 並顯示結果在活動..我想要顯示的結果共享preferences..can誰能幫助我如何從活動結果方法得到結果共享首選項

下面

是我的代碼

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_new_screen); 
    button1 = (Button) findViewById(R.id.button); 
    customerSno = (TextView) findViewById(R.id.scanContent); 

    button1.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      Intent i = new Intent(getApplicationContext(), IHomeActivity.class); 

      startActivity(i); 

     } 
    }); 
} 

private void setCustomerSerialNName(String s) { 

String customerSNo = IHomeActivity._sharedPreferences.getString("customerSNo", "null"); 

SharedPreferences.Editor editor =IHomeActivity._sharedPreferences.edit(); 

    editor.putString("customerSNo",s); 

    //editor.putString("customerPass", passcode); 
    if (customerSNo.equals(s)) { 
    } else { 
     editor.putBoolean("custSNoAuthStatus", false); 
    } 
    editor.commit(); 
    LightManager.LightOp lightOp = new LightManager.LightOp(); 
    lightOp.setToWWWMode(); 
} 





public void scanMarginScanner(View view) { 
    IntentIntegrator integrator = new IntentIntegrator(this); 
    integrator.setOrientationLocked(false); 
    integrator.setCaptureActivity(SmallCaptureActivity.class); 
    integrator.initiateScan(); 
} 


@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(); 
    } 
} 

}

+0

什麼是customerSno? –

回答

0

如果你想導致onActivityResult那麼你必須調用

startActivityForResult(intent, request_code); 
來電顯示活動

,並呼籲活動添加代碼:

Intent intent = new Intent(); 
    intent.putExtra("key", value); 
    setResult(RESULT_OK, intent); 

,並完成呼叫活動

,並在onActivityResult方法:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 


    if (requestCode == request_code){ 
     if (resultCode == RESULT_OK){ 
      String value = data.getExtras().getString("key"); 
     } 
    } 

} 
0

使用此來保存你的電話號碼爲SharedPreferences:

SharedPreferences sp = this.getSharedPreferences("myPrefName", 0); 
    SharedPreferences.Editor editor = sp.edit(); 
    editor.putInt("myPrefNumVarName", myPrefNumVarValue); 
    editor.commit(); 

然後閱讀已保存的值:

sp = PreferenceManager.getDefaultSharedPreferences(this); 
    String myValue = sp.getString("myPrefNumVarName", "none"); // second string is the return, used when nothing is returned from the first string 

該解決方案節省了價值爲I​​NT且讀爲字符串,而這可能不是你想要的,可以很容易地重新配置,滿足您的需求,雖然。

+0

@pruthvi v reddy如果它解決了你的問題,請檢查答案是否正確。謝謝。 – statosdotcom