【问题标题】:Intent filter activity and proper show of notifications意图过滤器活动和正确显示通知
【发布时间】:2011-04-12 13:16:25
【问题描述】:

我正在编写媒体文件共享应用程序。

现在分为两部分:

<application>
        <activity android:name="FileUploader"
                  android:label="..."
                  >
            <intent-filter>
                <action android:name="android.intent.action.SEND"/>
                <data android:mimeType="*/*"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

    <service android:name=".FileUploaderService" />


    </application>

FileUploader 是一个应该从其他活动(例如画廊或相机)启动的活动。 FileUploaderService 是一个服务,它实际上将文件上传到 HTTP 服务器。它执行几个 HTTP 请求。

现在用户可以在上传过程中按下主页按钮并返回主页屏幕。 在这种情况下,我希望有一个包含当前上传阶段详细信息的通知,当点击时它将返回用户到活动。

我是这样创建的:

    int icon = R.drawable.icon;
    CharSequence tickerText = "Uploading File";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);

    Context context = getApplicationContext();
    CharSequence contentTitle = "Uploading File";
    CharSequence contentText = "Upload in progress. Stage #" + i;

    Intent notificationIntent = new Intent(this, FileUploader.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(context, contentTitle, contentText,
            contentIntent);

    mNM.notify(R.layout.main, notification);

问题是,当我在上传过程中按下主页按钮时 - 从库启动的 FileUploader 活动实例变为“停止”,当我点击通知时 - 启动了第二个 FileUploader 活动实例。

那么,当点击通知而不是启动新的通知时,有什么方法可以“返回”到第一个活动?

我尝试了以下方法,但没有一个在这里工作:

  1. 在 FileUploader.onStop() 方法中调用 finish() - 活动在从纵向切换到横向时关闭。

  2. 为 AndroidManifest.xml 中的 FileUploader 活动设置 android:launchMode="singleTask" - 在此之后我的应用程序开始出现在最近的应用程序列表中,这是不可接受的,因为它允许从那里启动 FileUploader 活动,因为我上面提到它只能通过从 Gallery/Camera 发送 android.intent.action.SEND Intent 来启动。

  3. 使用与启动第一个实例相同的意图 - 仍然创建第二个实例并且日志中出现以下警告:

    04-12 17:01:17.041: WARN/ActivityManager(1289): startActivity 从非活动上下文中调用;强制 Intent.FLAG_ACTIVITY_NEW_TASK 用于:Intent { act=android.intent.action.SEND typ=image/jpeg cmp=.FileUploader bnds=[0,628][480,723](有附加功能)}

我也试过换行:

    Context context = getApplicationContext();

    Context context = this; // FileUploadServiceContext

但这无济于事。

嗯。我想,也许我发起了错误的活动。可能我应该为我的 FileUploader 启动“父”活动。 例如:如果我从 Gallery 启动 FileUploader,然后点击 Home,然后再次从 Home screen 启动 Gallery - 我会返回到我的 Activity。所以我想知道,也许我需要从通知中启动画廊/相机。但是有两个问题:

  1. 如何确定我的活动的父项到底是什么?

  2. 如果父级被系统关闭了怎么办?

【问题讨论】:

    标签: android


    【解决方案1】:

    一旦用户导航离开,您就不能依赖“返回”到任何活动。操作系统可以并且很可能会破坏它,因此将没有任何东西可以返回。我建议您在 Notification 的 Intent 中添加额外内容,并重新创建 Activity 以在启动时显示这些额外内容中的适当数据。

    【讨论】:

    • 我明白了。但在这种情况下如何: 1. 当我使用主页按钮退出时杀死第一个 Activity 实例,但在更改布局时不杀死(例如)? 2. 阻止我的活动显示在最近的应用程序列表中?
    【解决方案2】:

    嗯,我发现实现这一点的唯一方法是:

    android:excludeFromRecents="true" android:launchMode="singleTask"
    

    这不是我想要的 100% 的结果(例如,我无法在恢复后返回“父”应用程序),但总比没有好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多