扩展文件:下载扩展文件后,启动Intent to next活动

时间:2013-04-17 17:32:31

标签: android apk-expansion-files

我有以下代码可以成功下载扩展文件,但我不知道在哪里添加我的意图,以便expansion file is downloaded我可以start my activity.

请帮忙。

try {
            Intent launchIntent = ExpansionFilesActivity.this.getIntent();
            Intent intentToLaunchThisActivityFromNotification = new Intent(ExpansionFilesActivity.this,
                ExpansionFilesActivity.this.getClass());
            intentToLaunchThisActivityFromNotification.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intentToLaunchThisActivityFromNotification.setAction(launchIntent.getAction());

            if (launchIntent.getCategories() != null) {
            for (String category : launchIntent.getCategories()) {
                intentToLaunchThisActivityFromNotification.addCategory(category);
            }
            }

            // Build PendingIntent used to open this activity from
            // Notification
            PendingIntent pendingIntent = PendingIntent.getActivity(ExpansionFilesActivity.this, 0,
                intentToLaunchThisActivityFromNotification, PendingIntent.FLAG_UPDATE_CURRENT);
            // Request to start the download
            int startResult = DownloaderClientMarshaller.startDownloadServiceIfRequired(this, pendingIntent, SampleDownloaderService.class);

            if (startResult != DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED) {
            // The DownloaderService has started downloading the
            // files,
            // show progress
            initializeDownloadUI();
            return;
            } // otherwise, download not needed so we fall through to
              // starting the movie
        } catch (NameNotFoundException e) {
            Log.e(LOG_TAG, "Cannot find own package! MAYDAY!");
            e.printStackTrace();
        }
        }

2 个答案:

答案 0 :(得分:1)

Android开发者指南中的示例如下:

@Override
public void onCreate(Bundle savedInstanceState) {
    // Check if expansion files are available before going any further
    if (!expansionFilesDelivered()) {
        // Build an Intent to start this activity from the Notification
        Intent notifierIntent = new Intent(this, MainActivity.getClass());
        notifierIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                                Intent.FLAG_ACTIVITY_CLEAR_TOP);
        ...
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                notifierIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        // Start the download service (if required)
        int startResult = DownloaderClientMarshaller.startDownloadServiceIfRequired(this,
                        pendingIntent, SampleDownloaderService.class);
        // If download has started, initialize this activity to show download progress
        if (startResult != DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED) {
            // This is where you do set up to display the download progress (next step)
            ...
            return;
        } // If the download wasn't necessary, fall through to start the app
    }
    startApp(); // Expansion files are available, start the app
}

首先你看它是否可用。如果没有在服务中下载并在完成时向用户发出有关数据可用的通知。

答案 1 :(得分:0)

我正在探索另一种设计:

  1. 当newstate == STATE_COMPLETED时,从public void onDownloadStateChanged(int newState)内调用startApp()。出于某些原因,我调用了两次onDownloadState()。我添加了一个标志downloadStateCompleted以避免双重调用startApp()。

  2. 清除STATE_COMPLETED上的通知。

  3. 用户的体验可能更好,因为下载完成后应用程序继续(进入startApp())。但我不确定这种方法是否与下载库的设计和使用方式相悖。在扩展文件文档中,我没有看到任何从onDownloadStateChanged内调用startApp()的迹象。