Android简易的自动更新(通知栏+系统下载),包含7.0系统解析安装包失败的处理

来源:互联网 发布:江歌 刘鑫 知乎 编辑:程序博客网 时间:2024/05/17 22:53

项目需要添加自动更新功能,因为是挪用加整理的,所以记笔记方面以后使用

首先是UpdateService,这个是整个功能的核心代码,所有关于更新的处理都在这个代码中

package com.example.updata;import java.io.File;import android.app.DownloadManager;import android.app.Service;import android.content.BroadcastReceiver;import android.content.ContentResolver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.database.Cursor;import android.net.Uri;import android.os.Build;import android.os.Environment;import android.os.IBinder;import android.provider.MediaStore;import android.support.v4.content.FileProvider;import android.util.Log;public class UpdateService extends Service{    public UpdateService()    {    }    /** 安卓系统下载类 **/    DownloadManager manager;    /** 接收下载完的广播 **/    DownloadCompleteReceiver receiver;    /** 初始化下载器 **/    private void initDownManager()    {        manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);        receiver = new DownloadCompleteReceiver();        // 设置下载地址        String urlPath = "http://192.168.1.109/Updata.apk";        Uri parse = Uri.parse(urlPath);        DownloadManager.Request down = new DownloadManager.Request(parse);        // 设置允许使用的网络类型,这里是移动网络和wifi都可以        down.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);        // 下载时,通知栏显示途中        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)        {            down.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);        }        // 显示下载界面        down.setVisibleInDownloadsUi(true);        // 设置下载后文件存放的位置        String apkName = parse.getLastPathSegment();        down.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, apkName);        // 将下载请求放入队列        manager.enqueue(down);        // 注册下载广播        registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));    }    @Override    public int onStartCommand(Intent intent, int flags, int startId)    {        // 调用下载        initDownManager();        return super.onStartCommand(intent, flags, startId);    }    @Override    public IBinder onBind(Intent intent)    {        return null;    }    @Override    public void onDestroy()    {        // 注销下载广播        if (receiver != null)            unregisterReceiver(receiver);        super.onDestroy();    }    // 接受下载完成后的intent    class DownloadCompleteReceiver extends BroadcastReceiver    {        @Override        public void onReceive(Context context, Intent intent)        {            // 判断是否下载完成的广播            if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE))            {                // 获取下载的文件id                long downId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);                Log.d("kodulf", "id=" + downId);                // 自动安装apk                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)                {                    Uri uriForDownloadedFile = manager.getUriForDownloadedFile(downId);                    Log.d("kodulf", "uri=" + uriForDownloadedFile);                    installApkNew(uriForDownloadedFile);                }                // 停止服务并关闭广播                UpdateService.this.stopSelf();            }        }        // 安装apk        protected void installApkNew(Uri uri)        {            String fileName = getRealFilePath(getApplicationContext(), uri);            if (fileName != null)            {                if (fileName.endsWith(".apk"))                {                    if (Build.VERSION.SDK_INT >= 24)                    {// 判读版本是否在7.0以上                        File file = new File(fileName);                        Uri apkUri = FileProvider.getUriForFile(getApplicationContext(),                                "com.example.updata.fileprovider", file);// 在AndroidManifest中的android:authorities值                        Intent install = new Intent(Intent.ACTION_VIEW);                        install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                        install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);// 添加这一句表示对目标应用临时授权该Uri所代表的文件                        install.setDataAndType(apkUri, "application/vnd.android.package-archive");                        getApplicationContext().startActivity(install);                    } else                    {                        Intent install = new Intent(Intent.ACTION_VIEW);                        install.setDataAndType(Uri.fromFile(new File(fileName)),                                "application/vnd.android.package-archive");                        install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                        getApplicationContext().startActivity(install);                    }                }            }        }        /**         * 通过Uri得到文件路径         * @param context         * @param uri         * @return         */        protected String getRealFilePath(final Context context, final Uri uri)        {            if (null == uri)                return null;            final String scheme = uri.getScheme();            String data = null;            if (scheme == null)                data = uri.getPath();            else if (ContentResolver.SCHEME_FILE.equals(scheme))            {                data = uri.getPath();            } else if (ContentResolver.SCHEME_CONTENT.equals(scheme))            {                Cursor cursor = context.getContentResolver().query(uri, new String[]                {                        MediaStore.Images.ImageColumns.DATA                }, null, null, null);                if (null != cursor)                {                    if (cursor.moveToFirst())                    {                        int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);                        if (index > -1)                        {                            data = cursor.getString(index);                        }                    }                    cursor.close();                }            }            return data;        }    }}

MainActivity

package com.example.updata;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity{    @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    /**     *     * @param view     */    public void update(View view)    {        new Thread(new Runnable()        {            @Override            public void run()            {                // 启动服务                Intent service = new Intent(MainActivity.this, UpdateService.class);                startService(service);            }        }).start();    }}

activity_main.xml布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="${relativePackage}.${activityClass}" >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:onClick="update"        android:text="开始更新"        android:textColor="@android:color/holo_red_dark" /></RelativeLayout>

AndroidManifest.xml中,为更新添加权限,以及注册服务

 <uses-permission android:name="android.permission.INTERNET" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
  <service            android:name=".UpdateService"            android:enabled="true"            android:exported="true" >  </service>

关于7.0的处理,首先,在AndroidManifest.xml中添加provider

<provider            android:name="android.support.v4.content.FileProvider"            android:authorities="com.example.updata.fileprovider"            android:exported="false"            android:grantUriPermissions="true" >            <meta-data                android:name="android.support.FILE_PROVIDER_PATHS"                android:resource="@xml/file_paths" /></provider>

在res/xml中添加file_paths.xml文件

<?xml version="1.0" encoding="utf-8"?><resources>    <paths>        <external-path name="download" path="" />    </paths></resources>

在UpdateService中,installApkNew方法安装APK,通过判断,处理7.0的安装

// 安装apk        protected void installApkNew(Uri uri)        {            String fileName = getRealFilePath(getApplicationContext(), uri);            if (fileName != null)            {                if (fileName.endsWith(".apk"))                {                    if (Build.VERSION.SDK_INT >= 24)                    {// 判读版本是否在7.0以上                        File file = new File(fileName);                        Uri apkUri = FileProvider.getUriForFile(getApplicationContext(),                                "com.example.updata.fileprovider", file);// 在AndroidManifest中的android:authorities值                        Intent install = new Intent(Intent.ACTION_VIEW);                        install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                        install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);// 添加这一句表示对目标应用临时授权该Uri所代表的文件                        install.setDataAndType(apkUri, "application/vnd.android.package-archive");                        getApplicationContext().startActivity(install);                    } else                    {                        Intent install = new Intent(Intent.ACTION_VIEW);                        install.setDataAndType(Uri.fromFile(new File(fileName)),                                "application/vnd.android.package-archive");                        install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                        getApplicationContext().startActivity(install);                    }                }            }        }

这个方法里调用getRealFilePath方法来通过传递的Uri得到apk的文件路径

protected String getRealFilePath(final Context context, final Uri uri)        {            if (null == uri)                return null;            final String scheme = uri.getScheme();            String data = null;            if (scheme == null)                data = uri.getPath();            else if (ContentResolver.SCHEME_FILE.equals(scheme))            {                data = uri.getPath();            } else if (ContentResolver.SCHEME_CONTENT.equals(scheme))            {                Cursor cursor = context.getContentResolver().query(uri, new String[]                {                        MediaStore.Images.ImageColumns.DATA                }, null, null, null);                if (null != cursor)                {                    if (cursor.moveToFirst())                    {                        int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);                        if (index > -1)                        {                            data = cursor.getString(index);                        }                    }                    cursor.close();                }            }            return data;        }

这样,一个简易的自动更新就写出来了。

阅读全文
0 0
原创粉丝点击