javaandroidxmlandroid-studioandroid-4.1-jelly-bean

Using intent to launch a method within the same class or new class


I guess I sorta learned how to creating a noitification in android studio from their website and have a simple understanding how it runs through. Mebe more copy and paste with a little modification =\ . But I don't see anything on how to launch a method within the same activity/class using an intent.

Per my copy and paste code from android studio:

public void showNotification()
{
    Intent emptyIntent = new Intent(this, JellyNotify.class);
    TaskStackBuilder backStack = TaskStackBuilder.create(this)
            .addNextIntent(emptyIntent)
            .addParentStack(JellyNotify.class);

    PendingIntent emptyPending = backStack.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT);

    Notification buildNoti = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.jelly)
            .setContentTitle("Feeling Jelly")
            .setContentText("Tap to launch Jar of Jelly")
            .setContentIntent(emptyPending)
            .build();

    NotificationManager manageNotification = (NotificationManager)
            getSystemService(Context.NOTIFICATION_SERVICE);

    manageNotification.notify(0,buildNoti);
}

I guess what I am trying to say is, that I am trying launch and activity or new intent, mebe even a method within the class or new activity that I have created, when the user clicks on the ongoing notification. So my brain feels fried for searching this past week. To show what I have came up with I have found sites such as:

android-er.blogspot

Clickable Notification - StackOverflow


Solution

  • Try this,

    Update your intent as follows ,

    public void showNotification()
    {
    NotificationManager manageNotification = (NotificationManager)
            getSystemService(Context.NOTIFICATION_SERVICE);
    
    Notification buildNoti = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.jelly)
            .setContentTitle("Feeling Jelly")
            .setContentText("Tap to launch Jar of Jelly")
            .setContentIntent(emptyPending)
            .build();
    
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,new Intent(this, Activity_you_needto_launch.class), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
    
    manageNotification.notify(0,buildNoti);
    }