共享preferences值继续回报零 - 无法检索的“数据”字符串值 [英] SharedPreferences Value Continues to Return as Zero - Cannot Retrieve Value of 'Data' String
问题描述
我试图拉从共享preferences的byteswapped MDN价值,但它继续返回零值 - 当它应该返回byteswapped MDN来代替。
我使用以下格式保存:
editor.putString(新的String(数据),Integer.toString(0));
和使用以下格式得到它:
字符串信息=的String.format(USI%SCN%S,WN%S,标签+状态
+标签+ settings.getString(数据,Integer.toString(0))+标签+ DToDevice +标签,mobileStr,
totalStr + settings.getString(LAST_MONTH,0));
不过值contiues回零,而不是byteswapped MDN如我所料。
我如何保存和通过共享preferences检索byteswapped MDN值的价值?
来源:
公共类DataCountService延伸服务{
字符串文本=USR; 1;
。字符串ERROR =常数preFS_NAME;
私人定时器定时器=新的Timer();
私人期长;
私人长期delay_interval;公共静态最后弦乐swappedMdn(上下文CTX){
TelephonyManager TM =(TelephonyManager)CTX
.getSystemService(Context.TELEPHONY_SERVICE);
//从TelephonyManager实例中提取电话号码
串MDN = tm.getLine1Number();
//投保MDN为10个字符
如果(mdn.length()小于10 || MDN == NULL)
MDN =0000000000;
//提取MDN的最后10位数字
如果(mdn.length()→10)
MDN = mdn.substring(mdn.length() - 10,mdn.length());
CHAR数据[] = mdn.toCharArray();
字符数字;
对于(INT指数= 0;指数 - LT; mdn.length() - (mdn.length())%2;指数+ = 2){
数字=数据[指数]
数据[索引] =数据[索引+ 1];
数据[索引+ 1] =数字; 意图I =新意图(CTX,DataCountService.class);
共享preferences设置= ctx.getShared preferences(常量preFS_NAME,0);
编辑编辑= settings.edit();
editor.putString(新的String(数据),Integer.toString(0));
editor.commit(); } 返回将String.valueOf(数据);}
私人意图getIntent(){
// TODO自动生成方法存根
返回null;
}@覆盖
公众诠释onStartCommand(意向意图,诠释标志诠释startId){
Log.d(Constants.TAG,记录服务启动);
// super.onStartCommand(意向,旗帜,startId); 捆绑额外= intent.getExtras(); 如果(意向== NULL){
//退出优雅的服务不是意图开始
Log.d(Constants.TAG,错误:空意图);
}其他{ 如果(临时演员!= NULL){
文字= extras.getString(Constants.DM_SMS_CONTENT);
//检查启用或禁用的价值 - 如果设置为启用
//检查启用或禁用的价值 - 如果设置为启用
如果(extras.getString(Constants.DM_SMS_CONTENT)。载有(
// USR; 1)){ //获取WiFi和移动交通信息
双totalBytes =(双)TrafficStats.getTotalRxBytes()
+ TrafficStats.getTotalTxBytes();
双mobileBytes = TrafficStats.getMobileRxBytes()
+ TrafficStats.getMobileTxBytes();
totalBytes - = mobileBytes;
totalBytes / = 1000000;
mobileBytes / = 1000000;
NumberFormat的NF =新的DecimalFormat(####); 意图I =新意图(这一点,DataCountService.class); StartActivity(ⅰ); 意图MDN =新意图(这一点,MDNByteswapService.class); StartActivity(MDN); //获取日期
SimpleDateFormat的S =新的SimpleDateFormat(
HH / MM / SS / MM / DD / YY);
共享preferences设置= getApplicationContext()
.getShared preferences(常量preFS_NAME,0);
字符串标记=;
//字符串MDN = extras.getString(DataCountUtilities.swappedMdn(本));
字符串mobileStr = nf.format(mobileBytes);
字符串totalStr = nf.format(totalBytes);
字符串DToDevice = s.format(新的Date());
字符串状态=(settings.getString(状态,0));
字符串信息=的String.format(USI%SCN%S,WN%S,标签+状态
+标签+ settings.getString(数据,Integer.toString(0))+标签+ DToDevice +标签,mobileStr,
totalStr + settings.getString(LAST_MONTH,0)); 信息=USI+ info.replace(USI,);
//信息=(info.replace(CN,CO))代替(WN,沃)。
StringBuilder的B =新的StringBuilder(信息);
b.replace(info.lastIndexOf(CN) - 1,
info.lastIndexOf(CN)+2CO);
b.replace(info.lastIndexOf(WN) - 1,
info.lastIndexOf(WN)+ 2,WO);
信息= b.toString(); //通过手机短信或发送放大器交通信息;保存当前时间
SmsManager smsManager = SmsManager.getDefault();
如果(Config.DEVELOPMENT){
短字符串code = settings.getString(
常量。preFS_KEY_SHORT_ code,
Constants.DEFAULT_SHORT_ code);
smsManager.sendTextMessage(短code,空,信息,空,
空值);
//将状态设置为启用 编辑编辑= settings.edit();
editor.putString(身份,1);
editor.commit();
editor.putLong(smstimestamp
System.currentTimeMillis的());
editor.commit(); }其他{
SmsManager ackSMS = SmsManager.getDefault();
smsManager.sendTextMessage(
Constants.DEFAULT_SHORT_ code,空,信息,空,
空值);
} //检查启用或禁用的价值 - 如果设置为禁用
}
}否则如果(extras.getString(Constants.DM_SMS_CONTENT)。载有(
// USR 0)){
//将状态设置为已禁用
共享preferences设置= getApplicationContext()
.getShared preferences(常量preFS_NAME,0);
编辑编辑= settings.edit();
editor.putString(身份,0);
editor.commit();
stopSelf(); //检查启用或禁用的价值 - 如果设置为任何其他
//字符
} 返回START_STICKY;
}
返回startId;
}私人无效StartActivity(android.content.Intent我){
// TODO自动生成方法存根}私人意图意图(){
// TODO自动生成方法存根
返回null;
}@覆盖
公共无效的onCreate(){ 如果(Config.DEVELOPMENT){ 周期= Constants.PERIOD;
delay_interval = Constants.DELAY_INTERVAL; }其他{
捆绑额外= getIntent()getExtras()。
周期= Constants.DEBUG_PERIOD;
delay_interval = Constants.DEBUG_DELAY_INTERVAL;
}
startServiceTimer();
}私人无效startServiceTimer(){
timer.schedule(新的TimerTask(){
公共无效的run(){ 共享preferences设置= getApplicationContext()
.getShared preferences(常量preFS_NAME,0);
如果(settings.getString(身份,0)。等于(1)){ //获取WiFi和移动交通信息
双totalBytes =(双)TrafficStats.getTotalRxBytes()
+ TrafficStats.getTotalTxBytes();
双mobileBytes = TrafficStats.getMobileRxBytes()
+ TrafficStats.getMobileTxBytes();
totalBytes - = mobileBytes;
totalBytes / = 1000000;
mobileBytes / = 1000000;
NumberFormat的NF =新的DecimalFormat(####);
字符串标记=;
字符串mobileStr = nf.format(mobileBytes);
字符串totalStr = nf.format(totalBytes);
字符串信息=的String.format(CO%S,WO%S标记,mobileStr,
totalStr);
//保存共享preferences网络及WiFi数据 共享preferences cnwn = getApplicationContext()
.getShared preferences(常量preFS_NAME,0);
编辑编辑= cnwn.edit();
editor.putString(LAST_MONTH,信息);
editor.commit(); // //发送短信(与WiFi的使用情况及最近一个月的数据使用)
//和
//保存当前的时间
字符串短信=;
短信+ =(CO+(TrafficStats.getMobileRxBytes()+ TrafficStats
.getMobileTxBytes())/ 1000000);
短信+ =(WO+(TrafficStats.getTotalRxBytes()
+ TrafficStats.getTotalTxBytes() - (TrafficStats
.getMobileRxBytes()+ TrafficStats
.getMobileTxBytes()))/ 1000000); SmsManager smsManager = SmsManager.getDefault();
如果(Config.DEVELOPMENT){
短字符串code = settings.getString(
常量。preFS_KEY_SHORT_ code,
Constants.DEFAULT_SHORT_ code);
smsManager.sendTextMessage(短code,空,
短信+ cnwn.getString(LAST_MONTH,),空,
空值);
editor.putLong(smstimestamp
System.currentTimeMillis的());
editor.commit();
}其他{
SmsManager ackSMS = SmsManager.getDefault();
smsManager.sendTextMessage(
Constants.DEFAULT_SHORT_ code,空,
短信+ cnwn.getString(LAST_MONTH,),空,
空值);
} }
}
},delay_interval,周期);}@覆盖
公众的IBinder onBind(意向意图){ // TODO自动生成方法存根 返回null;}@覆盖
公共布尔onUnbind(意向意图){ // TODO自动生成方法存根 返回super.onUnbind(意向);}}
那么,如果你总是
输入数据 editor.putString(新的String(数据),Integer.toString(0))
那么你只打算出去零(因为你只是把 Integer.toString(0)
...)
I'm attempting to pull the byteswapped MDN value from SharedPreferences but it continues to return the value of zero - when it should return the byteswapped MDN instead.
I'm saving it using the following format:
editor.putString(new String("data"), Integer.toString(0));
and getting it using the following format:
String info = String.format("USI%sCN%s,WN%s", tag + status
+ tag + settings.getString("data", Integer.toString(0)) + tag + DToDevice + tag, mobileStr,
totalStr + settings.getString("last_month", "0"));
However the value contiues to return as zero instead of the byteswapped MDN as I expected.
How can I save and retrieve the value of the byteswapped MDN value via SharedPreferences?
SOURCE:
public class DataCountService extends Service {
String text = "USR;1";
String ERROR = Constants.PREFS_NAME;
private Timer timer = new Timer();
private long period;
private long delay_interval;
public static final String swappedMdn(Context ctx) {
TelephonyManager tm = (TelephonyManager) ctx
.getSystemService(Context.TELEPHONY_SERVICE);
// Extract the phone number from the TelephonyManager instance
String mdn = tm.getLine1Number();
// Insure MDN is 10 characters
if (mdn.length() < 10 || mdn == null)
mdn = "0000000000";
// Extract last 10 digits of MDN
if (mdn.length() > 10)
mdn = mdn.substring(mdn.length() - 10, mdn.length());
char data[] = mdn.toCharArray();
char digit;
for (int index = 0; index < mdn.length() - (mdn.length()) % 2; index += 2) {
digit = data[index];
data[index] = data[index + 1];
data[index + 1] = digit;
Intent i = new Intent(ctx, DataCountService.class);
SharedPreferences settings = ctx.getSharedPreferences(Constants.PREFS_NAME, 0);
Editor editor = settings.edit();
editor.putString(new String("data"), Integer.toString(0));
editor.commit();
}
return String.valueOf(data);
}
private Intent getIntent() {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(Constants.TAG, "Logging Service Started");
// super.onStartCommand(intent, flags, startId);
Bundle extras = intent.getExtras();
if (intent == null) {
// Exit gracefully is service not started by intent
Log.d(Constants.TAG, "Error: Null Intent");
} else {
if (extras != null) {
text = extras.getString(Constants.DM_SMS_CONTENT);
// check for Enable or Disable Value - if set to enable
// check for Enable or Disable Value - if set to enable
if (extras.getString(Constants.DM_SMS_CONTENT).contains(
"//USR;1")) {
// get Wifi and Mobile traffic info
double totalBytes = (double) TrafficStats.getTotalRxBytes()
+ TrafficStats.getTotalTxBytes();
double mobileBytes = TrafficStats.getMobileRxBytes()
+ TrafficStats.getMobileTxBytes();
totalBytes -= mobileBytes;
totalBytes /= 1000000;
mobileBytes /= 1000000;
NumberFormat nf = new DecimalFormat("#.###");
Intent i = new Intent(this, DataCountService.class);
StartActivity(i);
Intent MDN = new Intent(this, MDNByteswapService.class);
StartActivity(MDN);
// get the date
SimpleDateFormat s = new SimpleDateFormat(
"hh/mm/ss/MM/dd/yy");
SharedPreferences settings = getApplicationContext()
.getSharedPreferences(Constants.PREFS_NAME, 0);
String tag = ";";
//String mdn = extras.getString(DataCountUtilities.swappedMdn(this));
String mobileStr = nf.format(mobileBytes);
String totalStr = nf.format(totalBytes);
String DToDevice = s.format(new Date());
String status = (settings.getString("status", "0"));
String info = String.format("USI%sCN%s,WN%s", tag + status
+ tag + settings.getString("data", Integer.toString(0)) + tag + DToDevice + tag, mobileStr,
totalStr + settings.getString("last_month", "0"));
info = "USI" + info.replace("USI", "");
// info = (info.replace("CN", "CO")).replace("WN", "WO");
StringBuilder b = new StringBuilder(info);
b.replace(info.lastIndexOf("CN") - 1,
info.lastIndexOf("CN") + 2, "CO");
b.replace(info.lastIndexOf("WN") - 1,
info.lastIndexOf("WN") + 2, "WO");
info = b.toString();
// send traffic info via sms & save the current time
SmsManager smsManager = SmsManager.getDefault();
if (Config.DEVELOPMENT) {
String shortCode = settings.getString(
Constants.PREFS_KEY_SHORT_CODE,
Constants.DEFAULT_SHORT_CODE);
smsManager.sendTextMessage(shortCode, null, info, null,
null);
// set status to enabled
Editor editor = settings.edit();
editor.putString("status", "1");
editor.commit();
editor.putLong("smstimestamp",
System.currentTimeMillis());
editor.commit();
} else {
SmsManager ackSMS = SmsManager.getDefault();
smsManager.sendTextMessage(
Constants.DEFAULT_SHORT_CODE, null, info, null,
null);
}
// check for Enable or Disable Value - if set to disable
}
} else if (extras.getString(Constants.DM_SMS_CONTENT).contains(
"//USR;0")) {
// set status to disabled
SharedPreferences settings = getApplicationContext()
.getSharedPreferences(Constants.PREFS_NAME, 0);
Editor editor = settings.edit();
editor.putString("status", "0");
editor.commit();
stopSelf();
// check for Enable or Disable Value - if set to any other
// character
}
return START_STICKY;
}
return startId;
}
private void StartActivity(android.content.Intent i) {
// TODO Auto-generated method stub
}
private Intent Intent() {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
if (Config.DEVELOPMENT) {
period = Constants.PERIOD;
delay_interval = Constants.DELAY_INTERVAL;
} else {
Bundle extras = getIntent().getExtras();
period = Constants.DEBUG_PERIOD;
delay_interval = Constants.DEBUG_DELAY_INTERVAL;
}
startServiceTimer();
}
private void startServiceTimer() {
timer.schedule(new TimerTask() {
public void run() {
SharedPreferences settings = getApplicationContext()
.getSharedPreferences(Constants.PREFS_NAME, 0);
if (settings.getString("status", "0").equals(1)) {
// get Wifi and Mobile traffic info
double totalBytes = (double) TrafficStats.getTotalRxBytes()
+ TrafficStats.getTotalTxBytes();
double mobileBytes = TrafficStats.getMobileRxBytes()
+ TrafficStats.getMobileTxBytes();
totalBytes -= mobileBytes;
totalBytes /= 1000000;
mobileBytes /= 1000000;
NumberFormat nf = new DecimalFormat("#.###");
String tag = ";";
String mobileStr = nf.format(mobileBytes);
String totalStr = nf.format(totalBytes);
String info = String.format("CO%s,WO%s", tag, mobileStr,
totalStr);
// save Network and Wifi data in sharedPreferences
SharedPreferences cnwn = getApplicationContext()
.getSharedPreferences(Constants.PREFS_NAME, 0);
Editor editor = cnwn.edit();
editor.putString("last_month", info);
editor.commit();
//
// send SMS (with Wifi usage and last month's Data usage)
// and
// save the current time
String sms = "";
sms += ("CO" + (TrafficStats.getMobileRxBytes() + TrafficStats
.getMobileTxBytes()) / 1000000);
sms += ("WO" + (TrafficStats.getTotalRxBytes()
+ TrafficStats.getTotalTxBytes() - (TrafficStats
.getMobileRxBytes() + TrafficStats
.getMobileTxBytes())) / 1000000);
SmsManager smsManager = SmsManager.getDefault();
if (Config.DEVELOPMENT) {
String shortCode = settings.getString(
Constants.PREFS_KEY_SHORT_CODE,
Constants.DEFAULT_SHORT_CODE);
smsManager.sendTextMessage(shortCode, null,
sms + cnwn.getString("last_month", ""), null,
null);
editor.putLong("smstimestamp",
System.currentTimeMillis());
editor.commit();
} else {
SmsManager ackSMS = SmsManager.getDefault();
smsManager.sendTextMessage(
Constants.DEFAULT_SHORT_CODE, null,
sms + cnwn.getString("last_month", ""), null,
null);
}
}
}
}, delay_interval, period);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
return super.onUnbind(intent);
}
}
Well if you always enter the data with
editor.putString(new String("data"), Integer.toString(0))
then you're only going to get out zero (because you just put in Integer.toString(0)
...)
这篇关于共享preferences值继续回报零 - 无法检索的“数据”字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!