CERCA
PER MODELLO
FullScreen Chatbox! :)

Utente del giorno: carotix con ben 2 Thanks ricevuti nelle ultime 24 ore
Utente della settimana: 9mm con ben 11 Thanks ricevuti negli ultimi sette giorni
Utente del mese: 9mm con ben 34 Thanks ricevuti nell'ultimo mese

Visualizzazione dei risultati da 1 a 2 su 2
Discussione:

(Intent e pendingIntent) tra Notifiche e Activity

Se questa discussione ti è stata utile, ti preghiamo di lasciare un messaggio di feedback in modo che possa essere preziosa in futuro anche per altri utenti come te!
  1. #1
    Baby Droid


    Registrato dal
    Jul 2011
    Messaggi
    38
    Smartphone
    Samsung Galaxy S3 LTE GT-i9305

    Ringraziamenti
    6
    Ringraziato 1 volta in 1 Post
    Predefinito

    (Intent e pendingIntent) tra Notifiche e Activity

    Salve a tutti, mi chiedevo il motivo per cui pur creando notifiche diverse ma che inviano entrambe il proprio Intent alla stessa Activity non vengono interpretati correttamente dall'Activity ricevente. Ovvero...se partono 2 notifiche, l'intent che arriva all'activity sia che clicco su una notifica sia che clicco sull'altra è lo stesso. Eppure ho creato 2 funzioni differenti di invio nella quale creo per ciascuna Notification, Intent e PendingIntent diversi.

    Ho notato che l'intent che riceve l'activity è dell'ultima notifica creata, quindi se parte la Notification1 e poi la Notification2, sia che clicco una o che clicco l'altra entrambi inviano il pendingIntent dell'ultima (in questo caso Notification2)

    P.S. gli ID delle notifiche sono diversi, l'unica cosa che hanno in comune è il notificationManager.

    Vi lascio comunque il codice da testare delle rispettive 3 classi, così potete fare le prove anche voi stessi.

    MainActivity: LocalServiceTestActivity

    codice:
    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);
    }    
    }
    Service: MyLocalService

    codice:
    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();
            }
    }
    }
    E infine l'Activity di destinazione: DestinationActivity

    codice:
    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"));
        }
    }
    
    }
    Qualcuno sa dirmi dove sbaglio?

    Grazie mille

  2.  
  3. #2
    Senior Droid L'avatar di maurilios


    Registrato dal
    Dec 2009
    Messaggi
    328

    Ringraziamenti
    7
    Ringraziato 20 volte in 19 Posts
    Predefinito

    Non vorrei sbagliarmi, ho visto il codice molto velocemente, ma mi sembra che l'intent che passi al PendingIntent sia sempre lo stesso in etrambi i metodi (sendNotification1() e sendNotification2()) e cioè

    codice:
    intent = new Intent(this, DestinationActiviy.class);

Tag per questa discussione

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire risposte
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Torna su
Privacy Policy