Tuesday, July 17, 2012

How to Download and Install .apk file from server programmatically


Hi folks,
If you are planning to develop android application which is responsible to download .apk file from server(non market app,Enterprise distribution) and then install it on to the device ,this post will help you
so here is the choice.

1. Download Manager: this can be done using the Download Manager available from 2.3.3 your newely downloaded file goes in Downloads application as shown below,you can install your newly download .apk by clicking on it .



Android system just ask for permission to install  your .apk with the help of Package Installer . This seems that after downloading their is no controls of your application to install it rather than going to downloads and manually install the .apk .I am not explaing the the code for Download Manager coz this post is all about "How to download .apk from server and  install it programatically" :-).
So  here is the 2nd way

2. 
a. First download .apk file using simple http connection and store it on external storage area.


private void downloadapk(){
    try {
        URL url = new URL("http://www.efimoto.com/nilesh/HelloWorldProject.apk");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.connect();

        File sdcard = Environment.getExternalStorageDirectory();
        File file = new File(sdcard, "filename.apk");

        FileOutputStream fileOutput = new FileOutputStream(file);
        InputStream inputStream = urlConnection.getInputStream();

        byte[] buffer = new byte[1024];
        int bufferLength = 0;

        while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
            fileOutput.write(buffer, 0, bufferLength);
        }
        fileOutput.close();
        this.checkUnknownSourceEnability();
        this.initiateInstallation();

    } catch (MalformedURLException e) {
            e.printStackTrace();
    } catch (IOException e) {
            e.printStackTrace();
    }
    }


b. Now instantiate installing process.


 private void installApk(){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(new File("/sdcard/filename.apk"));
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        startActivity(intent);
    }

c.  Give uses-permission in application menifest file.


 <uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS"/>
    <uses-permission android:name="android.permission.INTERNET"/>
d. Ensure that your "Unknown source " check box must enable.





I have been created simple UI for this  having a button when button clicked it downloads apk after that installation process appears . If unknown source is disable you must got "Install blocked"

















That's all, you have successfully downloaded and installed your .apk from server.
feel free to query @:    bpsingh@pmtsl.onmicrosoft.com
enjoy coding :-)

Saturday, February 25, 2012

Hash Key Mismatch Problem in Facebook Android App



Hi,Friends 
Working on my android project i faced some problem during integrating Facebook in my app.
This post will help you to fix issue that i have faced.

Problem:
I implemented Facebook SSO(single Sign On) in my Android app.
All the time throughout the development, in emulator everything worked just
fine,problem occurred when i try to login with my app onto device.
It showed HASH KEY MISMATCH ERROR and failed to login.
I got following error:
11-02 16:41:31.660: D/Facebook-authorize(13194): Login failed: invalid_key:Android key mismatch. Your key "########your hash key####" does not match the allowed keys specified in your application settings. Check your application settings at http://www.facebook.com/developers


Solution:
At last after long time i resolve this problem.
Suppose your hash key is "#### your hash key####".
This key is encrypt by the device so you have to tell this key to Facebook(in your application which you have created).
for this hit this URL https://www.facebook.com/developers and edit your app.
Now you have to copy this hash key in your Native App section then save it.
Here is the screen shot

That's all
Now enjoy your uninterrupted Facebook integration in your app.

happy coding
have a good day.

Next: Customized list View