应用被杀死时,Android Broadcast Receiver无法正常工作 [英] Android Broadcast Receiver not working when the app is killed

查看:271
本文介绍了应用被杀死时,Android Broadcast Receiver无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用广播接收器和计时器显示间隔显示通知.它在应用程序运行时有效,但在应用程序被终止时无效.

I am trying to display notification in an interval using Broadcast Receiver and timer to display. It works when the app is running but did not work when the app is killed.

接收器外观

public class MyReceiver  extends BroadcastReceiver {
    int j;
      public void onReceive(final Context context, Intent intent) {

        // Vibrate the mobile phone
        //Declare the timer
        Timer t = new Timer();

//Set the schedule function and rate
        t.schedule(new TimerTask() {

                       @Override
                       public void run() {
                           Log.d("ME", "Notification started");

                           NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
                           mBuilder.setSmallIcon(R.drawable.ddc);
                           mBuilder.setContentTitle("My notification");
                           mBuilder.setDefaults(Notification.DEFAULT_SOUND);
                           mBuilder.setContentText("Hello World!");

                           NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                           mNotificationManager.notify(j++, mBuilder.build());

                       }

                   },
                0,
                30000);
    }
}

AndroidManifest.xml看起来像

AndroidManifest.xml Looks like

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.background.pushnotification">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver  android:name="MyReceiver"
            android:enabled="true"
            android:exported="true"
            >
            <intent-filter>
                <action android:name="com.background.myreceiver" />
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

它仅在应用运行时间隔显示通知.应用程序被杀死时,它不显示通知.我想念的是什么?

It only displays notification in an interval when app is running. It is not displaying notification when the app is killed. What I am missing?

推荐答案

应用被杀死时"不是精确的声明.我猜你的意思是当您从概述屏幕(又称为近期任务"列表)中滑动应用程序时."

"when the app is killed" is not a precise statement. I am going to guess that you mean "when you swipe away your app from the overview screen (a.k.a., recent-tasks list)".

一旦 onReceive()返回,如果您在前台没有活动并且没有正在运行的服务,则您的流程重要性将降至 Timer 就会消失.因此,您编写的代码将不可靠,因为您的过程可能会在30秒的窗口内终止.

Once onReceive() returns, if you do not have an activity in the foreground and you do not have a running service, your process importance will drop to what the documentation refers to as a "cached process". Your process is eligible to be terminated at any point. Once your process is terminated, your Timer goes away. Hence, your code as written will be unreliable, as your process might be terminated within your 30-second window.

其他可能性包括:

  • 您正在为应用程序被杀死时"做其他事情,其行为类似于设置"中应用程序屏幕上的强制停止".通常,强制停止"按钮是强制停止应用程序的唯一方法,但是偶尔设备制造商会做一些愚蠢的事情,并强制停止发生其他事情(例如,设备提供的应用程序管理器").一旦您的应用程序被强制停止,您的代码将永远不会再次运行,直到用户从主屏幕启动器图标启动您的应用程序,或者设备上的其他内容使用显式的 Intent 来启动您的组件之一.

如果设备进入睡眠状态,则只有在设备再次唤醒后,您的 Timer 才会被调用.

If the device falls asleep, your Timer will not be invoked until the device wakes up again.

这篇关于应用被杀死时,Android Broadcast Receiver无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆