Я хочу скачать свое приложение по ссылке, по которой можно скачать apk. Я нашел некоторый код на stackoverFlow, который я использовал, но не работает в моем приложении, когда вы нажимаете кнопку обновления, оно начинает обновляться и когда я касался его, Toast «загрузка» отображается, что означает, что код выполняется до конца, но все еще не работает
update.setOnClickListener(v -> {
String destination;
Uri uri;
try {
destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
String fileName = "AppName.apk";
destination += fileName;
uri = Uri.parse("file://" + destination);
} catch (Exception e) {
Toast.makeText(activity, "stage 1 : " + e, Toast.LENGTH_SHORT).show();
Log.i("New Version", "stage 1 : " + e);
return;
}
//
try {
File file = new File(destination);
if (file.exists())
file.delete();
} catch (Exception e) {
Toast.makeText(activity, "stage 2 : " + e, Toast.LENGTH_SHORT).show();
Log.i("New Version", "stage 2 : " + e);
return;
}
try {
String downLoadUrl = "http://api.iotkaran.ir/Android";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downLoadUrl));
request.setDescription("Downloading New version");
request.setTitle(MainActivity.this.getString(R.string.app_name));
request.setDestinationUri(uri);
} catch (Exception e) {
Toast.makeText(activity, "stage 3 : " + e, Toast.LENGTH_SHORT).show();
Log.i("New Version", "stage 3 : " + e);
return;
}
BroadcastReceiver onComplete;
try {
final String finalDestination = destination;
onComplete = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
try {
File toInstall = new File(finalDestination);
Log.i("Install new app", "here");
Intent intent2;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri apkUri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".fileprovider", toInstall);
intent2 = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent2.setData(apkUri);
intent2.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
MainActivity.this.startActivity(intent2);
} else {
Uri apkUri = Uri.fromFile(toInstall);
intent2 = new Intent(Intent.ACTION_VIEW);
intent2.setDataAndType(apkUri, "application/vnd.android.package-archive");
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MainActivity.this.startActivity(intent2);
}
activity.unregisterReceiver(this);
} catch (Exception e) {
Toast.makeText(activity, "stage 6 : " + e, Toast.LENGTH_SHORT).show();
Log.i("New Version", "stage 6 : " + e);
}
}
};
} catch (Exception e) {
Toast.makeText(activity, "stage 4 : " + e, Toast.LENGTH_SHORT).show();
Log.i("New Version", "stage 4 : " + e);
return;
}
//register receiver for when .apk download is compete
try {
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
Toast.makeText(activity, "downloading", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(activity, "stage 5 : " + e, Toast.LENGTH_SHORT).show();
Log.i("New Version", "stage 5 : " + e);
}
});
, и это мой манифест
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.iotsmarthouses"
android:usesCleartextTraffic="true">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"
tools:remove="android:maxSdkVersion" />
<uses-permission
android:name="android.permission.RECORD_AUDIO"
tools:remove="android:maxSdkVersion" />
<uses-permission
android:name="android.permission.CAMERA"
tools:remove="android:maxSdkVersion" />
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
tools:remove="android:maxSdkVersion" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:remove="android:maxSdkVersion" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-sdk tools:overrideLibrary="com.google.zxing.client.android" />
<application
android:name=".App.App"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@drawable/app_icon"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar">
<activity android:name=".DeviceRegisterActivities.RegisterViaViewPager"></activity>
<activity
android:name=".Activities.ShowingRoomsAndDevicesActivity"
android:theme="@style/Theme.Transparent" />
<activity
android:name=".Activities.AddNewDeviceToRoom"
android:theme="@style/Theme.Transparent" />
<activity
android:name=".Activities.AddingDeviceToScenarioActivity"
android:theme="@style/Theme.Transparent" />
<activity
android:name=".Activities.DeviceActivity"
android:theme="@style/Theme.Transparent" />
<activity android:name=".Activities.ScenarioInfoActivity" />
<activity android:name=".Activities.MyScenarioActivity" />
<activity
android:name=".Activities.VoiceRecorderActivity"
android:theme="@style/Theme.Transparent" />
<activity
android:name=".Activities.MyScenarioCreateActivity"
android:theme="@style/Theme.Transparent" />
<activity
android:name=".Activities.MyHomeActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Activities.AddDeviceActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Activities.ConnectionDown"
android:screenOrientation="portrait" />
<activity
android:name=".Activities.ServerDownActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Activities.InBuildActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Activities.MessagaActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Activities.SettingMenuActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Activities.RequestActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Activities.ProfileActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Activities.AccessPointActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Activities.sms_registeryActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Activities.LoadingActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Activities.FingerLogInActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Activities.MyHomeAddDeviceActivity"
android:theme="@style/Theme.Transparent" />
<activity
android:name=".Activities.LoginActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activities.MainActivity"
android:screenOrientation="portrait" >
</activity>
<service android:name=".MyService.MyService" />
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths" />
</provider>
</application>
</manifest>
, и это мой file_provider_paths xml file
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_download" path="Download"/>
</paths>