【发布时间】:2020-01-23 12:51:45
【问题描述】:
现在我有一个创建通知的前台服务:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Intent broadcastIntent = new Intent(this, NotificationReceiver.class);
broadcastIntent.putExtra("toastMessage", "Test message");
PendingIntent actionIntent = PendingIntent.getBroadcast(this, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
String channelId = "fcm_default_channel";
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.notification_uploading);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.animation_wifi)
.setContentTitle("Upload Photos")
.setContentText("1 photo uploading")
.setContentIntent(pendingIntent)
.addAction(R.drawable.icon_back_black_arrow, "Cancel", actionIntent);
startForeground(1111, notificationBuilder.build());
它有一个调用我的广播接收器的取消按钮。如何停止广播接收器类中的前台服务?
public class NotificationReceiver extends BroadcastReceiver {
private static final String TAG = "NotificationReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("toastMessage");
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
//When the foreground notification cancel button is clicked, this method is called
}
}
【问题讨论】:
标签: android broadcastreceiver foreground-service