Skip to content

Instantly share code, notes, and snippets.

@zurche
Created November 25, 2016 13:45
Show Gist options
  • Save zurche/2d4b7cf8a94484ccfc8d244b1cdda227 to your computer and use it in GitHub Desktop.
Save zurche/2d4b7cf8a94484ccfc8d244b1cdda227 to your computer and use it in GitHub Desktop.
An app to install a list of apps.
package com.example.zurcher.appinstaller;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final int INSTALL_APP = 1;
private int mCurrentAppBeingInstalled = 0;
String[] APPS_LIST = {
"com.google.android.apps.tachyon",
"com.whatsapp",
"com.twitter.android",
"com.instagram.android",
"com.google.android.talk",
"com.android.chrome",
"com.facebook.katana"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startInstallProcess();
}
private void startInstallProcess() {
startActivityForResult(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + APPS_LIST[mCurrentAppBeingInstalled])), INSTALL_APP);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == INSTALL_APP) {
try {
checkIfAppIsInstalled();
mCurrentAppBeingInstalled++;
launchGooglePlayForApp(APPS_LIST[mCurrentAppBeingInstalled]);
} catch (PackageManager.NameNotFoundException e) {
launchGooglePlayForApp(APPS_LIST[mCurrentAppBeingInstalled]);
}
}
}
private void checkIfAppIsInstalled() throws PackageManager.NameNotFoundException {
getPackageManager().getPackageInfo(APPS_LIST[mCurrentAppBeingInstalled], PackageManager.GET_ACTIVITIES);
Toast.makeText(MainActivity.this, APPS_LIST[mCurrentAppBeingInstalled] + " is installed.\nInstalling " + APPS_LIST[mCurrentAppBeingInstalled + 1], Toast.LENGTH_SHORT).show();
}
private void launchGooglePlayForApp(String appPackageName) {
startActivityForResult(
new Intent(Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)), INSTALL_APP);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment