【问题标题】:Start the application installation view启动应用程序安装视图
【发布时间】:2016-11-07 11:42:30
【问题描述】:

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


这是我的广播接收器

**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);
        }
    }
};**

这是我的日志猫

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$1@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$1.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) 

请有人帮助我

【问题讨论】:

  • 检查您是否在您的AndroidManifest.xml 上添加了CheckAppUpdateActivity
  • 是的,我的 AndroidManifest.xml* 中有 ChecAppUpdateActivity

标签: android android-intent android-broadcastreceiver


【解决方案1】:

您可以尝试更改您在下载的 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();
    }
}

【讨论】:

【解决方案2】:

没有可以处理意图的活动。在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>

【讨论】:

  • 使用ACTION_VIEW时需要为activity设置intent-filter。
【解决方案3】:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 当你开始一个新的活动时,如果不是从一个活动开始,intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK" 是需要的。

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

【讨论】:

  • 我使用 registerReceiver()
  • 这是我注册它的地方 ```` IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE); registerReceiver(downloadReceiver, filter);
  • 然后使用第二个参数作为你的 registerReceiver 中的意图过滤器
  • 一步一步调试你的应用,找出崩溃的地方
  • 我已经调试过了,当我调用 startActivity(installIntent); 在接收器中时它崩溃了
猜你喜欢
  • 1970-01-01
  • 2013-06-02
  • 1970-01-01
  • 2021-07-03
  • 1970-01-01
  • 1970-01-01
  • 2018-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多