gpt4 book ai didi

java - 尝试安装和更新应用程序时遇到困难

转载 作者:行者123 更新时间:2023-12-01 09:55:00 26 4
gpt4 key购买 nike

我在这里提到了一个关于如何(在 Google Play 之外)让应用程序本质上 self 更新的问题。为了进行测试,我只是想尝试看看是否可以下载并安装它。不幸的是,我收到解析错误。

我将非常感谢任何帮助:

调用 AsyncTask 类的类的 fragment :

public class downloadReceiver extends BroadcastReceiver {

private Context context;
private long localUpdate;
private long remoteUpdate = 20;

@Override
public void onReceive(final Context c, Intent i) {

new Thread(new Runnable() {
@Override
public void run() {
SharedPreferences preferences = c.getSharedPreferences("config", c.MODE_PRIVATE);
final String store = preferences.getString("store", "");
final String id = preferences.getString("id", "");

final long lastUpdated = preferences.getLong("updated", 0);
// autoUpdate app
appUpdater updater = new appUpdater(c);

try {
updater.execute(new URL("http://midamcorp.com/myApp.php"));
} catch (Exception e) {Log.e(this.getClass().toString(), " " + e.getMessage()); }

和appUpdater类:

public class appUpdater extends AsyncTask<URL, String, String> {
private Context c;
public appUpdater(Context context) {
this.c = context;
}

protected String doInBackground(URL... appUrl) {
String location = c.getFilesDir() + "/app.apk";
try {
URL url = appUrl[0];
URLConnection con = url.openConnection();
con.connect();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(location);
byte[] buffer = new byte[1024];
int read = 0;

while ((read = input.read(buffer)) != -1) {

output.write(buffer, 0, read);
}
output.close();
input.close();

} catch(Exception e){
Log.e(this.getClass().toString(), " " + e.getMessage());
}
return location;
}
@Override
protected void onPostExecute(String saveLocation) {
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
Log.i("Location of app is: ", " " + saveLocation);
i.setDataAndType(Uri.fromFile(new File(saveLocation)), "application/vnd.android.package-archive");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
c.startActivity(i);
}
}

请注意,该 URL 链接到强制下载的 PHP 文件,因为我所在的服务器在处理 .apk 文件时出现问题。

最佳答案

您的主要问题是安装程序无权访问您的 internal storage 部分(getFilesDir())。使用external storage .

我还建议您在您的设备上连续调用 flush()getFD().sync()close() FileOutputStream,在尝试安装应用程序之前。

关于java - 尝试安装和更新应用程序时遇到困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37310132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com