我有一個非常奇怪的問題。我已經寫了一個帶有2個SimpleNotifications的服務,由一個Thread調用,但是當我點擊它們來啓動DestinationActiviy時,我發現收到的Intent包含了最後點擊的通知的附加內容。錯誤意圖receveid在通知活動中(服務中)
例如: 步驟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"));
}
}
}
我也嘗試過相同的意圖,具有相同的pendingIntent具有相同的通知或viceversa與不同的Inte nt,pendingIntent(像這樣的代碼)...我不知道如何解決這個問題。請幫助我,謝謝。
這裏沒有足夠的信息。服務在哪裏開始? – Jivings 2012-01-29 11:16:36
我編輯了我的問題。現在清楚嗎? :-) – Aerox 2012-01-29 20:45:10