Android updates download automatically install themselves achieve

1, a number of companies to develop complete and may not go to the App Store shelves after an App, but later things also need regular maintenance update, we will choose to publish packaged apk to your server, and then build a version number in the database the table, and then the rest is up to you to develop an android, android version of himself to achieve detection update, due android comes DownloadManager you can achieve download functions, using them will be very simple, do not write a lot of other related downloads code, but in the downloaded some in the notification bar notification, then the user to manually click to install, some downloaded himself into the installed state, the user only needs to confirm the installation on it, but due to some high version of the system and lower version different automatic installation, here a brief introduction, and then we can quickly deal with this matter, I was automatically installed in this piece to get the good days do not know how, to summarize:

2, start the download start from here, the version number of those requesting not to mention ,,

    Here we can first write a utility class Util, then the top of a static method downLoadApk ();

   eg:

/ **
* Update download APK
* @param context context object
* @param title the name of the program
* @param url download the url address
*
* /

public static long downLoadApk(Context context,String title,String url){

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS,"ausee.apk");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
// 设置 Notification 信息
request.setTitle(title);
request.setDescription("下载完成后请点击打开");
request.setVisibleInDownloadsUi(true);
request.allowScanningByMediaScanner();
request.setMimeType("application/vnd.android.package-archive");

//实例化DownloadManager对象
DownloadManager DownloadManager = (DownloadManager) MyApp.getContext (). GetSystemService (Context.DOWNLOAD_SERVICE);
final lung refrence = downloadManager.enqueue (request);

refrence return;
}
that static methods above can achieve download, pass the url past to OK; Here's to deal with the rest, we note that the above method returns a value of type long,
 we call this method in activity the time to get this return value, and then build a radio receiver activity inside, because the return value is above DownloadManager download a download after the return of id, comes with each download task returns a unique id, and will send a broadcast, here I define the activity inside a method: listener (id), and built a radio receiver; as follows

void listener Private (Final Long Id) {
// Register the download is complete broadcast monitoring system events.
= New new IntentFilter the IntentFilter the IntentFilter (DownloadManager.ACTION_DOWNLOAD_COMPLETE);
BroadcastReceiver the BroadcastReceiver new new = () {
@Override
public void the onReceive (the Context context, the Intent Intent) {
DownloadManager Manager = (DownloadManager) context.getSystemService (Context.DOWNLOAD_SERVICE);
  // this is acquired by the following method download id,
Long ID = intent.getLongExtra (DownloadManager.EXTRA_DOWNLOAD_ID, -1);
// here the id and the acquired broadcast delivery id is not we compare the downloaded APK id, and if then, starts retrieving the download path
if (ID == Id) {

DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(Id);

Cursor = manager.query the Cursor (Query);
IF (cursor.moveToFirst ()) {
// Get File download path
String fileName = cursor.getString (cursor.getColumnIndex (DownloadManager.COLUMN_LOCAL_URI ));

// If the file name is not empty, indicating that the file already exists, then automatically install APK
IF (fileName! = Null) {

openAPK(fileName);

}
}
Cursor.close ();
}
}
};
registerReceiver (BroadcastReceiver, IntentFilter);
}
The following is a method of automatically opened apk above:
/ **
* installation apk
* @param fileSavePath
* /
Private void openAPK (String fileSavePath) {
File File = new new File (Uri.parse (fileSavePath) .getPath ());
String filePath = file.getAbsolutePath ();
the Intent the Intent Intent new new = (Intent.ACTION_VIEW);
Uri Data = null;
IF (Build.VERSION.SDK_INT > = Build.VERSION_CODES.N) {// version is determined not less than 7.0
// ,, generated file URI
  // Note that the following parameters com.ausee.fileprovider apk package name plus .fileprovider,
= FileProvider.getUriForFile Data (LoginActivity.this, "com.ausee.fileprovider", new new File (filePath));
intent.setFlags (Intent.FLAG_GRANT_READ_URI_PERMISSION); // a temporary license to the target application
} the else {
Data = Uri.fromFile (File);
}

intent.setDataAndType (Data, "file application / vnd.android.package-Archive");
startActivity (Intent);
}
The above base can be, but above that parameter com.ausee.fileprovider, this note, the following configuration
in the first new project res files in a folder named xml; after a new file of xml: file_paths.xml;
xml content is:
<xml Version = "1.0" encoding = "UTF-8"??>
<Paths>
<. "" External-path name = "name" path = />
</ Paths>
after written, arranged below a tag provider which here manifest: so written can be:

<Provider
Android: name = "android.support.v4.content.FileProvider"
Android: Authorities = "com.ausee.fileprovider"
Android: = exported "to false"
Android: grantUriPermissions attribute = "to true">
<Meta-Data
Android: name = "android.support.FILE_PROVIDER_PATHS"
Android: Resource = "@ xml / file_paths" />
</ Provider>
close look at the above android: authorities = "com.ausee.fileprovider" ( com.ausee for your own package name) the contents of this parameter and in front of the same, right? Because here, the content here is so filled, plus a fileprovider package name on it, and then in the meta-data inside the configuration xml file just opened configuration can come in!

 

We need to add android.intent.category.DEFAULT

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

Well, here it is completed, go try it!


 

Guess you like

Origin www.cnblogs.com/Alex80/p/11611798.html