サービスのエクストラをチェックしようとしていますが、次の 2 つのエラーが発生します。
行の「互換性のないオペランド タイプ 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);
}
}