gpt4 book ai didi

java - 待定 Intent 不是打开 Activity

转载 作者:行者123 更新时间:2023-12-02 05:40:05 25 4
gpt4 key购买 nike

我是 Android 开发新手。我可以看到股票消息“您到达办公室了吗”,但是当我单击它时,它并没有打开我的主要 Activity 。

下面是我的代码:

private class ViewUpdater implements Runnable{
private Context contextFromView;
public ViewUpdater(Context context) {
this.contextFromView = context;
}
@Override
public void run() {
CancelNotification(contextFromView, notificationId);
Random r= new Random();
notificationId =r.nextInt();
Intent intent = new Intent();
FragmentActivity activity = getActivity();
PendingIntent pi = PendingIntent.getActivity(getActivity(),1000,intent,0);
String body = "congratulations you made it today";
String title = "Office reached";
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getActivity())
.setSmallIcon(R.drawable.abc_ic_go)
.setContentTitle(title)
.setTicker("Did you reach office?")
.setContentText(body);
mBuilder.setContentIntent(pi);
nm.notify(notificationId, mBuilder.build());
timerHandler.postDelayed(this, 200000);
}
}

最佳答案

在创建 PendingIntent 时,您必须给出一个 Intent 来启动正确的 Activity。此外,FLAG_ACTIVITY_NEW_TASK 也是必需的,因为您将在现有 Activity 的上下文之外启动该 Activity(来源:Android doc)。

Intent = new Intent(getActivity(), YourActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getActivity(getActivity(),1000,intent,0);

关于java - 待定 Intent 不是打开 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24595689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com