Ich versuche, die Benachrichtigung in einem Intervall mit Broadcast Receiver und Timer anzuzeigen. Es funktioniert, wenn die App läuft, aber nicht funktioniert, wenn die App beendet wird.Android-Broadcast-Empfänger funktioniert nicht, wenn die App beendet wird
Empfänger sieht aus wie
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 Sieht aus wie
<?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>
Es nur Benachrichtigung in einem Intervall anzeigt, wenn App läuft. Es wird keine Benachrichtigung angezeigt, wenn die App beendet wird. Was ich vermisse?
Vielleicht müssen Sie WAKE_LOCK Erlaubnis hinzufügen? –
Bitte präzisieren Sie, was Sie mit "wenn die App getötet wird" meinen. – Karakuri
Beim Schließen der App vom Hintergrund. Die Benachrichtigung wird nicht empfangen –