请考虑以下代码。 我正在尝试打印与被关闭的通知的ID相对应的“已被关闭的通知#[id]”。 为什么如果我关闭通知#1,则同时打印“通知#1被关闭”和“通知#2被关闭”? 如果我关闭通知#2,“通知#0被取消”,为什么会打印并给出没有对应于id的值0的通知,为什么会显示?
我在哪里做错了? 纠正方法是什么?
MainActivity.java
if (intent.hasExtra(EXTRA_MATCH)) {
// clone the match object to prevent a concurrentModificationException
Match originalMatch = intent.getParcelableExtra(EXTRA_MATCH);
try {
match = (Match) originalMatch.clone();
FileUtils.writeMatchToFile(context, match);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
scoresFile = intent.getParcelableExtra(EXTRA_SCORES_FILE);
if (scoresFile != null) {
FileUtils.writeScoresFile(context, scoresFile);
}
} else {
return;
}
OnCancelBroadcastReceiver.java
1
00:00:01.876 --> 00:00:02.709
<v Instructor>We can go back now</v>
2
00:00:02.709 --> 00:00:05.042
to our web server checklist.
3
00:00:06.410 --> 00:00:08.722
1- We've already seen better ways to organise our code
4
00:00:08.722 --> 00:00:11.545
into reusable pieces with modules,
5
00:00:11.545 --> 00:00:13.315
2- we've seen ways to deal with files,
6
00:00:13.315 --> 00:00:15.940
both synchronous and asynchronous,
7
00:00:15.940 --> 00:00:16.773
and buffers,
8
00:00:16.773 --> 00:00:18.325
both the built-in Node one
9
00:00:18.325 --> 00:00:20.380
and the ES6 buffers,
10
00:00:20.380 --> 00:00:22.485
and we've seen a way to deal with work
activity_main.xml
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alert(1);
alert(2);
}
});
}
void alert(int id)
{
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.ic_launcher_foreground);
mBuilder.setContentTitle("Notification Example");
mBuilder.setContentText("Notification #" + Integer.toString(id));
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
Intent onCancelIntent = new Intent(this, OnCancelBroadcastReceiver.class);
PendingIntent onDismissPendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, onCancelIntent, 0);
mBuilder.setDeleteIntent(onDismissPendingIntent);
onCancelIntent.putExtra("id", id);
sendBroadcast(onCancelIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// notificationID allows you to update the notification later on.
int notificationID = id;
mNotificationManager.notify(notificationID, mBuilder.build());
}
}
strings.xml
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class OnCancelBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Do your logic here
int id = intent.getIntExtra("id", 0);
Toast.makeText(context, "Notification #" + Integer.toString(id) + " has been dismissed", Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/notify" />
</RelativeLayout>
顺便说一下,NotificationCompat.Builder在更高版本的Android中不推荐使用(从哪个Android版本开始?)。 还有其他选择吗?