Reputation: 1049
I've been struggling for hours now, my global variable seems to work in my service but when I try access that variable from my activity it gives no results.
Global var class:
public class GlobalShare extends Application{
public static GlobalShare singleton;
public List<LocationUsageDB> processed = new ArrayList<LocationUsageDB>();
public static GlobalShare getInstance(){
return singleton;
}
@Override
public void onCreate() {
super.onCreate();
singleton = this;
}
}
Relevant service code:
public int onStartCommand(Intent intent, int flags, int startId) {
if(gpschanged){
Time t = new Time(Time.getCurrentTimezone());
t.setToNow();
String date = t.format("%Y/%m/%d");
mydb.insertRecord(new LocationUsageDB(foregroundTaskAppName, date, t.hour+":"+t.minute+":"+t.second, "true", "false", Double.toString(worker.getLastKnownLocationNetwork(getBaseContext()).getLatitude()), Double.toString(worker.getLastKnownLocationNetwork(getBaseContext()).getLongitude()), worker.getLastKnownLocationNetwork(getBaseContext()).getAccuracy()));
List<LocationUsageDB> contacts = mydb.getAllRecords();
Log.v("process method called", contacts.size()+"");
Process(contacts);
}
public void Process(List<LocationUsageDB> contacts){
boolean first = false;
GlobalShare gv = (GlobalShare) getApplicationContext();
for (LocationUsageDB cn : contacts) {
// String log = "Id: "+cn.id+" ,Name: " + cn.AppName + " ,Date: " + cn.Date+ " ,Time: " + cn.Time+ " ,Gps: " + cn.Gps+ " ,Network: " + cn.Network + ",latitude:" + cn.Latitude + ", lonitude:" + cn.Longitude + ", accuracy:" + cn.Accuracy;
//String name, String date(freq), String time(nothing, String gps(freq), String network(freq), float accuracy(avg)
if(first){
Log.d("processsing contacts", cn.AppName);
if(cn.Gps == "true"){
gv.processed.add(new LocationUsageDB(cn.AppName, "1", "", "1", "0", cn.Accuracy));
}
else{
gv.processed.add(new LocationUsageDB(cn.AppName, "1", "", "0", "1", cn.Accuracy));
}
Log.d("first", "add");
first = true;
}
else{
Log.d("processsing contacts", cn.AppName);
for (LocationUsageDB p : gv.processed) {
if(cn.AppName == p.AppName){
gv.processed.add(new LocationUsageDB(cn.AppName, "1", "", "0", "1", cn.Accuracy)); //change still, but dummy data to try get it working
}
else{
if(cn.Gps == "true"){
gv.processed.add(new LocationUsageDB(cn.AppName, "1", "", "1", "0", cn.Accuracy));
}
else{
gv.processed.add(new LocationUsageDB(cn.AppName, "1", "", "0", "1", cn.Accuracy));
}
}
}
}
}
The log results show everything is working correct and data is added
The "problem" i think, Activity:
public class Statistics extends Activity {
Timer timer= new Timer();
DataHandler mydb;
ListView listview;
StatisticsAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_statistics);
listview = (ListView) findViewById(R.id.listViewStatistics);
GlobalShare gv = (GlobalShare) getApplicationContext();
adapter = new StatisticsAdapter(getApplicationContext(), R.layout.customstatistics_layout, gv.processed);
adapter.notifyDataSetChanged();
listview.setAdapter(adapter);
}
The size of this gv.processed is 0... if I used Log.v
The manifest:
<application
android:name="com.example.tracksme.GlobalShare"
android:allowBackup="true"
.....
And I don't have e.g
android:process=":MyService"
what caused problems for other people.
Please help me! really stuck
Upvotes: 0
Views: 775
Reputation: 213
You mean that your service started first, then open Activity but list empty?
First, try to replace all (in both Service and Activity) GlobalShare gv = (GlobalShare) getApplicationContext();
by GlobalShare gv = GlobalShare.getInstance();
Second, check carefully again, do not start your Service in another process by remove android:process=":MyService"
.
The common reason is about the process.
Upvotes: 1
Reputation: 6289
in the activity, where you initial your "gv" , try this instead...
gv = GlogalShare.getInstance();
Upvotes: 0