0

I have an activity which creates a unique ID and then passes the String to a service, the problem I am having is that the app force-closes every time I try passing the data from the activity to the service.

Normally when I am programming java applications I just use getter and setter methods to pass data between classes, so does anyone have an idea why this problem is occurring?

public void setId(String mydeviceId){

    TelephonyManager tm = (TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
    final String DeviceId, SerialNum, androidId;
    DeviceId = tm.getDeviceId();
    SerialNum = tm.getSimSerialNumber();
    androidId = Secure.getString(getContentResolver(),Secure.ANDROID_ID);
    UUID deviceUuid = new UUID(androidId.hashCode(), ((long)DeviceId.hashCode() << 32) | SerialNum.hashCode());
    mydeviceId = deviceUuid.toString(); 
    this.mydeviceId = mydeviceId;   

    }


    public String getId(){

        return mydeviceId;

    }
cdeszaq
  • 30,869
  • 25
  • 117
  • 173
Grady-lad
  • 105
  • 1
  • 2
  • 9
  • 1
    Pass between classes means activities? If so, you need to use Intent bundle. Get/set wont work between activities. – kosa Jan 23 '12 at 21:16
  • 1
    Please post the stack trace from logcat, the source where the exception was raised, and identify the line number in the source where the exception was raised. – Ted Hopp Jan 23 '12 at 21:18
  • I was getting a null pointer exception this must of been down to the point that @thinksteep made that I need to use an Intent bundle, so I will take a look in to how pass data using intent bundle – Grady-lad Jan 23 '12 at 21:31

2 Answers2

1

Pass between classes means activities? If so, you need to use Intent bundle. Get/set wont work between activities. Here is an example how to pass data between activities.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • cheers for that thinksteep, I was wondering would I also have to this when passing data from a service to a java class ? – Grady-lad Jan 23 '12 at 21:36
  • No, you cannot do this with services. Refer to my answer on this. – waqaslam Jan 23 '12 at 21:39
  • Here is SO discussion on service to activity. http://stackoverflow.com/questions/3747448/android-passing-data-between-service-and-activity. By the way, one small suggestion, in Android terms (not official, but understanding purpose) if you are discussing about activity, its better not call as class(which may represent helper class/inner class). – kosa Jan 23 '12 at 21:41
1

In order to communicate with your service, allow your Activity to bind with it. Refer to this guide. Its is the recommended way.

Alternatively, you may broadcast your desired values to communicate between Service and/or Activity. For more info, refer to this

waqaslam
  • 67,549
  • 16
  • 165
  • 178