【问题标题】:Using intent to launch a method within the same class or new class使用意图在同一类或新类中启动方法
【发布时间】:2015-04-07 01:31:13
【问题描述】:

我想我从他们的网站上学会了如何在 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

Clickable Notification - StackOverflow

【问题讨论】:

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


    【解决方案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);
    }
    

    【讨论】:

    • 抱歉这么晚回复,这确实解决了我的问题。非常感谢。
    • 欢迎,如果对您有帮助请采纳。那么将来可能会帮助其他人
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    • 2012-08-10
    相关资源
    最近更新 更多