Reputation: 342
I have an SQL
database that works, and the broadcast receiver
that functions, but I can't seem to get the info out of the database, into global variables (latInfo
, LonInfo
) so I can use them to enter data into the database. The BroadcastReceiver
gets it's information from a service that gets LocationManager
gps coordinate and the location_text
is a textview
that is updated every time a new set of coordinates is set.
@Override
protected void onResume() {
super.onResume();
if(br == null){
br = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent z) {
//location_text.append("\n" + z.getExtras().get("coordinates"));
latInfo = (double) z.getExtras().get("latitude");
lonInfo = (double) z.getExtras().get("longitude");
String x = "Latitude: " + latInfo + ", Longitude: " + lonInfo + "\n";
location_text.append(x);
}
};
}
registerReceiver(br, new IntentFilter("location_update"));
}
This is the method I want to implement to add data to the database
simply put, once the data I can get into the global variables (lonInfo
, latInfo
) is registered by the class below, it calls another class to get the cell signal cdmaSignal
and all three get inserted into the database.
public void infoCollection(){
if(lonInfo != 0 && latInfo != 0){
cdmaInfo = sl.cdmaSignal;
SQLInfoDatabase sid = new SQLInfoDatabase(cdmaInfo,lonInfo,latInfo);
cdmaSignal.setText(cdmaInfo);
dbh.addInfo(sid);
}
}
How do I get the received data from the onReceive
method to stay as global variables between updates of the Service?
Upvotes: 0
Views: 95
Reputation: 704
you can make static variables or can use singleton class
below is sample of singleton class
public class SharedClass
{
private static SharedClass sharedClass;
// global variables
public double latInfo, LonInfo;
public static SharedClass getInstance()
{
if (sharedClass == null)
sharedClass = new SharedClass();
return sharedClass;
}
private SharedClass()
{
}
}
you can access latInfo or LonInfo from anywhere in application by doing this
SharedClass.getInstance().latInfo
SharedClass.getInstance().LonInfo
Upvotes: 1