我试图在使用广播接收器和计时器显示的间隔中显示通知。它在应用程序运行时工作,但在应用程序被终止时无法工作。Android广播接收器在应用程序中断时不工作
接收机看起来像
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>
它应用程序正在运行时的间隔中仅显示通知。它在应用程序被杀时不显示通知。我错过了什么?
也许您需要添加WAKE_LOCK权限? –
请更具体地说明“当应用程序被杀时”的含义。 – Karakuri
从后台关闭应用程序。通知未收到 –