This Android Application uninstall
prior version of client from the device. It checks for the newer
version of client on the device and launch it. There is usage of
onActivityResult method which shows how to wait till previous Intent
is getting completed (returning its result).
import android.app.Activity; import android.os.Bundle; import android.net.Uri; import android.content.ComponentName; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; public class PreCachingIssueApp extends Activity { private final static int UNINSTALL_REQUEST_CODE = 0; private final static String m_sPackageToUninstall = "com.test"; //old client to be uninstall //client installed from market place private final static String m_sPackageToLaunch = "com.google.android.CalculatorCheck"; private final static String m_sClassToLaunch = "com.google.android.CalculatorCheck.Calculator"; //wap url to be opened - just an example set google.com private final static String m_sWebUrl = "http://google.com"; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Check for the previous Client and uninstall it try { // Check if the package exist or not PackageManager oPackageManager = getPackageManager(); PackageInfo oPackageInfo = oPackageManager.getPackageInfo(m_sPackageToUninstall, PackageManager.GET_ACTIVITIES); if (oPackageInfo != null) { Uri oPackageUri = Uri.parse("package:" + m_sPackageToUninstall); Intent oIntent = new Intent(Intent.ACTION_DELETE, oPackageUri); // wait till the uninstall is not completed startActivityForResult(oIntent, UNINSTALL_REQUEST_CODE); } else { launchApp(); } } catch (NameNotFoundException e) { launchApp(); } } private void launchApp() { // Launch the new client from another vendor if it is installed on the device else launch the WAP Mobile TV url try { // Check if the package exist or not PackageManager oPackageManager = getPackageManager(); PackageInfo oPackageInfo = oPackageManager.getPackageInfo(m_sPackageToLaunch, PackageManager.GET_ACTIVITIES); if (oPackageInfo != null) { Intent oNewClientIntent = new Intent(Intent.ACTION_MAIN); oNewClientIntent.setComponent(new ComponentName(m_sPackageToLaunch, m_sClassToLaunch)); // launch the new client startActivity(oNewClientIntent); } else { Intent oIntent = new Intent(Intent.ACTION_VIEW); oIntent.setData(Uri.parse(m_sWebUrl)); // launch the web url startActivity(oIntent); } } catch (NameNotFoundException e) { Intent oIntent = new Intent(Intent.ACTION_VIEW); oIntent.setData(Uri.parse(m_sWebUrl)); startActivity(oIntent); } // make sure this stub application will kill itself once user will exit from the application launched by // the stub super.onDestroy(); finish(); } protected void onActivityResult(int nRequestCode, int nResultCode, Intent oIntent) { super.onActivityResult(nRequestCode, nResultCode, oIntent); if (nRequestCode == UNINSTALL_REQUEST_CODE) { launchApp(); } } }