启动应用安装视图

Start the application installation view

我尝试在下载最新版本的应用程序后启动安装视图,但在尝试启动新版本时应用程序总是崩溃 activity。


这是我的广播接收器

**private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        //check if the broadcast message is for our Enqueued download
        long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
        if (downloadReference == referenceId) {
            Log.v(LOG_TAG, "Downloading of the new app version complete");
            //start the installation of the latest version
            Intent installIntent = new Intent(Intent.ACTION_VIEW);
            installIntent.setDataAndType(downloadManager.getUriForDownloadedFile(downloadReference), "application/vnd.android.package-archive");
            installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(installIntent);
        }
    }
};**

这是我的logcat

Process: com.astech.android.webxbettingsystem, PID: 17476
              java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10 pkg=com.astech.android.webxbettingsystem (has extras) } in com.astech.android.webxbettingsystem.ui.activity.CheckAppUpdateActivity@1282c32
                  at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:932)
                  at android.os.Handler.handleCallback(Handler.java:815)
                  at android.os.Handler.dispatchMessage(Handler.java:104)
                  at android.os.Looper.loop(Looper.java:207)
                  at android.app.ActivityThread.main(ActivityThread.java:5737)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
               Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://downloads/my_downloads/572 typ=application/vnd.android.package-archive flg=0x10000000 }
                  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1809)
                  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1523)
                  at android.app.Activity.startActivityForResult(Activity.java:3968)
                  at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
                  at android.app.Activity.startActivityForResult(Activity.java:3920)
                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:856)
                  at android.app.Activity.startActivity(Activity.java:4259)
                  at android.app.Activity.startActivity(Activity.java:4227)
                  at com.astech.android.webxbettingsystem.ui.activity.CheckAppUpdateActivity.onReceive(CheckAppUpdateActivity.java:55)
                  at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:922)
                  at android.os.Handler.handleCallback(Handler.java:815) 
                  at android.os.Handler.dispatchMessage(Handler.java:104) 
                  at android.os.Looper.loop(Looper.java:207) 
                  at android.app.ActivityThread.main(ActivityThread.java:5737) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 

请有人帮助我

没有 activity 可以处理意图。在 manifest 中使用 intent-filter 作为您的 activity

    <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>

            <data
                android:host="your-link.com"
                android:scheme="http"/>
        </intent-filter>

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 当您开始新的 activity 时,如果不是从 activity 开始, intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK" 是需要。

您有两个 Intent 过滤器;你只需要一个。如果您通过 registerReceiver() 注册 BroadcastReceiver, 仅使用作为该 API 调用的第二个参数的 IntentFilter —— 不要在清单中也放置一个元素.

您可以尝试更改在下载的 APK Uri 中传递的方式。尝试使用包含下载文件本地路径的 COLUMN_LOCAL_FILENAME 列从中生成 Uri

您的代码将类似于:

@Override
public void onReceive(Context context, Intent intent) {
    long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);


    if (downloadReference == referenceId) {
        DownloadManager.Query query = new DownloadManager.Query();
        query.setFilterById(downloadReference);
        Cursor cursor = (DownloadManager) getSystemService(Context.DOWNLOAD_MANAGER).query(query);


        if (cursor.moveToFirst()
                && cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
                        == DownloadManager.STATUS_SUCCESSFUL) {
            String filePath cursor.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));


            Log.v(LOG_TAG, "Downloading of the new app version complete");
            //start the installation of the latest version
            Intent installIntent = new Intent(Intent.ACTION_VIEW);
            installIntent.setDataAndType(Uri.parse("file://" + filePath), "application/vnd.android.package-archive");
            installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(installIntent);
        }


        cursor.close();
    }
}