java - 使用 Intent 在同一个类或新类中启动方法

标签 java android xml android-studio android-4.1-jelly-bean

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

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

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

android-er.blogspot

Clickable Notification - StackOverflow

最佳答案

试试这个,

按如下方式更新您的 Intent ,

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);
}

关于java - 使用 Intent 在同一个类或新类中启动方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29476088/

相关文章:

java - 如何将字符串转换为 GZIP Base64 字符串?

java - Roblox 论坛、网页抓取应用程序 (Android) 问题

Java 向上转换与向下转换

android - Proguard 不再适用于 Retrofit

java - java.io.StringReader.<init> 处的线程 "main"java.lang.NullPointerException 中出现异常(来源未知)

使用 xml2js 数组元素名将 json 转换为 xml

java - socket.accept() 不执行

java - 如何使用带有 json 的 Map 从数据库设置 map latlng 值?

java - 如何使用 DOM 删除 XML 文档的根节点

java - 更改微调器第一行中的文本颜色