0

I'm attempting to check for extras in my service and I'm getting two errors:

"Incompatible operand types Bundle and String" on the line:

if (extras != "0") {

...and "The method getIntent() is undefined for the type" on the line:

Bundle extras = getIntent().getExtras();

SOURCE:

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);

    }

}
Amani Swann
  • 11
  • 1
  • 5

2 Answers2

0

You are trying to compare a Bundle with a String in your onCreate method:

if (extras != "0") {

It's not possible.

Also, your class extends Service, not Activity. getIntent() method belongs to Activity, not Service.

You should move the onCreate() code into onStartCommand() method:

@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
}
Donkey
  • 1,176
  • 11
  • 19
  • It's difficult to give you an answer because I don't see what you want to do exactly. Can you give us more information about what you want to do? – Donkey Jun 18 '13 at 21:47
  • May be you should call _getInt(key, defaultValue)_ or _getString(key)_ on your variable _extras_ to retrieve the value you want and to compare it to 0 or "0" – Donkey Jun 18 '13 at 21:52
  • Im attempting to find out if the bundled extra contains a 1 - and if so execute the code below that line – Amani Swann Jun 18 '13 at 21:53
  • How did you put this 1 into this bundle? You should retrieve it with the key you used to insert it into the bundle. But first of all, you should move the content of your onCreate() method into onStartCommand() where you have the intent you want. – Donkey Jun 18 '13 at 22:05
0

Incompatible operand types Bundle and String

Your first operand (extras) is of type Bundle, while the second one "0" is of type String. That's why the compiler is complaining at if (extras != "0").

For the compiler to not complain both the operands should be of same type.

But since both of them are of reference types, != (also ==) performs an identity. You most probably want equality check for which you should use the equals() method instead. Check this out.

It's not quite clear, what your intention here is, i.e. what you are actually trying to achieve with extras != "0", to give you the right suggestion.

The method getIntent() is undefined for the type

Your class, DataCountService doesn't have the method getIntent().

For it to work, you should define that method first in DataCountService.

Community
  • 1
  • 1
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142