gpt4 book ai didi

android - 需要更新我的 apk 而不是来自 google play

转载 作者:搜寻专家 更新时间:2023-11-01 08:20:11 24 4
gpt4 key购买 nike

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

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

我遇到了一些问题,应用程序崩溃了,因为“file://”方案现在不允许在 targetSdkVersion 24 及更高版本上附加 Intent,事实上我的应用程序是针对更高的 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) 

最佳答案

我是这样做的...

// DownloadFile AsyncTask
private class DownloadFile extends AsyncTask<String, Integer, String> {

ProgressDialog mProgressDialog;
String filepath;

@Override
protected void onPreExecute() {
super.onPreExecute();
// Create progress dialog
mProgressDialog = new ProgressDialog(context);
// Set your progress dialog Title
mProgressDialog.setTitle("Downloading Updates!");
// Set your progress dialog Message
mProgressDialog.setMessage("Click Install when done...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
// Show progress dialog
mProgressDialog.show();
}

@Override
protected String doInBackground(String... Url) {
try {
String app_url = Url[0];
String f_name = "";
if (app_url.contains("/")) {

String temp[] = app_url.split("/");
f_name = temp[temp.length - 1];
}
URL url = new URL(app_url);
URLConnection connection = url.openConnection();
connection.connect();

// Detect the file length
int fileLength = connection.getContentLength();

// Locate storage location
filepath = Environment.getExternalStorageDirectory()
.getPath() + "/" + f_name;

// Download the file
InputStream input = new BufferedInputStream(url.openStream());

// Save the downloaded file
OutputStream output = new FileOutputStream(filepath);

byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
// Publish the progress
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}

// Close connection
output.flush();
output.close();
input.close();

} catch (Exception e) {
// Error Log
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}

@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
mProgressDialog.setProgress(progress[0]);
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
mProgressDialog.dismiss();
if (filepath != null) {
File file = new File(filepath);

Uri mUri = FileProvider.getUriForFile(context,
BuildConfig.APPLICATION_ID + ".provider",file);

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(mUri, "application/vnd.android.package-archive");
startActivity(intent);
}

}
}

关于android - 需要更新我的 apk 而不是来自 google play,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52309737/

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