我需要将OnReceivedDataMethod中的字符串数据用于第二个活动,以便将这些字符串数据保存到文件中,但目前我的数据中有空数据
SharedPreferences sp;
UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() { //Defining a Callback which triggers whenever data is read.
@Override
public void onReceivedData(byte[] arg0) {
try {
data = new String(arg0, "UTF-8");
data.concat("/n");
sp = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor et = sp.edit();
et.putString("key",data);
et.apply();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
};
在第二项活动中
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
desiredPreference = sharedPreferences.getString("key","data");
Toast.makeText(this,"spdata"+desiredPreference,Toast.LENGTH_LONG).show();
}
答案 0 :(得分:3)
Value为null,因为您使用<?php
date_default_timezone_set('America/Los_Angeles');
if (!session_id()) {
session_start();
}
$id = $_POST['actid'];
define('VENDOR_DIR', '/'); // Path to the Vendor directory
$loader = include 'vendor/autoload.php';
use FacebookAds\Api;
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Fields\AdSetFields;
use FacebookAds\Object\Fields\UserFields;
if(isset($_SESSION['fb_access_token'])) {
// Initialize a new Session and instanciate an Api object
Api::init('{appinfo}', '{appinfo}', $_SESSION['fb_access_token']);
// The Api object is now available trough singleton
$api = Api::instance();
$account = new AdAccount($id);
$adsets = $account->getAdSets(array(
AdSetFields::NAME,
));
header('Content-Type: application/json');
echo json_encode(array('id'=>$id));
}
?>
返回此活动名称的首选项,它与您在第一个活动中使用的其他首选项不同。相反,您应该按照在第一个活动中调用它的方式调用您的首选项。
PreferenceManager.getDefaultSharedPreferences(this)
要详细了解差异,您可以查看documentation和this答案。
答案 1 :(得分:0)
做这样的事,
用于存储
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("key", data);
editor.commit();
for Retriving
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
String checkDate = sharedPref.getString("key","");