1

我有下面的代码可以成功下载扩展文件,但我不知道在哪里添加我的 Intent 以便一旦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();
        }
        }
4

2 回答 2

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
}

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

于 2013-06-06T12:04:25.330 回答
0

我正在探索另一种设计:

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

  2. 在 STATE_COMPLETED 上清除通知。

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

于 2014-12-17T00:07:01.827 回答