【问题标题】:android studio intentservice not running outside of applicationandroid studio intentservice 没有在应用程序之外运行
【发布时间】: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);

}

}

我不能只找到我缺少的东西...... 感谢您的帮助

【问题讨论】:

标签: java android intentservice


【解决方案1】:

此解决方案仅适用于 preor 26 (Android O) SDK:

您必须使用WakefulBroadcastReceiver 启动您的意图服务 使用这种方式,您可以使用警报管理器管理您的服务,防止被系统杀死(唤醒锁)并删除 Activity 依赖项。

此外,我建议使用它来实现您的想法。

https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html

【讨论】:

  • 这似乎是做我想做的事情的正确方法!谢谢 Vyacheslav 我找到了一个使用警报管理器的好例子:mindstick.com/Articles/1627/…
猜你喜欢
  • 1970-01-01
  • 2012-05-15
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多