需要更新我的apk而不是来自Google Play

时间:2018-09-13 08:35:32

标签: android

我正在开发一个不在Google Play上的应用程序,我需要通过从任何可随机下载的链接下载我手动上传的最新apk来更新该apk,然后在安装时进行安装。

我关注了这个话题 Update an Android app (without Google Play)

我遇到了一个问题,该应用程序崩溃,因为现在不允许在targetSdkVersion 24及更高版本上使用Intent附加“ file://”方案,而实际上我的应用程序针对的是更高版本的sdk。

然后,我关注了此博客以解决此问题: https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en 我实现了一个在博客中描述的FileProvider,如下所示:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"/>
</provider>

然后我添加了xml目录并创建了文件provider_paths.xml

我只是遵循它说的所有内容,这是我的Java代码:

public class MainActivity extends AppCompatActivity {

Uri fileUriGlobal;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MyTask task = new MyTask(this);
    task.execute("download1325.mediafire.com/034htxjngoeg/iypqfw37umzo85a/imgapk.apk");
}

class MyTask extends AsyncTask<String, Integer, Void> {

    Context context;

    public MyTask(Context context) {
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(String... strings) {
        String link = strings[0];
        try {
            URL url = new URL(link);
            URLConnection connection = url.openConnection();
            connection.connect();

            int fileLength = connection.getContentLength();

            // download the file
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = openFileOutput("imgapk.apk", MODE_PRIVATE);



            byte data[] = new byte[6000];
            int count;
            while ((count = input.read(data)) != -1) {
                Log.d("KingArmstring", "doInBackground: " + count);
                output.write(data, 0, count);
            }

            File apkFile = new File(getFilesDir(), "imgapk.apk");

            fileUriGlobal = FileProvider.getUriForFile(context,
                    BuildConfig.APPLICATION_ID + ".provider",
                    apkFile);

            output.flush();
            output.close();
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        Intent i = new Intent();
        i.setAction(Intent.ACTION_VIEW);
        i.setDataAndType(fileUriGlobal,  "application/vnd.android.package-archive");
        context.startActivity(i);
    }
}
}

毕竟,我所做的一切仍然无法解决,并且出现了崩溃:

java.lang.RuntimeException: An error occurred while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:318)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:761)
 Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.microdoers.updateapk/files/imgapk.apk
    at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
    at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:418)
    at com.microdoers.updateapk.MainActivity$MyTask.doInBackground(MainActivity.java:78)
    at com.microdoers.updateapk.MainActivity$MyTask.doInBackground(MainActivity.java:37)
    at android.os.AsyncTask$2.call(AsyncTask.java:304)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
    at java.lang.Thread.run(Thread.java:761) 

1 个答案:

答案 0 :(得分:2)

这就是我的方法...

MAX