Valdèse Kamdem Valdèse Kamdem - 27 days ago 13
Android Question

Start the application installation view

i'm trying to start an installationn view after download the lastest version of my application, but the application always crashing when ttring to start the new activity.




This is my Broadcast receiver

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


This my 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$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) 


Someone helps me please

Answer

You can try changing the way you are passing in the Uri of the downloaded APK. Try using the COLUMN_LOCAL_FILENAME column which holds the local path of the downloaded file to make a Uri out of it.

You code will look something like:

@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();
    }
}
Comments