gpt4 book ai didi

android - 来自通知的 Activity 中收到错误的 Intent (在服务中)

转载 作者:行者123 更新时间:2023-11-29 00:41:31 26 4
gpt4 key购买 nike

我有一个非常奇怪的问题。我编写了一个带有 2 个由线程调用的 SimpleNotifications 的服务,但是当我单击它们以启动 DestinationActiviy 时,我发现收到的 Intent 包含上次单击的通知的额外信息。

例如:步骤 1) 调用 SimpleNotification 2步骤 2) 调用 SimpleNotification 1步骤 3) 点击 SimpleNotification 2

结果:DestinationActivity 显示:“SimpleNotification1”

你能告诉我为什么吗?我真的很惊讶......

这是我的服务代码:

public class MyLocalService extends Service {

private final static String LOG_TAG = "MyLocalService";

private final static int MAX_NOTIFICATION_NUMBER = 10;

private final static int SIMPLE_NOTIFICATION_ID = 1;

private NotificationManager notificationManager;

private BackgroundThread backgroundThread;

private Notification notification;

private PendingIntent pIntent;
private int notificationNumber;
private PendingIntent pIntent2;
private Intent intent;
private Intent intent2;

@Override
public void onCreate() {
super.onCreate();
backgroundThread = new BackgroundThread();
backgroundThread.start();
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Log.i(LOG_TAG, "Service Created");

}

public void sendNotification1(){
notification = new Notification(R.drawable.icon,"Simple Notification1", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
intent = new Intent(this, DestinationActiviy.class);
intent.putExtra("notificationType", "Simple Notification1");
pIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
notificationNumber++;
notification.number=notificationNumber;
notification.setLatestEventInfo(this, "Simple Notification1","Simple Notification Extended", pIntent);
notificationManager.notify(1, notification);
}

public void sendNotification2(){
// Creiamo la Notification
notification = new Notification(R.drawable.icon,"Simple Notification2", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
intent2 = new Intent(this, DestinationActiviy.class);
intent2.putExtra("notificationType", "Simple Notification2");
pIntent2 = PendingIntent.getActivity(this, 0, intent2,PendingIntent.FLAG_UPDATE_CURRENT);
notificationNumber++;
notification.number=notificationNumber;
notification.setLatestEventInfo(this, "Simple Notification2","Simple Notification Extended", pIntent2);
notificationManager.notify(2, notification);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(LOG_TAG, "Service Started");
notificationNumber = 0;
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
backgroundThread.running = false;
super.onDestroy();
Log.i(LOG_TAG, "Service Destroyed");
}

@Override
public IBinder onBind(Intent arg0) {
return null;
}


private final class BackgroundThread extends Thread {
private final static long MIN_DELAY = 2000L;
private final static long MAX_RANDOM_DELAY = 10000L;
public boolean running= true;
public void run() {
Log.i(LOG_TAG, "BackgroundThread Started");
Random random = new Random();
while(running && notificationNumber<MAX_NOTIFICATION_NUMBER){
long randomDelay = MIN_DELAY + Math.abs(random.nextInt() %MAX_RANDOM_DELAY);
Log.i(LOG_TAG, "Delay is (ms) "+randomDelay);
try{
Thread.sleep(randomDelay);
}
catch(InterruptedException ie){

}
sendNotification2();
sendNotification1();
}
stopSelf();
}
}
}

服务从 MainActivity 开始:

public class LocalServiceTestActivity extends Activity {

private Intent serviceIntent;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
serviceIntent = new Intent(this,MyLocalService.class);
}

public void startLocalService(View button){
startService(serviceIntent);
}

public void stopLocalService(View button){
stopService(serviceIntent);
}
}

和 DestinationActivity:

public class DestinationActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_activity);
// Otteniamo le informazioni associate all'Intent
Bundle extras = getIntent().getExtras();
if (extras != null) {
TextView textView = (TextView) findViewById(R.id.outputView);
textView.setText(extras.getString("notificationType"));
}
}

}

我也尝试过使用相同的 Intent,使用相同的 pendingIntent 和相同的通知,反之亦然,使用不同的 Intent,pendingIntent(就像这段代码)...我不知道如何解决这个问题。请帮助我,谢谢。

最佳答案

当您在这里为第二个 Notification 创建 PendingIntent 时:

pIntent2 = PendingIntent.getActivity(this, 0, 
intent2,PendingIntent.FLAG_UPDATE_CURRENT);

您指定了 PendingIntent.FLAG_UPDATE_CURRENT 标志。因为第二个 Notification 和第一个 Notification 是“相同的”(即:它们是同一个 Activity 的 Intents),所以此标志阻止创建新的 PendingIntent。相反,它只会导致现有的 PendingIntent 被新的 Extras 修改。参见 documentation for PendingIntent .

那么您得到的是 2 个共享相同 PendingIntent 的通知。无论您选择哪一个通知,您总是会从第二个通知中获得附加功能。

关于android - 来自通知的 Activity 中收到错误的 Intent (在服务中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9048992/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com