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