试图捆绑附加功能:不兼容的操作数类型Bundle和String&方法getIntent()未定义类型

时间:2013-06-18 21:33:11

标签: java android android-intent service extras

我正在尝试检查我的服务中的额外内容,我遇到两个错误:

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

    }

}

2 个答案:

答案 0 :(得分:0)

您正试图在Bundle方法中将StringonCreate进行比较:

  

if(extras!=“0”){

这是不可能的。

此外,您的课程延伸Service,而不是ActivitygetIntent()方法属于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
}

答案 1 :(得分:0)

  

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

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

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

但由于它们都是引用类型,!=(也==)执行标识。您最有可能需要使用equals()方法进行等式检查。检查this

目前还不是很清楚,你的目的是什么,即你用extras != "0"实际想要达到的目的,给你正确的建议。

  

对于类型

,方法getIntent()未定义

您的班级DataCountService没有方法getIntent()

要使其正常工作,您应首先在DataCountService中定义该方法。