0
我對通知和forgroundtasks有幾個問題。首先,這是我的服務:地面服務中的通知
public class NFCUploadService extends Service implements MeasureDataPostCallback {
private static final int IMAGE_SEND_NFC_SERVICE_ID = 100;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equals(ForegroundServiceTags.STARTFOREGROUND_ACTION.getValue())) {
String email = intent.getStringExtra(Globals.KEY_EMAIL);
String password = intent.getStringExtra(Globals.KEY_PASSWORD);
String year = intent.getStringExtra(Globals.KEY_YEAR);
String month = intent.getStringExtra(Globals.KEY_MONTH);
String day = intent.getStringExtra(Globals.KEY_DATE);
String hour = intent.getStringExtra(Globals.KEY_HOUR);
String minute = intent.getStringExtra(Globals.KEY_MINUTE);
String value = intent.getStringExtra(Globals.KEY_VALUE);
String mealTime = intent.getStringExtra(Globals.KEY_MEALTIME);
String mood = intent.getStringExtra(Globals.KEY_MOOD);
File imageFile = null;
if(intent.getStringExtra(Globals.KEY_FILE_PATH) != null) {
imageFile = new File(intent.getStringExtra(Globals.KEY_FILE_PATH));
}
MeasureDataPostTask task = new MeasureDataPostTask(email, password, year, month, day, hour, minute, value, mealTime, mood, imageFile);
task.setCallback(this);
task.execute(this);
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle("Progress");
builder.setContentText("Sending Data...");
builder.setTicker("Notification!");
builder.setWhen(System.currentTimeMillis());
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setOngoing(true);
Notification notification = builder.build();
this.startForeground(IMAGE_SEND_NFC_SERVICE_ID, notification);
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onMeasureDataPostCompleted(StatusMeasureDataPost status, MeasureDataPostPOJO measureDataPostPojo) {
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle("Progress");
builder.setTicker("Notification!");
builder.setWhen(System.currentTimeMillis());
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setOngoing(true);
switch(status) {
case ERROR_CONNECTION_FAILED:
builder.setContentText("Connection Failed");
break;
case ERROR_DB_CONNECTION_FAILED:
builder.setContentText("DB Connection Failed");
break;
case ERROR_IO_EXCEPTION:
builder.setContentText("IO Exception");
break;
case ERROR_JSON_EXCEPTION:
builder.setContentText("JSON Exception");
break;
case ERROR_MEASUREDATA_ALREADY_EXISTS:
builder.setContentText("Data already exists");
break;
case ERROR_PROTOCOL_EXCEPTION:
builder.setContentText("Protocol Exception");
break;
case ERROR_WRONG_PARAMETERS:
builder.setContentText("Wrong Parameters");
break;
case MEASUREDATA_TRANSFER_SUCCESSFUL:
builder.setContentText("Transfer complete");
break;
}
Notification notification = builder.build();
this.startForeground(IMAGE_SEND_NFC_SERVICE_ID, notification);
}
}
只要我的AsyncTask(MeasureDataPostTask)還沒有完成,我想我的服務是殺不死的。這意味着我不希望用戶能夠在Android設備的通知選項卡中清除通知。
在AsyncTask完成之後(即當調用onMeasureDataPostCompleted
時)我想要更改我的通知,我想打印出結果消息並使通知可用。用戶應該能夠在AsyncTask完成但不在運行時刷新通知。我怎麼能做到這一點?
後來我還想添加一個機制,當AsyncTask由於缺少互聯網連接而失敗時,每5分鐘重複一次AsyncTask。