[英]Installing APK programmatically
我使用的代碼與Android基本相同:以編程方式安裝.apk 。 該應用程序啟動了新的意圖,但沒有任何反應。 該APK已從我們的服務器(內部應用程序不在Play商店中)上拉下來,但安裝提示從不出現,就像我導航到downloads文件夾並手動單擊該APK一樣。 我將意圖過濾器放置在.Main活動上是否與事實有關?
這是我的代碼
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
URL url = new URL("http://ourURL/app.apk");
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.connect();
String PATH = Environment.getExternalStorageDirectory() + "/Download/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "app.apk");
FileOutputStream fos = new FileOutputStream(outputFile);
is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
Intent promptInstall = new Intent(Intent.ACTION_VIEW);
promptInstall.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/Download/" + "app.apk")), "application/vnd.andriod.package-archive");
promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(promptInstall);
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Update error!" + e.toString() + e.getStackTrace().toString(), Toast.LENGTH_LONG).show();
TextView txtQuestion = (TextView) findViewById(R.id.txtViewQuestion);
txtQuestion.setText(e.toString());
}
}`
這是我的清單`
<application
android:allowBackup="true"
android:icon="@drawable/bancsource"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:configChanges="orientation"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.OPENABLE"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:path="@string/updatePath"/>
<data android:mimeType="application/vnd.andriod.package-archive"/>
</intent-filter>
</activity>`
我發現了...在“ application / vnd.andriod.package-archive”中拼寫錯誤的android
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.