我正在尝试在5秒内显示我的应用启动的通知。经过一些调试后,我发现BroadcastReceiver
从未被调用。这就是我所拥有的:
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.seppe.momentum7">
<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=".HomeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AndroidDatabaseManager"></activity>
<receiver
android:name=".BroadcastManager"
android:label="BroadcastReceiverAux">
<intent-filter>
<action android:name="com.example.seppe.momentum7" /> --!!--!!--
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
</manifest>
HomeActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener( onNavigationItemSelectedListener);
boolean alarm = (PendingIntent.getBroadcast(this, 0, new Intent("ALARM"), PendingIntent.FLAG_NO_CREATE) == null);
Log.d(TAG, "onCreate: alarm = "+ alarm);
if(alarm){
AlarmManager alarme = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, 5);
Intent itAlarm = new Intent(".DISPLAY_NOTIFICATION");
itAlarm.setClass(this, HomeActivity.class);
Log.d(TAG, "onCreate: intent = " + itAlarm);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,100,itAlarm,PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarme.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
else {
alarme.setTime(calendar.getTimeInMillis());
}
}
}
BroadcastManager.java:
public class BroadcastManager extends BroadcastReceiver {
private static final String TAG = "BroadcastManager";
public static final String CHANNEL_1_ID = "channel1";
public static final String CHANNEL_2_ID = "channel2";
private NotificationManagerCompat notificationManager;
private static Context mContext;
@Override
public void onReceive(Context context, Intent intent) {
createNotificationChannels();
mContext = context;
notificationManager = NotificationManagerCompat.from(mContext);
Notification notification = new NotificationCompat.Builder(mContext, CHANNEL_1_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("notification")
.setContentText("this is a notification")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.build();
notificationManager.notify(1, notification);
}
private void createNotificationChannels() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel1 = new NotificationChannel(
CHANNEL_1_ID,
"channel 1",
NotificationManager.IMPORTANCE_HIGH
);
channel1.setDescription("this is channel 1");
NotificationChannel channel2 = new NotificationChannel(
CHANNEL_2_ID,
"channel 2",
NotificationManager.IMPORTANCE_HIGH
);
channel2.setDescription("this is channel 2");
NotificationManager manager = mContext.getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel1);
manager.createNotificationChannel(channel2);
}
}
}
所以问题是:
我认为问题是:
我尝试过的事情:
如果您需要更多信息或其他意见,请随时发表评论。