启动应用程序安装视图

时间:2016-11-07 11:42:30

标签: android android-intent android-broadcastreceiver

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

这是我的广播接收器

depedencies.dot

这是我的logcat

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

有人帮助我

3 个答案:

答案 0 :(得分: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();
    }
}

答案 1 :(得分:0)

没有可以处理意图的活动。在intent-filter manifest

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>
-(void)roundCorners:(UIRectCorner)corners radius:(CGFloat)radius
{
    CGRect bounds = _IBtxtField.bounds;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds
                                                   byRoundingCorners:corners
                                                         cornerRadii:CGSizeMake(radius, radius)];

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = bounds;
    maskLayer.path = maskPath.CGPath;

    _IBtxtField.layer.mask = maskLayer;

    CAShapeLayer*   frameLayer = [CAShapeLayer layer];
    frameLayer.frame = bounds;
    frameLayer.path = maskPath.CGPath;
    frameLayer.strokeColor = [UIColor blackColor].CGColor;
    frameLayer.fillColor = nil;

    [_IBtxtField.layer addSublayer:frameLayer];
}

-(void)roundCornersRadius:(CGFloat)radius
{
    [self roundCorners:(UIRectCornerTopLeft|UIRectCornerTopRight | UIRectCornerBottomLeft) radius:radius];
}

答案 2 :(得分:0)

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 当您开始新活动时,如果没有从活动开始, intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK“是必需的。

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