Android 从通知中接收到的活动中的错误意图(在用)

Android 从通知中接收到的活动中的错误意图(在用),android,multithreading,android-service,android-pendingintent,android-notifications,Android,Multithreading,Android Service,Android Pendingintent,Android Notifications,我有一个很奇怪的问题。我编写了一个服务,其中包含两个由线程调用的SimpleNotifications,但是当我单击它们来启动DestinationActiviy时,我发现收到的意图包含上次单击的通知的额外内容 例如: 步骤1)调用简化2 步骤2)调用简化1 步骤3)单击简化2 结果:DestinationActivity显示:“简化1” 你能告诉我为什么吗?我真的很惊讶 这是我的服务代码: public class MyLocalService extends Service { priva

我有一个很奇怪的问题。我编写了一个服务,其中包含两个由线程调用的SimpleNotifications,但是当我单击它们来启动DestinationActiviy时,我发现收到的意图包含上次单击的通知的额外内容

例如: 步骤1)调用简化2 步骤2)调用简化1 步骤3)单击简化2

结果:DestinationActivity显示:“简化1”

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

这是我的服务代码:

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();
        }
}
}
和命运活动:

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);
}    
}
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"));
    }
}

}

我也尝试过同样的意图,同样的pendingent,同样的通知,或者相反,不同的意图,pendingent(就像这个代码)。。。我不知道如何解决这个问题。请帮助我,谢谢。

当您在此处为第二个通知创建挂起内容时:

pIntent2 = PendingIntent.getActivity(this, 0, 
                intent2,PendingIntent.FLAG_UPDATE_CURRENT);     
您可以指定
pendingent.FLAG\u UPDATE\u CURRENT
标志。因为第二个通知和第一个通知是“相同的”(即:它们是相同活动的意图),所以此标志阻止创建新的PendingEvent。相反,它只会导致现有PendingEvent使用新PendingEvent中的附加项进行修改。看


因此,您得到的是两个通知,它们共享相同的挂起内容。无论您选择哪一个通知,您都会从第二个通知中获得额外信息。

此处信息不足。服务在哪里启动?我已编辑了我的问题。现在清楚了吗?:-)