我的通知会显示在自定义时间和日期中,一切正常,但当应用关闭时,通知不再显示。我已经使用AlarmManager在特定时间显示通知。
MainActivity.java
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.RingtoneManager;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlarmManager alarmManager=(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
Receiver receiver=new Receiver();
IntentFilter intentFilter=new IntentFilter("ALARM_ACTION");
registerReceiver(receiver, intentFilter);
Intent intent = new Intent("ALARM_ACTION");
intent.putExtra("param", "My scheduled action");
PendingIntent operation = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar calendar=Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.clear();
calendar.set(Calendar.DATE,26);
calendar.set(Calendar.MONTH,Calendar.MARCH);
calendar.set(Calendar.YEAR,2018);
calendar.set(Calendar.HOUR_OF_DAY, 16);
calendar.set(Calendar.MINUTE, 52);
calendar.set(Calendar.SECOND, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), operation) ;
}
private void addNotification() {
Intent intent = new Intent(this, SecondActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "ChannelID");
builder.setSmallIcon(R.drawable.ic_launcher_background);
builder.setContentTitle("Notifications Example");
builder.setContentText("This is a test notification");
builder.setContentIntent(pendingIntent);
builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
builder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
public class Receiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
addNotification();
}
}
}
和 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.arantik.test">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".Receiver"
android:enabled="true"
android:exported="true"
android:label="StartMyServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
答案 0 :(得分:1)
请勿从您的活动中调用registerReceiver(),当您关闭应用时,接收器将会死亡。您需要在Manifest中声明接收器。 并将接收器用作独立类而不是内部类。 AlarmManager将使用PendingIntent数据调用接收器。 像这样设置你的PendingIntent并将以下方法添加到你的Activity中并从onCreate中调用它,如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlarmManager alarmManager=(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(getApplicationContext(), YourReceiver.class);
PendingIntent operation = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar=Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.clear();
calendar.set(Calendar.DATE,26);
calendar.set(Calendar.MONTH,Calendar.MARCH);
calendar.set(Calendar.YEAR,2018);
calendar.set(Calendar.HOUR_OF_DAY, 16);
calendar.set(Calendar.MINUTE, 52);
calendar.set(Calendar.SECOND, 0);
registerOneTimeAlarm(operation, calendar.getTimeInMillis());
}
private void registerOneTimeAlarm(PendingIntent alarmIntent, long when) {
int SDK_INT = Build.VERSION.SDK_INT;
if (SDK_INT < Build.VERSION_CODES.KITKAT) {
alarmManager.set(AlarmManager.RTC_WAKEUP, when, alarmIntent);
} else if (Build.VERSION_CODES.KITKAT <= SDK_INT && SDK_INT < Build.VERSION_CODES.M) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, when, alarmIntent);
} else if (SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, when, alarmIntent);
}
}
此方法更好地支持新的Android限制。
将接收器添加到您的Manifest中,以便可以明确触发:
<receiver
android:name=".YourReceiver"
android:enabled="true"
/>
将addNotification()方法放在接收器类中,或者启动一个将调用此方法的服务,这不是一个长操作,所以我只是把它放在接收器中。