0
我在一個應用程序中需要每5秒鐘執行一次後臺作業以從服務器檢索圖像,當作業完成時,如果返回某些內容,需要更新我的UI中的ImageView
。 我使用AlarmManager
來完成這項工作,但在作業完成後不知道如何更新UI。 另外我想知道這是做這件事的最好方法,還是我必須實施某種服務。如何使用AlarmManager更新組件?
這是我的onCreate
方法,其中,I初始化我的UI組件和AlarmManager
與PendingIntent
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swtSIGNAL = (Switch) findViewById(R.id.swtSIGNAL);
snackView = swtSIGNAL;
swtSIGNAL.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked) {
mSetSignalTask = new SetSignalAsync(progressDialog, snackView, true);
mSetSignalTask.execute();
} else {
mSetSignalTask = new SetSignalAsync(progressDialog, snackView, false);
mSetSignalTask.execute();
}
}
});
swtALARM=(Switch)findViewById(R.id.swtALARM);
snackView=swtALARM;
swtALARM.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked) {
mSetAlarmTask = new SetAlarmAsync(progressDialog, snackView,true);
mSetAlarmTask.execute();
} else {
mSetAlarmTask = new SetAlarmAsync(progressDialog,snackView,false);
mSetAlarmTask.execute();
}
}
});
imgFOTO=(ImageView) findViewById(R.id.imgFOTO);
progressDialog = new ProgressDialog(this);
progressDialog.setCancelable(false);
progressDialog.isIndeterminate();
callListener = new EndCallListener();
telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
telephonyManager.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
}
這是我的setAlarm
方法,其激活AlarmManager:
public void setAlarm(){
manager=(AlarmManager)getSystemService(ALARM_SERVICE);
int interval=5000;
manager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),interval,pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
這是AlarmReceiver
類從BroadcastReceiver
延伸:
public class AlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Working", Toast.LENGTH_SHORT).show();
}
}
最後,這是從服務器獲取圖像的方法;我正在使用AsyncTask
來凍結應用程序。
public class GetImageAsync extends AsyncTask<Void,Void,Bitmap>{
@Override
protected Bitmap doInBackground(Void... params) {
try{
URL url=new URL("http://192.168.0.16/HouseSecure/SECImages/example.jpg");
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream=connection.getInputStream();
Bitmap image= BitmapFactory.decodeStream(inputStream);
connection.disconnect();
return image;
}catch (MalformedURLException e){
Log.e(LOG_TAG, e.getMessage());
return null;
}catch(IOException ex){
Log.e(LOG_TAG,ex.getMessage());
return null;
}
}
@Override
protected void onPostExecute(Bitmap image) {
imgFOTO.setImageBitmap(image);
}
}
是你的AlarmReceiver?您是否忘記在上面的AlarmReceiver類中包含該行? – user1088166