从第一次运行中保存字符串以便稍后启动一个活动

时间:2016-12-16 14:56:38

标签: android android-activity sharedpreferences android-sharedpreferences

我的Android应用程序中有4个活动。

1)启动活动 - 决定应用程序是否首次启动并决定打开哪个活动

2)主要活动 - 打开相机并开始扫描的按钮,即进入QR活动

2)QR活动 - 扫描QR码

3)网络活动 - 成功扫描后,在应用中打开网页。使用QR码中的数据制作网页的URL

在我的启动活动中,我会检查它是否是第一次运行。如果是,我参加了主要活动,如果没有,我想参加网络活动。在我的QR活动中,我扫描QR码并从中获取一个数字。我在下一个活动中使用这个号码,即网络活动使用扫描的号码制作网址并打开网页,但现在,因为我想根据应用程序运行编号启动不同的活动,我想保存扫描应用程序所有未来运行的第一个活动的编号。就像Facebook一样,它存储我们以后所有运行的登录凭据。

我正在尝试做类似的事情,但扫描的值不会传递给我的网络活动

ScannerActivity.java

public static final String PREFS_NAME = "myPrefs";
if (barcodes.size() != 0) {

                Intent intent = new Intent(getApplication(), WebActivity.class);
                //intent.putExtra("result",barcodes.valueAt(0));

                SharedPreferences.Editor editor=settings.edit();
                editor.putString("result", barcodes.valueAt(0).displayValue);
                editor.commit();

                startActivity(intent);
                finish();
            }

WebActivity.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web);

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    String result = settings.getString("result", "");

    /*Barcode barcode = (Barcode) getIntent().getParcelableExtra("result");
    Toast.makeText(WebActivity.this, barcode.displayValue, Toast.LENGTH_LONG).show();*/

    Toast.makeText(WebActivity.this, result, Toast.LENGTH_SHORT).show();

    webView = (WebView) findViewById(R.id.webview);
    webView.setWebViewClient(new myWebClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl(" http://url?u="+result);

}

3 个答案:

答案 0 :(得分:0)

您建议访问Android Studio Storage Option以更好地了解机制和功能

话虽如此,请允许我向您提供我在应用程序中存储值的片段

请注意,您需要进行微调,因为我在片段中应用了Sharedpreferences

第一

   addusername = (EditText) oView.findViewById(R.id.addnewUsername);

第二

  //adjustment and reattachment
  String bonsiour = addusername.getText().toString().trim();

            SharedPreferences sPref = getActivity().getPreferences(0);
            SharedPreferences.Editor edt = sPref.edit();
            edt.putString("key1", bonsiour);
            edt.commit();


            //toast to confirm value has been saved
            String buzo = sPref.getString("key1", "empty");
            Toast.makeText(getActivity(), "You're  " + buzo + "!!", Toast.LENGTH_LONG).show();

这是如何从中提取/读取

  SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
    String name = prefs.getString("key1", "No name defined");

    if(name.equals("PDF_FOUND")){
        Toast.makeText(Controller.this,"IT WORKED !", Toast.LENGTH_SHORT).show();

   //skip the splash screen and move to another activity asap



    } else{
       Toast.makeText(Controller.this,"BAD NEWS !", Toast.LENGTH_SHORT).show();


  //display Splash screen

 }    
}

答案 1 :(得分:0)

要将您的字符串存储在共享首选项中,您可以在一行中执行以下操作:

String QRCode = "code";
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("QRCode", QRCode).commit();

要检索存储的String的值,请使用:

//The second parameter for getString() is the default value to return if QRCode is not set 
String QRCode = PreferenceManager.getDefaultSharedPreferences(this).getString("QRCode", "NOT_FOUND");

if(QRCode.equals("NOT_FOUND")){
    //open MainActivity
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
} else {
    //open WebActivity
    Intent intent = new Intent(this, WebActivity.class);
    //you can pass code to webactivity or retrieve it from shared prefs
    intent.putExtra("QRCode", QRCode);
    startActivity(intent);
}

答案 2 :(得分:0)

您可以使用SharedPreferences存储String并稍后重新使用

例如,如果要存储字符串,请首先创建键以获取值

public static final String         sharedPreferencesKey = "shareKey";
public static final String         stringKey = "stringKey"; 

存储您的价值

SharedPreferences sharedpreferences =
       context.getSharedPreferences(sharedPreferencesKey, Context.MODE_PRIVATE);
SharedPreferences.Editor ed = sharedpreferences.edit();
ed.putString(stringKey, "");
ed.apply();

获得你的价值

if (sharedpreferences.contains(stringKey))
    sharedPreferences.getString(stringKey, "")