【发布时间】:2017-08-30 23:29:44
【问题描述】:
我正在尝试让 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 " + "test@gmail.com")
.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 上
-
如何触发 Intent 服务?
标签: java android intentservice