Tuesday, 9 July 2013

Install / Uninstall Apk Or Launch App From another App Android

Install / Uninstall Apk Or Launch App From another App Android



Hi Guys Toady We will see how to install another app( SD card or local storage ) from our app in android.
we can also launch any other app from our app. 






Here is code Example 
activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_image" >

    <Button
        android:id="@+id/LaunchButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/InstallButton"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        android:background="@drawable/patched_button"
        android:text="Launch Existing App"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/InstallButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="10dp"
        android:background="@drawable/patched_button"
        android:text="Install Apk"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/UnInstallButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/LaunchButton"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        android:background="@drawable/patched_button"
        android:text="Uninstall Existing App"
        android:textColor="@android:color/white" />

</RelativeLayout>


MainActivity>java


package com.example.sampleapp;

import java.io.File;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

       private Button instalBtn;
       private Button launchBtn;
       private Context context;
       private Button UnInstallBtn;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              initializeAndSetClick();
       }

       private void initializeAndSetClick() {
              context = MainActivity.this;
              instalBtn = (Button) findViewById(R.id.InstallButton);
              launchBtn = (Button) findViewById(R.id.LaunchButton);
              UnInstallBtn = (Button) findViewById(R.id.UnInstallButton);

              instalBtn.setOnClickListener(this);
              UnInstallBtn.setOnClickListener(this);
              launchBtn.setOnClickListener(this);
       }

       @Override
       public void onClick(View v) {
              switch (v.getId()) {
              case R.id.InstallButton:
                     /**
                      * Environment Provides access to environment variables.
                      * .getExternalStorageDirectory() Gets the Android external storage
                      * directory. This directory may not currently be accessible if it
                      * has been mounted by the user on their computer, has been removed
                      * from the device, or some other problem has happened. You can
                      * determine its current state with getExternalStorageState().
                      *
                      */

                     String fileName = Environment.getExternalStorageDirectory()
                                  + "/Safety For Kids.apk";

                     Intent intent = new Intent(Intent.ACTION_VIEW);
                     intent.setDataAndType(Uri.fromFile(new File(fileName)),
                                  "application/vnd.android.package-archive");
                     startActivity(intent);

                     break;
              case R.id.LaunchButton:
                     /**
                      * getPackageManager() Return PackageManager instance to find global
                      * package information.
                      *
                      * getLaunchIntentForPackage("") Return a "good" intent to launch a
                      * front-door activity in a package, for use for example to
                      * implement an "open" button when browsing through packages. The
                      * current implementation will look first for a main activity in the
                      * category CATEGORY_INFO, next for a main activity in the category
                      * CATEGORY_LAUNCHER, or return null if neither are found.
                      *
                      * Throws PackageManager.NameNotFoundException if a package with the
                      * given name can not be found on the system.
                      *
                      */

                     try {
                           Intent LaunchiIntent = getPackageManager()
                                         .getLaunchIntentForPackage(
                                                       "com.nityaa.learningtimesolutions");
                           startActivity(LaunchiIntent);
                     } catch (ActivityNotFoundException e) {
                           Toast.makeText(context,
                                         "Bad package Name : Sorry Packgae Name not found ", 1)
                                         .show();
                     }
                     break;

              case R.id.UnInstallButton:

                     try {
                           Uri packageURI = Uri.parse("com.nityaa.learningtimesolutions");
                           /**
                            * Uri.parse("com.tinytapps.safety"); Creates a Uri which parses
                            * the given encoded URI string.
                            *
                            * Intent.ACTION_DELETE Activity Action: Delete the given data
                            * from its container. Input: getData() is URI of data to be
                            * deleted.
                            */
                           Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,
                                         packageURI);
                           startActivity(uninstallIntent);
                     } catch (Exception e) {
                           // TODO Auto-generated catch block
                           Toast.makeText(context,
                                         "Bad package Name : Sorry Packgae Name not found ", 1)
                                         .show();
                     }

                     break;

              default:
                     break;
              }

       }
}



and No special Permissions or anything Special Not require in AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sampleapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.sampleapp.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


and now Here is our favorite thing that you can download complete Source code example  

Happy Coddddding :)