Android PendingIntent 带您前往已经存在的 Activity?

发布于 2024-10-27 22:03:35 字数 1246 浏览 5 评论 0原文

不太确定如何搜索这个...

我有一个以下内容,每当从队列中添加或删除作业时都会调用它,以在状态栏中放置通知:

private void showNotification()
{
    int jobsize = mJobQueue.size();
    int icon = (jobsize == 0) ? 
        android.R.drawable.stat_sys_upload_done : 
        android.R.drawable.stat_sys_upload;
    Notification notification = 
        new Notification(icon, "Test", System.currentTimeMillis());
    Intent intent = new Intent(this, FileManagerActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

    notification.flags = 
        (Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL);
    notification.setLatestEventInfo(this, 
        "Uploading to our servers",
        getString((jobsize > 0) ? 
            R.string.notification_active_transfers : 
            R.string.notification_no_transfers),
        pendingIntent);

    mNotifyManager.notify(NOTIFICATION, notification);
}

现在的行为是这样的:

  • < p>如果用户注销并点击通知,它仍然会打开一个新的 FileManagerActivity (操作!)我可以通过从我的身份验证活动开始并以自然顺序将意图传递到我的堆栈来解决这个问题,当应用程序已经运行是我遇到困难的地方。

  • 如果用户已打开 FileManagerActivity,则单击通知将在其上放置第二个实例。在这种情况下,我希望当前正在运行的 FileManagerActivity 接收焦点,而不是启动新实例。

我怎样才能得到正确的行为?

Wasn't really sure how to search for this...

I have a the following which is called whenever a job is added or removed from my queue to put a notification in the status bar:

private void showNotification()
{
    int jobsize = mJobQueue.size();
    int icon = (jobsize == 0) ? 
        android.R.drawable.stat_sys_upload_done : 
        android.R.drawable.stat_sys_upload;
    Notification notification = 
        new Notification(icon, "Test", System.currentTimeMillis());
    Intent intent = new Intent(this, FileManagerActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

    notification.flags = 
        (Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL);
    notification.setLatestEventInfo(this, 
        "Uploading to our servers",
        getString((jobsize > 0) ? 
            R.string.notification_active_transfers : 
            R.string.notification_no_transfers),
        pendingIntent);

    mNotifyManager.notify(NOTIFICATION, notification);
}

As it is now the behavior is this:

  • if the user logs out and hits the notification, it will still open a new FileManagerActivity (ops!) I could get around this by starting at my authentication activity and passing the intent up my stack in a natural order, its when the application is already running is where I have difficulties.

  • if the user already has the FileManagerActivity open clicking the notification will put a second instance over it. In this case, I want the currently running FileManagerActivity to recieve focus instead of launching a new instance.

How could I get the correct behavior?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟花肆意 2024-11-03 22:03:35

我之前通过将我的 Activity 设置为使用应用程序清单中的启动模式“singleTop”。它将使用现有的活动(如果存在)来实现所需的功能。在这种情况下, onNewIntent 将在您的活动中被调用。

您需要签入 FileManagerActivity 进行身份验证,并在用户未登录时酌情启动新活动。

I've done this before by setting my Activity to use the launch mode 'singleTop' in the Application Manifest. It will achieve the desired function, using the existing activity if one exists. In this case, onNewIntent will be called in your activity.

You'll need to check in your FileManagerActivity for authentication and start a new activity as appropriate if the user is not logged in.

倒数 2024-11-03 22:03:35

我认为添加这些后可以工作:

intent.setAction(Long.toString(System.currentTimeMillis()));

PendingIntent.FLAG_UPDATE_CUR

Intent intent = new Intent(context, MyOwnActivity.class);
intent.putExtra("foo_bar_extra_key", "foo_bar_extra_value");
intent.setAction(Long.toString(System.currentTimeMillis()));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

I think Worked when added these:

intent.setAction(Long.toString(System.currentTimeMillis()));

PendingIntent.FLAG_UPDATE_CUR

Intent intent = new Intent(context, MyOwnActivity.class);
intent.putExtra("foo_bar_extra_key", "foo_bar_extra_value");
intent.setAction(Long.toString(System.currentTimeMillis()));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文