这是通知代码
private void customNotification() {
Intent closeButton = new Intent("Download_Cancelled");
closeButton.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(getContext(), 0, closeButton, 0);
RemoteViews notificationView = new RemoteViews(getContext().getPackageName(), R.layout.notification);
NotificationManager notificationManager = (NotificationManager)getContext().getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext())
.setSmallIcon(R.drawable.music)
.setContent(notificationView)
.setChannelId(ID);
notificationView.setOnClickPendingIntent(R.id.next, pendingSwitchIntent);
notificationManager.notify(0, builder.build());
}
这是广播活动
public class NotificationBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"thanks",Toast.LENGTH_LONG).show();
}
}
这是显而易见的
<receiver android:name=".BlankFragment2$NotificationBroadcast">
<intent-filter>
<action android:name="Download_Cancelled" />
</intent-filter>
</receiver>