我正在尝试在Google Play商店之外下载并安装我的Android更新。 使用本教程作为指南(http://simpledeveloper.com/how-to-update-android-apk-outside-the-playstore/)我在我的应用程序中插入代码,从我的谷歌驱动器下载我的应用程序(包含更新)的apk文件。
该应用程序仅下载文件的一部分(可能是前14KB到100KB)。
我做错了什么?
这是我的代码:
public class ApkUpdateTask extends AsyncTask <String,Void,Void>{
private Context context;
public void setContext(Context contextf){
context = contextf;
}
@Override
protected Void doInBackground(String... arg0) {
try {
URL url = new URL(arg0[0]);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = "/storage/emulated/0/Download/";
//String PATH = Environment.getExternalStorageDirectory().getPath();
File file = new File(PATH);
//file.mkdirs();
File outputFile = new File(file, "sonandso.apk");
if(outputFile.exists()){
outputFile.delete();
}
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
Intent intent = new Intent(Intent.ACTION_VIEW);
//intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory().getPath())), "blahblah.apk");
intent.setDataAndType(Uri.fromFile(new File("/storage/emulated/0/Download/")), "blahblah.apk");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
context.startActivity(intent);
} catch (Exception e) {
Log.e("UpdateAPP", "Update error! " + e.getMessage());
}
return null;
}
}
注意:
答案 0 :(得分:0)
目前,我已决定将其置于架子上,并使用基于谷歌API的方法,我仍在研究。