我一直在努力工作几个小时,我的全局变量似乎在我的服务中起作用,但是当我尝试从我的活动中访问该变量时,它没有给出任何结果。
全球变种类:
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"
是什么给其他人带来了问题。
请帮帮我!真的卡住了
答案 0 :(得分:1)
您的意思是您的服务首先启动,然后打开活动但列表为空?
首先,尝试替换所有(在服务和活动中)GlobalShare gv = (GlobalShare) getApplicationContext();
按GlobalShare gv = GlobalShare.getInstance();
其次,请仔细检查,不要通过删除android:process=":MyService"
在其他进程中启动您的服务。
常见的原因是关于这个过程。
答案 1 :(得分:0)
在活动中,您初始化“gv”,请尝试这样做......
gv = GlogalShare.getInstance();