繁体   English   中英

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

[英]Android Broadcast Receiver not working 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看起来像

<?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>

它仅在应用程序运行时显示间隔中的通知。 应用程序被杀时,它不会显示通知。 我错过了什么?

“当应用被杀时”并不是一个准确的陈述。 我猜你的意思是“当你从概览屏幕(也就是最近任务列表)中移除你的应用程序时”。

一旦onReceive()返回,如果您没有活动在前台并且您没有正在运行的服务,您的流程重要性将降至文档所称的“缓存流程”。 您的流程有资格随时终止。 一旦您的进程终止,您的Timer就会消失。 因此,您编写的代码将不可靠,因为您的进程可能会在30秒的窗口内终止。

其他可能性包括:

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

  • 如果设备处于睡眠状态,则在设备再次唤醒之前不会调用Timer

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM