wrong Intent receveid in Activity from Notification Service

Put your problem here if it does not fit any of the other categories.

wrong Intent receveid in Activity from Notification Service

Postby stefanoadsl » Mon Jan 30, 2012 11:37 pm

Hi all, I'm a new entry :)

I have a really strange problem. I've write a Service with 2 SimpleNotifications called by a Thread, but when i click on them to start the DestinationActiviy i discovered that the received Intent contains the extras of the last Notification clicked.

For example: Step 1) SimpleNotification 2 is called Step 2) SimpleNotification 1 is called Step 3) Click on SimpleNotification 2

Result: DestinationActivity shows: "SimpleNotification1"

Could you tell me why? I'm really surprised about that...

The Service start from a MainActivity:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class LocalServiceTestActivity extends Activity {
  2.  
  3. private Intent serviceIntent;
  4.  
  5. @Override
  6. public void onCreate(Bundle savedInstanceState) {
  7.     super.onCreate(savedInstanceState);
  8.     setContentView(R.layout.main);
  9.     serviceIntent = new Intent(this,MyLocalService.class);
  10. }
  11.  
  12. public void startLocalService(View button){
  13.     startService(serviceIntent);
  14. }
  15.  
  16. public void stopLocalService(View button){
  17.     stopService(serviceIntent);
  18. }    
  19. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4



This is the code of my Service:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MyLocalService extends Service {
  2.  
  3. private final static String LOG_TAG = "MyLocalService";
  4.  
  5. private final static int MAX_NOTIFICATION_NUMBER = 10;
  6.  
  7. private final static int SIMPLE_NOTIFICATION_ID = 1;
  8.  
  9. private NotificationManager notificationManager;        
  10.  
  11. private BackgroundThread backgroundThread;
  12.  
  13. private Notification notification;
  14.  
  15. private PendingIntent pIntent;
  16. private int notificationNumber;
  17. private PendingIntent pIntent2;
  18. private Intent intent;
  19. private Intent intent2;
  20.  
  21. @Override
  22. public void onCreate() {
  23.         super.onCreate();
  24.         backgroundThread = new BackgroundThread();
  25.         backgroundThread.start();
  26.         notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  27.         Log.i(LOG_TAG, "Service Created");
  28.  
  29. }
  30.  
  31. public void sendNotification1(){
  32.         notification = new Notification(R.drawable.icon,"Simple Notification1", System.currentTimeMillis());            
  33.         notification.flags |= Notification.FLAG_AUTO_CANCEL;
  34.         intent = new Intent(this, DestinationActiviy.class);
  35.         intent.putExtra("notificationType", "Simple Notification1");
  36.         pIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);    
  37.         notificationNumber++;
  38.         notification.number=notificationNumber;
  39.         notification.setLatestEventInfo(this, "Simple Notification1","Simple Notification Extended", pIntent);
  40.         notificationManager.notify(1, notification);
  41. }
  42.  
  43. public void sendNotification2(){
  44.         // Creiamo la Notification
  45.         notification = new Notification(R.drawable.icon,"Simple Notification2", System.currentTimeMillis());            
  46.         notification.flags |= Notification.FLAG_AUTO_CANCEL;
  47.         intent2 = new Intent(this, DestinationActiviy.class);
  48.         intent2.putExtra("notificationType", "Simple Notification2");
  49.         pIntent2 = PendingIntent.getActivity(this, 0, intent2,PendingIntent.FLAG_UPDATE_CURRENT);    
  50.         notificationNumber++;
  51.         notification.number=notificationNumber;
  52.         notification.setLatestEventInfo(this, "Simple Notification2","Simple Notification Extended", pIntent2);
  53.         notificationManager.notify(2, notification);
  54. }
  55.  
  56. @Override
  57. public int onStartCommand(Intent intent, int flags, int startId) {
  58.         Log.i(LOG_TAG, "Service Started");
  59.         notificationNumber = 0;
  60.         return super.onStartCommand(intent, flags, startId);
  61. }
  62.  
  63. @Override
  64. public void onDestroy() {
  65.         backgroundThread.running = false;
  66.         super.onDestroy();
  67.         Log.i(LOG_TAG, "Service Destroyed");
  68. }
  69.  
  70. @Override
  71. public IBinder onBind(Intent arg0) {
  72.         return null;
  73. }
  74.  
  75.  
  76. private final class BackgroundThread extends Thread {
  77.         private final static long MIN_DELAY = 2000L;            
  78.         private final static long MAX_RANDOM_DELAY = 10000L;
  79.         public boolean running= true;
  80.         public void run() {
  81.                 Log.i(LOG_TAG, "BackgroundThread Started");                    
  82.                 Random random = new Random();
  83.                 while(running && notificationNumber<MAX_NOTIFICATION_NUMBER){
  84.                         long randomDelay = MIN_DELAY + Math.abs(random.nextInt() %MAX_RANDOM_DELAY);
  85.                         Log.i(LOG_TAG, "Delay is (ms) "+randomDelay);
  86.                         try{
  87.                                 Thread.sleep(randomDelay);
  88.                                 }
  89.                         catch(InterruptedException ie){
  90.  
  91.                         }
  92.                         sendNotification2();
  93.                         sendNotification1();
  94.                 }
  95.                 stopSelf();
  96.         }
  97. }
  98. }
Parsed in 0.052 seconds, using GeSHi 1.0.8.4


And DestinationActivity:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class DestinationActivity extends Activity {
  2.  
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5.     super.onCreate(savedInstanceState);
  6.     setContentView(R.layout.notification_activity);
  7.     // Otteniamo le informazioni associate all'Intent
  8.     Bundle extras = getIntent().getExtras();
  9.     if (extras != null) {
  10.         TextView textView = (TextView) findViewById(R.id.outputView);
  11.         textView.setText(extras.getString("notificationType"));
  12.     }
  13. }
  14.  
  15. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


I've tryed also with same Intent, with same pendingIntent with same notification or viceversa with different Intent, pendingIntent (like this code)... I don't know how fix that :( Please help me, Thanks.
stefanoadsl
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Jan 30, 2012 11:31 pm

Top

Re: wrong Intent receveid in Activity from Notification Serv

Postby Phyll » Tue Jan 31, 2012 3:47 am

Hi stefanoadsl,

I wrote something and then decided to start over. Under certain conditions the activity you are trying to start will only be brought to the surface and not started as a new task, showing you the state it was in from the last time it was started. The singletask launch mode for instance or at least one of the flags for the intents will appear to behave as you describe. Since I can't see either of these things happening, I would suggest, it must be something like that that I can't see.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 20 guests