如何避免空的意图,我在服务已经得到了什么?(how to avoid null in intent

2019-10-17 10:14发布

我有一个在后台运行的服务。 他返回START_STICKY因为它监听的一些事件,这个事件(来自传感器的数据)出现非常快。 我有些情况下我的服务被杀害:我有一个空的意图,当我的服务已杀死,因为他的过程中消失根据文档它可以发生。

http://developer.android.com/reference/android/app/Service.html#onStart%28android.content.Intent,%20int%29

我想知道如何避免它。 任何帮助将不胜感激。

更新

    private void startForeground() {
    String title = "Warning";//getString(R.string.note_title);
    String text = "App still working";//getString(R.string.note_msg);

    Intent intent = new Intent(this, DriverActivity.class);

    PendingIntent pending = PendingIntent.getActivity(this, 0, intent, 0);


    Bitmap bitmap = BitmapFactory.decodeResource(
            getResources(),
            R.drawable.ic_launcher);
    Notification.Builder builder = new Notification.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(bitmap)
            .setContentTitle(title)
            .setContentText(text)
            .setContentIntent(pending);

    Notification notification = builder.getNotification();
    notification.flags |= Notification.FLAG_NO_CLEAR;
    startForeground(NOTIFICATION_ID, notification);
}

Answer 1:

杉杉您需要确定为什么你的服务终止。 两个最可能的动机是:

  • 服务trows一个例外,是由Android终止
  • 服务被由Android终止,因为是为长时间(通常为30分钟至1小时之间)运行而不被表示为前景服务

例外这类问题,你可以伊斯利识别看着logcat的。 修正将取决于报道的原因。

这个前景 ,你可以通过设置您的服务中使用,以解决前景:

        startForeground(message, notification);

在您服务。

问候。



文章来源: how to avoid null in intent that I have got in Service?