我想我从他们的网站上学会了如何在 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 中的方法。所以我的大脑因过去一周的搜索而感到煎熬。为了展示我的想法,我找到了以下网站:
最佳答案
试试这个,
按如下方式更新您的 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/