我有重绘的问题。我有接收信息的应用程序(在字符串中),当我想更新它时,它不会改变界面中显示的任何值。但是如果我旋转手机,它会改变值。我有更新值的服务,但我不知道如何更改界面。有人知道吗?
public class MainActivity extends Activity {
PrinterObject printer;
private IntentFilter filter;
static Boolean VISUAL_UPDATES_ENABLED = true;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
filter = new IntentFilter();
filter.addAction("update");
printer = new PrinterObject("134.188.204.155");
printer.updateAllData();
//Set the name of the printer
TextView printername = (TextView)findViewById(R.id.printer_name);
printername.setText(printer.printerName);
printerName = printer.printerName;
String[] message = new String[printer.printerStateReasons.size()];
message = printer.printerStateReasons.toArray(message);
//Set the printer status:
TextView printerstatus = (TextView)findViewById(R.id.tvPrinter_status_msg);
printerstatus.setText(printer.printerState);
printerStatus = printer.printerState;
...
protected void onResume() {
super.onResume();
startService(new Intent(MainActivity.this,
Service_refresh.class));
if(reciever == null) {
reciever = new myBroadcastReceiver();
}
registerReceiver(reciever, new IntentFilter("SendMessage"));
}
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//set the correct intent filters to listen to
IntentFilter filter = new IntentFilter();
filter.addAction("update");
this.registerReceiver(reciever, filter);
VISUAL_UPDATES_ENABLED = true;
//start/resume the refresh service
startService(new Intent(MainActivity.this,
Service_refresh.class));
}
在另一个类中,我有 Service_refresh 来更改值:
...
public class Service_refresh extends Service {
private static Timer timer = new Timer();
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
startService();
}
@Override
public void onDestroy() {
super.onDestroy();
}
public void startService() {
timer.scheduleAtFixedRate(new mainTask(), 0, 1000);
}
private class mainTask extends TimerTask {
public void run() {
ArrayList<String> temp = new ArrayList<String>();
ArrayList<PrinterObject> printerObjects = new ArrayList<PrinterObject>();
// create a new printer object with params[0] (which is an ip/web
// adress)
// populate the printer variables
SharedPreferences appSharedPrefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
// create a gson objecr
Gson gson = new Gson();
// retrieve the current printerObjects stored in your
// sharedpreferences
String json = appSharedPrefs.getString("MyObject", "");
// create an arraylist to store the data in.
ArrayList<PrinterObject> tempArray = new ArrayList<PrinterObject>();
try {
// get each printerobject in the json array
JSONArray array = new JSONArray(json);
for (int i = 0; i < array.length(); i++) {
JSONObject row = array.getJSONObject(i);
// convert the json to a printerObject class
PrinterObject obj = gson.fromJson(row.toString(),
PrinterObject.class);
// add the object to the temporary array.
tempArray.add(obj);
}
// if everything succeeded clear the original array
// and put the new data in it
printerObjects.clear();
printerObjects = tempArray;
} catch (Exception e) {
Log.e("TEST_TEST", "TEST_TEST update borken");
}
int i = 0;
for (PrinterObject printert : printerObjects) {
if (printert.updateAllData()) {
temp.add(printert.printerName + " update successfull");
printerObjects.set(i, printert);
}
// failed
else {
printert = new PrinterObject(printert.printerIpOrWebAddress);
if (printert.updateAllData()) {
temp.add(printert.printerName
+ " updated after session refresh");
printerObjects.set(i, printert);
} else {
temp.add(printert.printerName + " failed to update");
}
}
i++;
}
appSharedPrefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
Editor prefsEditor = appSharedPrefs.edit();
gson = new Gson();
json = gson.toJson(printerObjects);
prefsEditor.putString("MyObject", json);
prefsEditor.commit();
Intent ix = new Intent("update").putExtra("update", "update");
getApplicationContext().sendBroadcast(ix);
}
}
}
有人知道如何更改界面吗?