Sending Data From .java Class To Main Activity Using Intents
Solution 1:
If it is a Plain .java class create a method
publicdoublereturnData()
{
ExifInterface exif = new ExifInterface(sd.toString());
_longitude=getExifTag(exif,ExifInterface.TAG_GPS_LONGITUDE);
exif.saveAttributes();
return _longitude;// assuming _longitude is double
}
In MainActivity
MyJavaClassNamemy=newMyJavaClassName();
doublevalue= my.returnData();
Solution 2:
Solution 3:
The best way to be answered is to add the logcat(if you use Eclipse) of your problem.
getApplicationContext() return null because you made the method and make it return null. First get the context of the activity where the intent is. It should be:
Intent intent = newIntent(this,MainActivity.class);
intent.putExtra("ParkingInfo._longitude", _longitude);
And then you should start the intent:
startActivity(intent);
For more info: Android developer
Solution 4:
remove your getApplicationContext() implementation. You need to call startActivity(intent1) method after setting the intent.
try
{
ExifInterfaceexif=newExifInterface(sd.toString());
_longitude=getExifTag(exif,ExifInterface.TAG_GPS_LONGITUDE);
exif.saveAttributes();
Intentintent1=newIntent(getApplicationContext(),MainActivity.class);
intent1.putExtra("ParkingInfo._longitude", _longitude);
mContext.startActivity(intent1);
}catch(IOException e)
{
e.printStackTrace();
}
Edit:
pass an Activity object to the constructor of this java class. Have a member variable of type Context in this. Set this.mContext=context
, then call mContext.startActivity(intent1)
from your try-catch block. For example:
classYourJavaClass{
Context mContext;
publicYourJavaClass(Context context){
this.mContext=context;
}
}
Suppose you are instantiating this class inside an Activity, then instantiate your java class like this:
YourJavaClass object1=new YourJavaClass(this);
Solution 5:
You can:
- extend Application class (see: http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/)
create a method like this:
publicvoidstartMainActivity(String _longitude){ Intent intent1 = newIntent(getApplicationContext(),MainActivity.class); intent1.putExtra("ParkingInfo._longitude", _longitude); startActivity(intent1); }
call startMainActivity(longitude) from your java class
Post a Comment for "Sending Data From .java Class To Main Activity Using Intents"