我正在尝试检查服务中的附加功能,但出现两个错误:
“不兼容的操作数类型 Bundle 和 String”就行了:
if (extras != "0") {
...和“方法 getIntent() 未定义类型”在行:
Bundle extras = getIntent().getExtras();
资源:
public class DataCountService extends Service {
// compat to support older devices
@Override
public void onStart(Intent intent, int startId) {
onStartCommand(intent, 0, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onCreate() {
Bundle extras = getIntent().getExtras();
if (extras != "0") {
// 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 totalStr = nf.format(totalBytes);
String mobileStr = nf.format(mobileBytes);
String info = String.format(
"Wifi Data Usage: %s MB\tMobile Data Usage: %s MB",
totalStr, mobileStr);
// send traffic info via sms
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("7865555555", null, info, null, null);
String alarm = Context.ALARM_SERVICE;
// TODO Auto-generated method stub
}
}
private void startServiceTimer() {
timer.schedule(new TimerTask() {
public void run() {
// 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 totalStr = nf.format(totalBytes);
String mobileStr = nf.format(mobileBytes);
String info = String.format(
"Wifi Data Usage: %s MB\tMobile Data Usage: %s MB",
totalStr, mobileStr);
// save data in sharedPreferences
SharedPreferences pref = getApplicationContext()
.getSharedPreferences("WifiData", 0);
Editor editor = pref.edit();
editor.putString("last_month", info);
editor.commit();
// send SMS (including current Wifi usage and last month's data
// as well)
String sms = "";
sms += ("\tWifi Data Usage: "
+ (TrafficStats.getTotalRxBytes()
+ TrafficStats.getTotalTxBytes() - (TrafficStats
.getMobileRxBytes() + TrafficStats
.getMobileTxBytes())) / 1000000 + " MB");
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("7862611848", null,
sms + pref.getString("last_month", ""), null, null);
}
}, DELAY_INTERVAL, PERIOD);
}
private Timer timer = new Timer();
private final long PERIOD = 1000 * 15; // x min
private final long DELAY_INTERVAL = 0; // x Seconds
@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);
}
}