【问题标题】:Android getIntent() returns the first intentAndroid getIntent() 返回第一个意图
【发布时间】:2012-08-19 22:18:48
【问题描述】:

我开发了一个应用程序来下载视频文件并将其存储在 SD 卡中。在此过程中,我还使用NotificationManager 将下载的进度和状态更新为状态栏通知。

我的类名为DownloadTask.java 扩展了AsyncTask。所以在这里我使用onProgressUpdate() 方法更新进度,其中我使用NotificationManager 来达到目的。一切都像一个魅力,除了下载完成后我想点击通知打开特定的视频文件。所以这就是我所做的:

mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

int icon = android.R.drawable.stat_sys_download_done;
long when = System.currentTimeMillis();

mNotification = new Notification(icon, "", when);
mContentTitle_complete = mContext.getString(R.string.download_complete);
notificationIntent = new Intent(mContext,OpenDownloadedVideo.class);
notificationIntent.putExtra("fileName", file);
mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);
mNotification.setLatestEventInfo(mContext, file, mContentTitle_complete, mContentIntent);
mNotification.flags = Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(NOTIFICATION_ID, mNotification);

请注意,fileNameNOTIFICATION_ID 在我的情况下是独一无二的。

ActivityOpenDownloadedVideo.java 通过以下方式打开文件:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {  
        fileName = getIntent().getExtras().getString("fileName");
        Intent i = new Intent(Intent.ACTION_VIEW);
        File videoFileToPlay = new File(Environment.getExternalStorageDirectory()+"/MyFolder"+"/"+fileName);
        i.setDataAndType(Uri.fromFile(videoFileToPlay), "video/*");
        startActivity(i);
        finish();
    } catch(Exception e) {
        //
    }
}

因此,当我第一次下载视频并单击通知时,将打开相应的视频文件。但是下次我下载另一个视频时,点击通知,下载的第一个文件将再次打开。

这是因为 getIntent 内的 OpenDownloadedVideo 返回第一个创建的 Intent 而不是最新的。我该如何纠正这个问题?

另外,请注意,当我下载多个视频时存在问题场景,例如如果我下载了五个不同的视频文件并且状态栏中有五个通知。每次点击通知都会打开同一个文件。

【问题讨论】:

    标签: android android-intent notifications android-activity


    【解决方案1】:
    /**
     * Override super.onNewIntent() so that calls to getIntent() will return the
     * latest intent that was used to start this Activity rather than the first
     * intent.
     */
    @Override
    public void onNewIntent(Intent intent){
        super.onNewIntent(intent); // Propagate.
        setIntent(intent); // Passing the new intent to setIntent() means this new intent will be the one returned whenever getIntent() is called.
    }
    

    【讨论】:

      【解决方案2】:

      其实你只需要用 PendingIntent.FLAG_UPDATE_CURRENT 创建 PendingIntent,像这样:

      mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      

      【讨论】:

        【解决方案3】:

        来自Android Activity.onNewIntent() documentation

        请注意,getIntent() 仍然返回原始 Intent。您可以使用 setIntent(Intent) 将其更新为这个新的 Intent。

        所以当你得到新的 Intent 时,你需要将它显式设置为 Activity Intent。

        【讨论】:

        • 另外,要检索你必须重写 onNewIntent() 的新意图,即使你使用 setIntent(),调用 getIntent() 也会返回旧意图
        • 这是一个很好的答案。我需要基本上使用 setIntent 或调用 onNewIntent() 的超级方法来确保 getIntent 返回最新的意图。
        【解决方案4】:

        @Alex 和@Codinguser,感谢您的回复。非常感激。但是,我找到了一个对我有用的不同答案。在为Intent 创建PendingIntent 时,将唯一值传递给它。就我而言,我正在这样做:

        mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0); 
        

        但现在我使用的是NOTIFICATION_ID,因为它是独一无二的。我已将上述调用更改为:

        mContentIntent = PendingIntent.getActivity(mContext, NOTIFICATION_ID,
                                                   notificationIntent, 0);
        

        就是这样,它有效。

        我在问题中找到了一些相关信息 Mulitple Instances of Pending Intent

        【讨论】:

          【解决方案5】:

          如果您将 OpenDownloadedVideo 活动设为 SINGLE_TOP 并覆盖 onNewIntent 会怎样?

          【讨论】:

          • 我该怎么做??抱歉,我对此很陌生。
          • 不知道有没有用。在清单编辑器中,您为 Activity 启动模式添加 SINGLE_TOP。然后覆盖 Activity 的 onNewIntent() 并在其中放入与 onCreate 中相同的代码
          • 我已经创建了OpenDownloadedVideo 活动SINGLE_TOP 并且在活动内部我已经覆盖了onNewIntent 以及您的建议....但是当活动启动或重新启动时似乎没有进入onNewIntent 方法。
          • @Bopanna 也许它没有重新启动而是暂停然后恢复?
          【解决方案6】:

          在您打算启动的活动中。 添加(覆盖)以下函数:

          @Override
          protected void onNewIntent(final Intent intent) {
                  super.onNewIntent(intent);
                  this.setIntent(intent);
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-01-20
            • 2015-05-08
            • 2017-11-28
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多