因爲你不能沒有安裝對話框或背景中安裝APK安全原因。您可以使用root和Packagemanager在後檯安裝APK。
有幾次嘗試偵聽應用程序的安裝,因此您可以重新啓動您的活動。一個是註冊一個BroadcastReceiver,它監聽應用程序的安裝。
<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>
這個類,然後安裝一個新包時被調用:
public class PackageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// handle install event here
}
}
第二次嘗試是有一個計時器,試圖找到該應用程序的安裝所有n秒。從另一個問題示例:
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 ;
}
}
最後但並非最不重要的:如果你有APK和根,你可以使用shell命令來安裝它:
pm install APKFile.apk
不,你不能從Play下載的APK您自己存儲,您必須通過商店的用戶界面,無論是在設備上的應用程序或Web界面。 – 2015-03-13 14:41:03