如何从活动结果方法获得共享首选项的结果

时间:2016-02-12 12:45:57

标签: android

我是android的初学者...在qr扫描仪中点击按钮后它会扫描  并在活动中显示结果。我希望将显示的结果显示给共享首选项..任何人都可以帮助我

下面是我的代码

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

}

2 个答案:

答案 0 :(得分:1)

使用此选项将您的号码保存到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

此解决方案将值保存为Int并读取为String,但这可能不是您想要的,可以根据您的需要轻松重新配置。

答案 1 :(得分: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");
        }
    }

}