Sunday 15 January 2012

android - Start a new activity on notification icon click -


I can already show an icon in the notification bar as shown in the code below. When the user clicks on this icon, I want to start a new activity ( myclass ), but I do not know how to do it. Where should I be kept?

  enhances public square notification activity (Override public void'm alarm manager; @Create (bundle saved Instansstet) {Suprknkret (saved Instensstet); setContentView (R. layout.activity_notification); Am = (AlarmManager) getSystemService (context .ALARM_SERVICE); setRepeatingAlarm ();} public void Setprkashitalarm () {intent intent = new intent (this, TimeAlarm.class); pending pending vertical = anticipated = Lmbitintentkgetbodakast (This, 0, intent, perpendicular Per provider. FLAG_CANCEL_CURRENT); Am.setrepeating (AlarmManager.RTC_WAKEUP, System.currentTimeMillis (), (20 * 1000), pendingIntent); System.out.println ( "Alaram calling ...");}} public class BootUpReciever Broadcast Expands the receiver {Override public null at Notification Manager nm; @Receive (Reference Reference, intent to intent) {System.out.println ("in detail ...."); Nm = (Notification Manager) Reference .getSystemService (Reference.NETIFICATION_SERVICE); = Extreme from "Lokesh"; Charaxa Message = "Notification Test ..."; Pending Intent ContentIntent = Pending.teactivity (Reference, 0, New Intent (), 0); Notification Notification = New Notification (Ardoable.Create_con, "Notification Test ...", System. Content-time Millise ()); Notif.setLatestEventInfo (reference, message, content content); Nm.notify (1, notif); If ((intent.getAction ()! = Null) & amp; (intent.getAction (.) Equals ("android.intent.action.BOOT_COMPLETED"))) {System.out.println ("In broadcast receiver ... .. "); I = new intent (reference, main activity category); I.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); Context.startActivity (i); }}}}    

There should be an intention in the pending intention of giving notification to the builder When the user clicks the notification, he will launch an activity. You need to intend to launch one of your activities:

  Intended = The new intent (this, as a result, the class);   

Then you create a pending notification using this intent:

  pending result representative PendingIntent = stackbuilder.getPendingIntent (0, pending INNT.FLAG_UPDATE_CURRENT );   

And then you create a notification using it when a user clicks your notification, then ResultActivity will be launched.

The code snippet:

  NotificationCompat.Builder mbuilder = new NotificationCompat.Builder (it) .setSmallIcon (R.drawable.notification_icon) .setContentTitle ( "My Information") .setContentText ("Hello World!"); // Creates a clear intention for an activity in your app that means the result = new intent (this, as a result, class); // Stack Builder object will include an artificial back stack // current activity // ensures that navigating backward by activity reaches the home screen from your computer Task StackBuilder StackBuilder = Task StackBuilder. Create (this); // Adds the stack back to the intent (but not the self itself) stackBuilder.addParentStack (ResultActivity.class); // Stack Stack Bailer adds the intent to start activity on top. AddNextIntent (as a result); Pending prominent result padding = stackBuilder.getPendingIntent (0, pending INNT.FLAG_UPDATE_CURRENT); MBuilder.setContentIntent (resultPendingIntent); Notification Manager Account Manager = (Notification Manager) getSystemService (reference .NETIFICATION_SERVICE); // MID allows you to update the information later on. MNotificationManager.notify (MID, Mbuilder Build ());   

For a great explanation, see the detailed article in Developer Docs:

No comments:

Post a Comment