For security Reasons you can't install APK without an installation Dialog or within the Background. You can install APK's in the background with root and the Packagemanager.
There are several attempts to listen for the installation of the app, so you can restart you activity. One is registering a BroadcastReceiver, which listen for an Installation of the App.
<receiver android:name=".PackageReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="package" />
</intent-filter>
This class then gets called when a new package is installed:
public class PackageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// handle install event here
}
}
A second attempt is having a Timer which try to find the app all nSeconds for installation. Sample from another Question:
public class Example extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Put the package name here...
boolean installed = appInstalledOrNot("com.Ch.Example.pack");
if(installed) {
//This intent will help you to launch if the package is already installed
Intent LaunchIntent = getPackageManager()
.getLaunchIntentForPackage("com.Ch.Example.pack");
startActivity(LaunchIntent);
System.out.println("App already installed on your phone");
}
else {
System.out.println("App is not installed on your phone");
}
}
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
boolean app_installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
}
catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed ;
}
}
Last but not least: If you have the APK and root, you can install it using the shell command:
pm install APKFile.apk