2016-02-12 82 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