我試圖讓一個intentservice像我一樣在一個文件中完美保存位置服務。android studio intentservice沒有在應用程序之外運行
當文件出現在目錄中時,服務會彈出通知。 這很好地在一個活動內工作,但不關閉時。
這裏是我的代碼:
public class NotifyService extends IntentService {
public NotifyService() {
super("NotifyService");
}
@Override
protected void onHandleIntent(Intent intent) {
File alerte = new File(Environment.getExternalStorageDirectory() +
File.separator +"SmartCollecte"+File.separator+ "ALERTE"+File.separator+ "IN"+File.separator+"alerte");
if(alerte.exists()){
createNotification();
}
else{}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void createNotification() {
Intent intent = new Intent(this, NotificationReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
Notification noti = new Notification.Builder(this)
.setContentTitle("nouvelle collecte " + "[email protected]")
.setContentText("une nouvelle collecte est disponible").setSmallIcon(R.drawable.notification)
.setContentIntent(pIntent)
.setSmallIcon(R.drawable.notification)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
}
我不能找到我缺少的是什麼... 感謝您的幫助
我的是什麼版本你 –
測試的Android仍然在5.1現在 – filoman
你怎麼火的意圖服務? – Vyacheslav