我一直在努力奋斗几个小时,我的全局变量似乎在我的服务中工作,但是当我尝试从我的活动访问该变量时,它没有任何结果。Android全局变量无法在服务和活动之间工作
全局变量类:
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;
}
}
相关服务代码:
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));
}
}
}
}
}
日志结果显示一切工作正确的数据添加
“问题” 我认为,活动:
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);
}
这个gv.processed的大小为0 ...如果我用Log.v
清单:
<application
android:name="com.example.tracksme.GlobalShare"
android:allowBackup="true"
.....
而且我也没有如
android:process=":MyService"
是什么原因造成对其他人的问题。
请帮帮我!真的卡住