0

我想我从他们的网站上学会了如何在 android studio 中创建通知,并简单了解它是如何运行的。Mebe 更多的复制和粘贴,稍加修改 =\ 。但我没有看到任何关于如何使用意图在同一活动/类中启动方法的任何内容。

根据我从 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);
}

我想我想说的是,当用户单击正在进行的通知时,我正在尝试启动和活动或新意图,甚至是我创建的类或新活动中的方法。所以我的大脑在过去一周的搜索中感到很煎熬。为了展示我的想法,我找到了以下网站:

android-er.blogspot

可点击通知 - StackOverflow

4

1 回答 1

1

尝试这个,

更新您的意图如下,

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);
}
于 2015-04-06T17:31:13.887 回答