我对通知和前台任务有几个问题。首先,这是我的服务:
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。
答案 0 :(得分:2)
首先,您可以从startForeground()
致电onStartCommand()
。
要重复AsyncTask
,您应该设置一个重新发送STARTFOREGROUND_ACTION
意图的闹钟。