0

我正在尝试检查服务中的附加功能,但出现两个错误:

“不兼容的操作数类型 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);

    }

}
4

2 回答 2

0

您正在尝试在您的方法中将 aBundle与 a进行比较:StringonCreate

如果(额外!=“0”){

这是不可能的。

此外,您的课程 extends Service,而不是Activity. getIntent()方法属于Activity,不是Service

您应该将 onCreate() 代码移动到 onStartCommand() 方法中:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Bundle extras = intent.getExtras();

    if (extras != "0") {
        ...
    }
    return START_STICKY;
}

@Override
public void onCreate() {
    // Nothing to do
}
于 2013-06-18T21:40:47.297 回答
0

不兼容的操作数类型 Bundle 和 String

您的第一个操作数 ( extras) 是 type Bundle,而第二个"0"是 type String。这就是编译器抱怨的原因if (extras != "0")

为了让编译器不抱怨两个操作数应该是相同的类型。

但是由于它们都是引用类型,所以!=(also ==) 执行一个标识。您很可能想要使用该equals()方法代替的相等性检查。看看这个

目前还不太清楚,您在这里的意图是什么,即您实际上想要达到什么目的extras != "0",以便给您正确的建议。

该类型的方法 getIntent() 未定义

你的班级,DataCountService没有方法getIntent()

为了让它工作,你应该首先在DataCountService.

于 2013-06-18T21:41:33.213 回答