[英]Need to update my apk not from the google play
我正在開發一個不在Google Play上的應用程序,我需要通過從任何可隨機下載的鏈接下載我手動上傳的最新apk來更新該apk,然后在安裝時進行安裝。
我按照此主題更新了Android應用(沒有Google Play)
我遇到了一個問題,該應用程序崩潰,因為現在不允許將“ file://”方案與Intent一起附加在targetSdkVersion 24和更高版本上,實際上我的應用程序針對的是更高版本的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);
}
}
}
我已經解決了這個問題,解決方案的一半與@SRB禁令相同,但單獨使用onPostExecute()方法而不是:
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);
我用了:
File file = new File(filepath);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Log.d("KingArmstring", "onPostExecute: 24 or higher");
Uri apkUri = FileProvider.getUriForFile(((Activity)context), BuildConfig.APPLICATION_ID + ".provider", file);
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData(apkUri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
((Activity)context).startActivity(intent);
} else {
Log.d("KingArmstring", "onPostExecute: 23 or lower");
Uri apkUri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
((Activity)context).startActivity(intent);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.