Android Question Apk Installation fails with intent

walterf25

Expert
Licensed User
Longtime User
Hi all, i'm having a small issue using an intent to install an updated apk file which gets downloaded from a server.

The apk gets downloaded just fine, i have verified that.

i receive the following error
** Activity (main) Resume **
** Service (downloadupdate) Create **
** Service (downloadupdate) Start **
apk size: 12785167
response code: 200
true
main_installnewapk (B4A line: 759)
StartActivity(newintent)
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///storage/emulated/0/Download/Observer.apk typ=application/vdn.android.package-archive flg=0x20000 }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1861)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1534)
at android.app.Activity.startActivityForResult(Activity.java:4124)
at android.app.Activity.startActivityForResult(Activity.java:4071)
at android.app.Activity.startActivity(Activity.java:4395)
at android.app.Activity.startActivity(Activity.java:4363)
at anywheresoftware.b4a.keywords.Common.StartActivity(Common.java:698)
at ifims.observer.main._installnewapk(main.java:3470)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:179)
at anywheresoftware.b4a.keywords.Common$5.run(Common.java:996)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6914)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

I have searched the forums about this issue and found several posts that suggest the following code.

B4X:
 Sub InstallnewApk()
Dim newintent As Intent
    newintent.Initialize(newintent.ACTION_VIEW, "file://"&File.Combine(File.DirRootExternal&"/Download", "Observer.apk"))
    newintent.SetType("application/vdn.android.package-archive")
    StartActivity(newintent)
End Sub

as you can see at the beginning of the logs, the size of the apk file is 12.7Mb, and the response code from the server is OK, I'am bothered at the fact that on the threads i've read, this code works for them, but i can't figure out why it won't work for me, is there anything i should be adding to the manifest file perhaps?

I'am using a service to download the apk file and i'am using the OKHttpUtils library, i'm using B4A version 6.8 on a samsung Galaxy Active tab SM-T360 running Android version 5.1.1

Any advice will be greatly appreciated guys.

Thanks,
Walter
 

walterf25

Expert
Licensed User
Longtime User
Can you install the downloaded APK using a file manager app?

Try to use B4A-Bridge. Does it work properly (it sends this intent to install apps)?
Yes it works if I go to the location where the apk was downloaded to and if I click on it, i can see the installation dialog.
And yes, if I use B4A-Bridge it works as well,i can see the installation dialog too.

Regards,
Walter
 
Upvote 0

udg

Expert
Licensed User
Longtime User
StartActivity(newintent) android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///storage/emulated/0/Download/Observer.apk typ=application/vdn.android.package-archive flg=0x20000 }

It seems that the Intent is executed correctly but that OS can't find a service named "newintent". Did you rename it? Did you place in it code like:
B4X:
  If StartingIntent.Action = "android.intent.action.PACKAGE_REPLACED" Then

If you like, you can have a look at the source of my lib AppUpdating.

udg
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Note that you are not using File.Combine correctly, though it is not related to this issue.

It should be:
B4X:
File.Combine(File.DirRootExternal, "Download/Observer.apk")

Where is the code that copies the APK file?
Hi Erel, here's the code in the Service that is used to download the apk file from the server.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim out As OutputStream
End Sub

Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)
    DownloadApk
End Sub

Sub Service_Destroy

End Sub

Sub DownloadApk
    Dim hc As OkHttpClient
    hc.InitializeAcceptAll("hc")
    Dim req As OkHttpRequest
    req.InitializeGet("http://xxxxxxxxxxxxxxxxxx")
    hc.Execute(req, 1000)
End Sub
Sub hc_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
   
    Log("apk size: " & Response.ContentLength)
    Log("response code: " & Response.StatusCode)
    Response.GetAsynchronously("res", File.OpenOutput(File.DirRootExternal&"/Download", "Observer.apk", False), True, TaskId)
End Sub
Sub res_StreamFinish (Success As Boolean, TaskId As Int)
    Log($" ${Success}"$)
    If Success Then
        CallSubDelayed(Main, "InstallnewApk")
    End If
End Sub
Sub hc_ResponseError (Response As OkHttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
    Log($"Error: ${Response.ErrorResponse}, ${Reason}, ${StatusCode}"$)
    If Response <> Null Then Response.Release
End Sub

Thanks,
Walter
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
It seems that the Intent is executed correctly but that OS can't find a service named "newintent". Did you rename it? Did you place in it code like:
B4X:
  If StartingIntent.Action = "android.intent.action.PACKAGE_REPLACED" Then

If you like, you can have a look at the source of my lib AppUpdating.

udg
No, i have not replaced anything, but i don't have a service named "newintent", i'll give this a try when i get home today.

Thanks for the tip.

Regards,
Walter
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
I have resolved the issue guys, thanks for all the help, it turns out i had misspelled the intent.Type
newintent.SetType("application/vdn.android.package-archive")

it's supposed to be
newintent.SetType("application/vnd.android.package-archive")

Now my other question is, how can I re-start the app automatically once the app is updated, when it finishes updating the app shuts down, so how can i re-launch the app automatically,

I've tried running another service named RestartActivity and check for the StartingIntent, but it doesn't seem to work for me.

Any advise on this?

Regards,
Walter
 
Upvote 0

ronell

Well-Known Member
Licensed User
Longtime User
Now my other question is, how can I re-start the app automatically once the app is updated, when it finishes updating the app shuts down, so how can i re-launch the app automatically,
i think you need to create new thread for this question
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub DownloadApk
 Dim hc As OkHttpClient
 hc.InitializeAcceptAll("hc")
 Dim req As OkHttpRequest
 req.InitializeGet("http://xxxxxxxxxxxxxxxxxx")
 hc.Execute(req, 1000)
End Sub
1. Why aren't you using OkHttpUtils2?
2. Don't create a new OkHttpClient every request. A single client should be created and reused.
3. Why do you use InitializeAcceptAll with a http url?
 
Upvote 0
Top