简体   繁体   English

当应用程序被杀时,Android广播接收器无法正常工作

[英]Android Broadcast Receiver not working when the app is killed

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. 它在应用程序运行时有效,但在应用程序被杀死时无效。

Receiver looks like 接收器看起来像

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 Looks like AndroidManifest.xml看起来像

<?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 (aka, recent-tasks list)". 我猜你的意思是“当你从概览屏幕(也就是最近任务列表)中移除你的应用程序时”。

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". 一旦onReceive()返回,如果您没有活动在前台并且您没有正在运行的服务,您的流程重要性将降至文档所称的“缓存流程”。 Your process is eligible to be terminated at any point. 您的流程有资格随时终止。 Once your process is terminated, your Timer goes away. 一旦您的进程终止,您的Timer就会消失。 Hence, your code as written will be unreliable, as your process might be terminated within your 30-second window. 因此,您编写的代码将不可靠,因为您的进程可能会在30秒的窗口内终止。

Other possibilities include: 其他可能性包括:

  • You are doing something else for "when the app is killed" that behaves like "Force Stop" does on your app's screen in Settings. 您正在为“应用程序被杀”时执行其他操作,其行为类似于“强制停止”,在“设置”中的应用程序屏幕上执行此操作。 Normally, that "Force Stop" button is the only way to force-stop an app, but occasionally device manufacturers do something stupid and make force-stop something that happens from other things (eg, a device-supplied "application manager"). 通常,“强制停止”按钮是强制停止应用程序的唯一方法,但偶尔设备制造商会做一些愚蠢的事情并强制停止从其他事情发生的事情(例如,设备提供的“应用程序管理器”)。 Once your app is force-stopped, your code will never run again, until the user launches your app from the home screen launcher icon or something else on the device uses an explicit Intent to start one of your components. 一旦您的应用程序被强制停止,您的代码将永远不会再次运行,直到用户从主屏幕启动器图标启动您的应用程序或设备上的其他内容使用显式的Intent启动您的某个组件。

  • If the device falls asleep, your Timer will not be invoked until the device wakes up again. 如果设备处于睡眠状态,则在设备再次唤醒之前不会调用Timer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当应用程序被杀死时,互联网广播接收器不工作 - Internet broadcast receiver is not working when app is killed 当应用程序在奥利奥中被杀死时,广播接收器不工作为什么? - Broadcast Receiver not working when the App killed in Oreo Why? 当应用程序在 android 版本 9 中被杀死时收听呼叫广播接收器 - listen to call broadcast receiver when app gets killed in android version 9 当应用程序被杀死时使广播接收器活着 - Make Broadcast Receiver Alive when app is killed 应用程序被杀死时广播接收器不工作 - Broadcast receiver is not working when application is killed 广播接收器即使在app被用户杀死后仍然工作 - Broadcast receiver working even after app is killed by user 如果应用程序在 android oreo 及更早版本中被杀死,则显式广播接收器不会启动 - Explicit Broadcast receiver dont launch if app is killed in android oreo and earlier 应用被杀死时,广播接收器未在Oreo上收听 - Broadcast receiver not listening on Oreo when app gets killed 手动终止应用程序时如何获取广播接收器? - How to get broadcast receiver when an app is killed manually? 即使应用程序被杀死,也可以从广播接收器运行并获取事件 - Run and get event from Broadcast Receiver even when app is killed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM