Question

I'm getting the following error when attempting to pass data between an activity and a service using the following method and I'm really not sure why:

DataCountUtility:

public class DataCountUtilities {

    //swap the content of a string by switching place
    //each pair of consecutive characters
    //If string length is odd last character is left in place
    public String swappedMdn(Context ctx){ 
        TelephonyManager tm = (TelephonyManager)ctx.getSystemService(Context.TELEPHONY_SERVICE);
        //Extract the phone number from the TelephonyManager instance
        String mdn = tm.getLine1Number();
        //Insure MDN is 10 characters
        if (mdn.length() < 10 || mdn == null) mdn ="0000000000";
        //Extract last 10 digits of MDN
        if (mdn.length() > 10) mdn = mdn.substring(mdn.length() - 10, mdn.length()); 
        char data[] = mdn.toCharArray();
        char digit;
        for (int index = 0; index < mdn.length() - (mdn.length())%2; index+=2){
            digit = data[index];
            data[index] = data[index+1];
            data[index+1] = digit;


            Intent i = new Intent(DataCountUtilities.this, DataCountService.class);  
            i.putExtra("key", mdn);  
            startActivity(i);  

                }

        return String.valueOf(data); 

}

    private void startActivity(Intent i) {
        // TODO Auto-generated method stub

    }}
Was it helpful?

Solution

Intent expects a Context as first parameter.

Change the line

Intent i = new Intent(DataCountUtilities.this, DataCountService.class);

to

Intent i = new Intent(ctx, DataCountService.class);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top