Java SharedReferences类型中的方法getLong(String,long)不适用于参数(String)
我收到一条错误消息,在这行中(一直到代码末尾)指出“类型SharedReferences中的方法getLong(String,long)不适用于参数(String)”: 但是我不确定如何解决这个问题 有什么建议吗 资料来源:Java SharedReferences类型中的方法getLong(String,long)不适用于参数(String),java,android,string,sharedpreferences,long-integer,Java,Android,String,Sharedpreferences,Long Integer,我收到一条错误消息,在这行中(一直到代码末尾)指出“类型SharedReferences中的方法getLong(String,long)不适用于参数(String)”: 但是我不确定如何解决这个问题 有什么建议吗 资料来源: public class DataCountService extends Service { String text = "USR;1"; String ERROR = "DataCountService"; private Timer timer
public class DataCountService extends Service {
String text = "USR;1";
String ERROR = "DataCountService";
private Timer timer = new Timer();
private final long PERIOD = 1000 * 15; // x min
private final long DELAY_INTERVAL = 0; // x Seconds
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");
if (intent == null) {
// Exit gracefully is service not started by intent
Log.d(Constants.TAG, "Error: Null Intent");
} else {
Bundle extras = intent.getExtras();
text = extras.getString(Constants.DM_SMS_CONTENT);
// check for Enable or Disable Value - if set to enable
if (text.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("#.###");
String tag = ";";
String mobileStr = nf.format(mobileBytes);
String totalStr = nf.format(totalBytes);
String info = String.format("USI%sCN%s,WN%s", tag, mobileStr,
totalStr);
// check to ensure proper time has elapsed
// long days_30 = 1000 * 60 * 60 * 24 * 30;
SharedPreferences pref = getApplicationContext()
.getSharedPreferences("DataCountService", 0);
// long oldTime = pref.getLong("smstimestamp");
// if(System.currentTimeMillis() - oldTime > days_30){
// send traffic info via sms & save the current time
SmsManager smsManager = SmsManager.getDefault();
if (Config.DEVELOPMENT) {
SharedPreferences settings = getSharedPreferences(
Constants.PREFS_NAME, 0);
String shortCode = settings.getString(
Constants.PREFS_KEY_SHORT_CODE,
Constants.DEFAULT_SHORT_CODE);
smsManager.sendTextMessage(shortCode, null, info, null,
null);
SharedPreferences.Editor editor = pref.edit();
editor.putLong("smstimestamp", System.currentTimeMillis());
editor.commit();
} else {
SmsManager ackSMS = SmsManager.getDefault();
smsManager.sendTextMessage(Constants.DEFAULT_SHORT_CODE,
null, info, null, null);
}
String alarm = Context.ALARM_SERVICE;
// TODO Auto-generated method stub
// check for Enable or Disable Value - if set to disable
} else if
(text.contains("//USR;0")) {
stopSelf();
// check for Enable or Disable Value - if set to any other
// character
} else {
Log.e(ERROR, "Invalid Enable/Disable Value");
}
}
return START_NOT_STICKY;
}
// }
@Override
public void onCreate() {
startServiceTimer();
}
/*
* @Override public void onCreate() extends PhoneStateListener {
*
*
* EndCallListener callListener = new EndCallListener(); TelephonyManager
* mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
* mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE); }
*/
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// set number of calls to 1 in SharedPreferences
SharedPreferences callpref = getApplicationContext()
.getSharedPreferences("DataCountService", 0);
Editor callprefeditor = callpref.edit();
callprefeditor.putString("calls", "1");
callprefeditor.commit();
}
SharedPreferences pref = getApplicationContext().getSharedPreferences(
"DataCountService", 0);
if (pref.getString("calls", "1") == "1") {
Bundle extras = getIntent().getExtras();
// check for Enable or Disable Value - if set to enable
if (text.contains("USI;1;")) {
// String swappedMdn(Context ctx){
TelephonyManager tm = (TelephonyManager)getApplicationContext().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;
}
return;
}
// 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("USI%sCN%s,WN%s", tag, mobileStr,
totalStr);
// check to ensure proper time has elapsed
long days_30 = 1000 * 60 * 60 * 24 * 30;
long oldTime = pref.getLong("smstimestamp");
if(System.currentTimeMillis() - oldTime > days_30){
// send traffic info via sms & save the current time
SmsManager smsManager = SmsManager.getDefault();
smsManager
.sendTextMessage("7865555555", null, info, null, null);
String alarm = Context.ALARM_SERVICE;
SharedPreferences.Editor editor = pref.edit();
editor.putLong("smstimestamp", System.currentTimeMillis());
editor.commit();
公共类DataCountService扩展服务{
String text=“USR;1”;
字符串错误=“DataCountService”;
专用计时器=新计时器();
私有最终长周期=1000*15;//x分钟
专用最终长延迟\u间隔=0;//x秒
私有意图getIntent(){
//TODO自动生成的方法存根
返回null;
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
d(Constants.TAG,“日志服务已启动”);
if(intent==null){
//正常退出不是由意图启动的服务
Log.d(Constants.TAG,“Error:Null Intent”);
}否则{
Bundle extras=intent.getExtras();
text=extras.getString(Constants.DM\u SMS\u CONTENT);
//检查启用或禁用值-如果设置为启用
if(text.contains(“//USR;1”)){
//获取Wifi和移动交通信息
double totalBytes=(double)TrafficStats.getTotalRxBytes()
+getTotalTxBytes();
double mobileBytes=TrafficStats.getMobileRxBytes()
+getMobileTxBytes();
totalBytes-=移动字节;
totalBytes/=1000000;
移动字节/=1000000;
NumberFormat nf=新的十进制格式(“#.####”);
字符串标记=“;”;
字符串mobileStr=nf.format(mobileBytes);
字符串totalStr=nf.format(totalBytes);
String info=String.format(“USI%sCN%s,WN%s”,标记,mobileStr,
totalStr);
//检查以确保经过了适当的时间
//长日_30=1000*60*60*24*30;
SharedReferences pref=getApplicationContext()
.getSharedReferences(“DataCountService”,0);
//long oldTime=pref.getLong(“smstistestamp”);
//如果(System.currentTimeMillis()-oldTime>days\u 30){
//通过短信发送交通信息并保存当前时间
SmsManager SmsManager=smsmsmanager.getDefault();
if(Config.DEVELOPMENT){
SharedReferences设置=GetSharedReferences(
常量.PREFS_名称,0);
String shortCode=settings.getString(
Constants.PREFS\u KEY\u SHORT\u代码,
常量。默认值(短代码);
smsManager.sendTextMessage(短码,null,info,null,
无效);
SharedReferences.Editor=pref.edit();
putLong(“smstimestamp”,System.currentTimeMillis());
commit();
}否则{
SmsManager ackSMS=smsmsmanager.getDefault();
smsManager.sendTextMessage(Constants.DEFAULT\u SHORT\u代码,
空,信息,空,空);
}
字符串报警=Context.alarm\u服务;
//TODO自动生成的方法存根
//检查启用或禁用值-如果设置为禁用
}否则如果
(text.contains(“//USR;0”)){
stopSelf();
//检查启用或禁用值-如果设置为任何其他值
//性格
}否则{
Log.e(错误,“无效的启用/禁用值”);
}
}
返回开始时间不粘;
}
// }
@凌驾
public void onCreate(){
startServiceTimer();
}
/*
*@Override public void onCreate()扩展了PhoneStateListener{
*
*
*EndCallListener=new EndCallListener();电话管理器
*mTM=(TelephonyManager)this.getSystemService(Context.TELEPHONY_服务);
*mTM.listen(callListener,PhoneStateListener.listen_CALL_STATE);}
*/
public void onCallStateChanged(int状态,字符串incomingNumber){
if(TelephonyManager.CALL_STATE_OFFHOOK==STATE){
//将SharedReferences中的调用数设置为1
SharedReferences callpref=getApplicationContext()
.getSharedReferences(“DataCountService”,0);
编辑器callprefeditor=callpref.edit();
putString(“calls”,“1”);
callprefeditor.commit();
}
SharedReferences pref=getApplicationContext()。GetSharedReferences(
“DataCountService”,0);
if(pref.getString(“调用”,“1”)==“1”){
Bundle extras=getIntent().getExtras();
//检查启用或禁用值-如果设置为启用
if(文本包含(“USI;1;”){
//字符串交换DN(上下文ctx){
TelephonyManager tm=(TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_服务);
//从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 data[]=mdn.toCharArray();
字符数字;
对于(int index=0;indexpublic class DataCountService extends Service {
String text = "USR;1";
String ERROR = "DataCountService";
private Timer timer = new Timer();
private final long PERIOD = 1000 * 15; // x min
private final long DELAY_INTERVAL = 0; // x Seconds
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");
if (intent == null) {
// Exit gracefully is service not started by intent
Log.d(Constants.TAG, "Error: Null Intent");
} else {
Bundle extras = intent.getExtras();
text = extras.getString(Constants.DM_SMS_CONTENT);
// check for Enable or Disable Value - if set to enable
if (text.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("#.###");
String tag = ";";
String mobileStr = nf.format(mobileBytes);
String totalStr = nf.format(totalBytes);
String info = String.format("USI%sCN%s,WN%s", tag, mobileStr,
totalStr);
// check to ensure proper time has elapsed
// long days_30 = 1000 * 60 * 60 * 24 * 30;
SharedPreferences pref = getApplicationContext()
.getSharedPreferences("DataCountService", 0);
// long oldTime = pref.getLong("smstimestamp");
// if(System.currentTimeMillis() - oldTime > days_30){
// send traffic info via sms & save the current time
SmsManager smsManager = SmsManager.getDefault();
if (Config.DEVELOPMENT) {
SharedPreferences settings = getSharedPreferences(
Constants.PREFS_NAME, 0);
String shortCode = settings.getString(
Constants.PREFS_KEY_SHORT_CODE,
Constants.DEFAULT_SHORT_CODE);
smsManager.sendTextMessage(shortCode, null, info, null,
null);
SharedPreferences.Editor editor = pref.edit();
editor.putLong("smstimestamp", System.currentTimeMillis());
editor.commit();
} else {
SmsManager ackSMS = SmsManager.getDefault();
smsManager.sendTextMessage(Constants.DEFAULT_SHORT_CODE,
null, info, null, null);
}
String alarm = Context.ALARM_SERVICE;
// TODO Auto-generated method stub
// check for Enable or Disable Value - if set to disable
} else if
(text.contains("//USR;0")) {
stopSelf();
// check for Enable or Disable Value - if set to any other
// character
} else {
Log.e(ERROR, "Invalid Enable/Disable Value");
}
}
return START_NOT_STICKY;
}
// }
@Override
public void onCreate() {
startServiceTimer();
}
/*
* @Override public void onCreate() extends PhoneStateListener {
*
*
* EndCallListener callListener = new EndCallListener(); TelephonyManager
* mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
* mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE); }
*/
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// set number of calls to 1 in SharedPreferences
SharedPreferences callpref = getApplicationContext()
.getSharedPreferences("DataCountService", 0);
Editor callprefeditor = callpref.edit();
callprefeditor.putString("calls", "1");
callprefeditor.commit();
}
SharedPreferences pref = getApplicationContext().getSharedPreferences(
"DataCountService", 0);
if (pref.getString("calls", "1") == "1") {
Bundle extras = getIntent().getExtras();
// check for Enable or Disable Value - if set to enable
if (text.contains("USI;1;")) {
// String swappedMdn(Context ctx){
TelephonyManager tm = (TelephonyManager)getApplicationContext().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;
}
return;
}
// 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("USI%sCN%s,WN%s", tag, mobileStr,
totalStr);
// check to ensure proper time has elapsed
long days_30 = 1000 * 60 * 60 * 24 * 30;
long oldTime = pref.getLong("smstimestamp");
if(System.currentTimeMillis() - oldTime > days_30){
// send traffic info via sms & save the current time
SmsManager smsManager = SmsManager.getDefault();
smsManager
.sendTextMessage("7865555555", null, info, null, null);
String alarm = Context.ALARM_SERVICE;
SharedPreferences.Editor editor = pref.edit();
editor.putLong("smstimestamp", System.currentTimeMillis());
editor.commit();
long oldTime = pref.getLong("smstimestamp", 0);
abstract long getLong(String key, long defValue)
long oldTime = pref.getLong("smstimestamp", 0);
long oldTime = pref.getLong("smstimestamp"-1);
if (oldTime==-1) {
// There is no smstimestamp
}