难以尝试安装和更新应用程序

时间:2016-05-18 21:08:28

标签: java android mobile

我在这里引用了一个问题,关于人们如何接近(在Google Play之外)让应用本身更新自己。为了测试,我只是想试着看看我是否可以下载和安装它。不幸的是,我得到一个解析错误。

我非常感谢任何帮助:

调用AsyncTask类的类的片段:

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文件方面有问题。

1 个答案:

答案 0 :(得分:1)

您的主要问题是安装程序无法访问您internal storagegetFilesDir())的部分内容。使用external storage

我还建议您在尝试安装应用之前,在flush()上连续致电getFD().sync()close()FileOutputStream