Android studio app版本检测更新

本文介绍了一个简化版的安卓应用更新机制实现,通过检测服务器上的最新版本信息,对比本地版本,弹出更新对话框并允许用户下载和安装新版本。文章详细展示了如何在不同安卓版本上实现这一功能,包括使用广播接收器监听下载状态,以及如何处理7.0及更高版本的文件提供者权限。

基本上每个都要检测更新的功能,参考了下大佬的文章,弄了个简化版的记录一下。在安卓6.0和9.0版本中都试过了,可以用

参考文章:

			 https://blog.youkuaiyun.com/fengyuzhengfan/article/details/52876586
			 https://blog.youkuaiyun.com/MingHuang2017/article/details/82830727

直接复制黏贴吧

demo下载地址

MainActivity.class

package com.example.rock.versioncontrolldemo;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    private Button btnRefresh;
    private VersionInfoBean versionInfoBean;
    private boolean isLoading;
    private MyReceive myReceive;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnRefresh = (Button)findViewById(R.id.btn_refresh);
        myReceive = new MyReceive();
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.intent.action.loading_over");
        filter.addAction("android.intent.action.loading");
        registerReceiver(myReceive,filter);
        btnRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isLoading){
                    Toast.makeText(MainActivity.this,"正在下载...",Toast.LENGTH_SHORT).show();
                }else{
                    getVersionInfoFromServer();
                }
            }
        });
    }

    /**
     * 从服务器获取版本最新的版本信息
     */
    private void getVersionInfoFromServer(){
        //模拟从服务器获取信息
        versionInfoBean = new VersionInfoBean("1.2","这里放你的apk下载链接地址","1.更新了xxxxx功能"
        ,getExternalCacheDir()+"/apk");
        SharedPreferences sharedPreferences = getSharedPreferences("data",MODE_PRIVATE);
        sharedPreferences.edit().putString("url",versionInfoBean.getDownloadUrl()).commit();
        sharedPreferences.edit().putString("path",versionInfoBean.getPath()).commit();//getExternalCacheDir获取到的路径 为系统为app分配的内存 卸载app后 该目录下的资源也会删除
        //比较版本信息
        try {
            int result = Utils.compareVersion(Utils.getVersionName(this),versionInfoBean.getVersionName());
            if(result==-1){//不是最新版本
                showDialog();
            }else{
                Toast.makeText(MainActivity.this,"已经是最新版本",Toast.LENGTH_SHORT).show();
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

    }
    private  void showDialog(){
        final Dialog dialog = new Dialog(MainActivity.this);
        LayoutInflater inflater = (LayoutInflater)MainActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        TextView version,content;
        Button    left,right;
        View view = inflater.inflate(R.layout.version_update,null,false);
        version = (TextView)view.findViewById(R.id.version);
        content = (TextView)view.findViewById(R.id.content);
        left = (Button)view.findViewById(R.id.left);
        right = (Button)view.findViewById(R.id.right);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            content.setText(Html.fromHtml(versionInfoBean.getDesc(),Html.FROM_HTML_MODE_LEGACY));
        }else{
            content.setText(Html.fromHtml(versionInfoBean.getDesc()));
        }
        content.setMovementMethod(LinkMovementMethod.getInstance());
        version.setText("版本号:"+versionInfoBean.getVersionName());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                    downloadNewVersionFromServer();

            }
        });
        dialog.setContentView(view);
        dialog.setCancelable(false);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        //dialogWindow.setWindowAnimations(R.style.ActionSheetDialogAnimation);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        WindowManager wm = (WindowManager)
                getSystemService(Context.WINDOW_SERVICE);
        lp.width =wm.getDefaultDisplay().getWidth()/10*9;
        dialogWindow.setAttributes(lp);
        dialog.show();
    }

    /**
     * 启动服务后台下载
     */
    private void downloadNewVersionFromServer(){
        if(new File(versionInfoBean.getPath()).exists()){
            new File(versionInfoBean.getPath()).delete();
        }
        Toast.makeText(MainActivity.this,"开始下载...",Toast.LENGTH_SHORT).show();
            LoadingService.startUploadImg(this);
    }

    /**
     * 定义广播接收者 接受下载状态
     */
    public class MyReceive extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
           if("android.intent.action.loading_over".equals(action)){
                isLoading = false;
            }else if("android.intent.action.loading".equals(action)){
                isLoading = true;
            }
        }
    }
}

LoadingService.class

package com.example.rock.versioncontrolldemo;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.FileProvider;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import java.io.File;


public class LoadingService extends IntentService {
    private HttpUtils httpUtils;
    NotificationManager nm;
    private String url,path;
    private SharedPreferences sharedPreferences;
    public LoadingService(String name) {
        super(name);
    }
    public LoadingService() {
        super("MyService");

    }


    public static void startUploadImg(Context context)
    {
        Intent intent = new Intent(context, LoadingService.class);
        context.startService(intent);
    }



    public void onCreate() {
        super.onCreate();
        httpUtils = new HttpUtils();
        httpUtils.configCurrentHttpCacheExpiry(0);
        nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        sharedPreferences = getSharedPreferences("data",MODE_PRIVATE);
    }



    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        updateApk();
    }




    private void updateApk(){
        url = sharedPreferences.getString("url","");
        path = sharedPreferences.getString("path","");
        httpUtils.download(url,
                path , new RequestCallBack<File>() {
                    @Override
                    public void onLoading(final long total, final long current,
                                          boolean isUploading) {
                        createNotification(total,current);
                        sendBroadcast(new Intent().setAction("android.intent.action.loading"));//发送正在下载的广播
                        super.onLoading(total, current, isUploading);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<File> arg0) {
                        nm.cancel(R.layout.notification_item);
                        Toast.makeText(LoadingService.this,"下载成功...",Toast.LENGTH_SHORT).show();
                        installApk();//下载成功 打开安装界面
                        stopSelf();//结束服务
                        sendBroadcast(new Intent().setAction("android.intent.action.loading_over"));//发送下载结束的广播
                    }

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        Toast.makeText(LoadingService.this,"下载失败...",Toast.LENGTH_SHORT).show();
                        sendBroadcast(new Intent().setAction("android.intent.action.loading_over"));//发送下载结束的广播
                        nm.cancel(R.layout.notification_item);
                        stopSelf();
                    }
                });
    }
    /**
     * 安装下载的新版本
     */
    protected void installApk() {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        File file = new File(path);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            // 7.0 以上
            try {
                Uri apkUri = FileProvider.getUriForFile(this, "com.mw.mwreaderdemo.fileprovider", file);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
            }catch (Exception e){

            }

        } else {
            // 7.0以下
            Uri uri = Uri.fromFile(file);
            intent.setDataAndType(uri, "application/vnd.android.package-archive");

        }
        this.startActivity(intent);
    }

    private void createNotification(final long total, final long current){
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);//必须要设置这个属性,否则不显示
        RemoteViews contentView = new RemoteViews(this.getPackageName(),R.layout.notification_item);
        contentView.setProgressBar(R.id.progress, (int)total, (int)current, false);
        builder.setOngoing(true);//设置左右滑动不能删除
//        Notification notification  = builder.build();
//        notification.contentView = contentView;
        String id = "channel_001";
        String name = "name";
        Notification notification = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
            nm.createNotificationChannel(mChannel);
            notification = new Notification.Builder(LoadingService.this)
                    .setChannelId(id)
                    .setContentTitle("更新了")
                    .setContentText("更新了xxx内容")
                    .setSmallIcon(R.mipmap.ic_launcher).build();
            notification.contentView = contentView;
        } else {
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(LoadingService.this)
                    .setContentTitle("更新了")
                    .setContentText("更新了xxx内容")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setOngoing(true)
                    ;//无效
            notification = notificationBuilder.build();
            notification.contentView = contentView;

        }


        nm.notify(R.layout.notification_item,notification);//发送通知
    }


}

Utils.class

package com.example.rock.versioncontrolldemo;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;



public class Utils {
    /**
     * 获取版本号
     *
     * @throws PackageManager.NameNotFoundException
     */
    public static String getVersionName(Context context) throws PackageManager.NameNotFoundException {
        // 获取packagemanager的实例
        PackageManager packageManager = context.getPackageManager();
        // getPackageName()是你当前类的包名,0代表是获取版本信息
        PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
        String version = packInfo.versionName;
        return version;
    }

    /**
     * 版本号比较
     *0代表相等,1代表version1大于version2,-1代表version1小于version2
     * @param version1
     * @param version2
     * @return
     */
    public static int compareVersion(String version1, String version2) {
        if (version1.equals(version2)) {
            return 0;
        }
        String[] version1Array = version1.split("\\.");
        String[] version2Array = version2.split("\\.");
        int index = 0;
        // 获取最小长度值
        int minLen = Math.min(version1Array.length, version2Array.length);
        int diff = 0;
        // 循环判断每位的大小
        while (index < minLen
                && (diff = Integer.parseInt(version1Array[index])
                - Integer.parseInt(version2Array[index])) == 0) {
            index++;
        }
        if (diff == 0) {
            // 如果位数不一致,比较多余位数
            for (int i = index; i < version1Array.length; i++) {
                if (Integer.parseInt(version1Array[i]) > 0) {
                    return 1;
                }
            }

            for (int i = index; i < version2Array.length; i++) {
                if (Integer.parseInt(version2Array[i]) > 0) {
                    return -1;
                }
            }
            return 0;
        } else {
            return diff > 0 ? 1 : -1;
        }
    }
}

VersionInfoBean.class

package com.example.rock.versioncontrolldemo;

/**
 * Created by Rock on 2018/2/2.
 */

public class VersionInfoBean {
    private String versionName;
    private String downloadUrl;
    private String desc;
    private String path;

    public VersionInfoBean(String versionName, String downloadUrl, String desc, String path) {
        this.versionName = versionName;
        this.downloadUrl = downloadUrl;
        this.desc = desc;
        this.path = path;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getVersionName() {
        return versionName;
    }

    public void setVersionName(String versionName) {
        this.versionName = versionName;
    }

    public String getDownloadUrl() {
        return downloadUrl;
    }

    public void setDownloadUrl(String downloadUrl) {
        this.downloadUrl = downloadUrl;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public VersionInfoBean(String versionName, String downloadUrl, String desc) {
        this.versionName = versionName;
        this.downloadUrl = downloadUrl;
        this.desc = desc;
    }
}

LoadingService.class

package com.example.rock.versioncontrolldemo;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.FileProvider;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import java.io.File;


public class LoadingService extends IntentService {
    private HttpUtils httpUtils;
    NotificationManager nm;
    private String url,path;
    private SharedPreferences sharedPreferences;
    public LoadingService(String name) {
        super(name);
    }
    public LoadingService() {
        super("MyService");

    }


    public static void startUploadImg(Context context)
    {
        Intent intent = new Intent(context, LoadingService.class);
        context.startService(intent);
    }



    public void onCreate() {
        super.onCreate();
        httpUtils = new HttpUtils();
        httpUtils.configCurrentHttpCacheExpiry(0);
        nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        sharedPreferences = getSharedPreferences("data",MODE_PRIVATE);
    }



    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        updateApk();
    }




    private void updateApk(){
        url = sharedPreferences.getString("url","");
        path = sharedPreferences.getString("path","");
        httpUtils.download(url,
                path , new RequestCallBack<File>() {
                    @Override
                    public void onLoading(final long total, final long current,
                                          boolean isUploading) {
                        createNotification(total,current);
                        sendBroadcast(new Intent().setAction("android.intent.action.loading"));//发送正在下载的广播
                        super.onLoading(total, current, isUploading);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<File> arg0) {
                        nm.cancel(R.layout.notification_item);
                        Toast.makeText(LoadingService.this,"下载成功...",Toast.LENGTH_SHORT).show();
                        installApk();//下载成功 打开安装界面
                        stopSelf();//结束服务
                        sendBroadcast(new Intent().setAction("android.intent.action.loading_over"));//发送下载结束的广播
                    }

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        Toast.makeText(LoadingService.this,"下载失败...",Toast.LENGTH_SHORT).show();
                        sendBroadcast(new Intent().setAction("android.intent.action.loading_over"));//发送下载结束的广播
                        nm.cancel(R.layout.notification_item);
                        stopSelf();
                    }
                });
    }
    /**
     * 安装下载的新版本
     */
    protected void installApk() {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        File file = new File(path);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            // 7.0 以上
            try {
                Uri apkUri = FileProvider.getUriForFile(this, "com.mw.mwreaderdemo.fileprovider", file);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
            }catch (Exception e){

            }

        } else {
            // 7.0以下
            Uri uri = Uri.fromFile(file);
            intent.setDataAndType(uri, "application/vnd.android.package-archive");

        }
        this.startActivity(intent);
    }

    private void createNotification(final long total, final long current){
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);//必须要设置这个属性,否则不显示
        RemoteViews contentView = new RemoteViews(this.getPackageName(),R.layout.notification_item);
        contentView.setProgressBar(R.id.progress, (int)total, (int)current, false);
        builder.setOngoing(true);//设置左右滑动不能删除
//        Notification notification  = builder.build();
//        notification.contentView = contentView;
        String id = "channel_001";
        String name = "name";
        Notification notification = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
            nm.createNotificationChannel(mChannel);
            notification = new Notification.Builder(LoadingService.this)
                    .setChannelId(id)
                    .setContentTitle("更新了")
                    .setContentText("更新了xxx内容")
                    .setSmallIcon(R.mipmap.ic_launcher).build();
            notification.contentView = contentView;
        } else {
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(LoadingService.this)
                    .setContentTitle("更新了")
                    .setContentText("更新了xxx内容")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setOngoing(true)
                    ;//无效
            notification = notificationBuilder.build();
            notification.contentView = contentView;

        }


        nm.notify(R.layout.notification_item,notification);//发送通知
    }


}

在manifest中操作

加入权限

<uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

注册

 <service android:name=".activity.LoadingService"
            android:process="system"/><!--注册我系统级别的服务 这样退出app可以继续下载-->
<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.mw.mwreaderdemo.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <!-- 元数据 -->
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/update_apk_paths" />
                </provider>

在res文件中新建一个xml文件夹,在这个文件夹中新建一个update_apk_paths.xml

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

新建layout,命名:notification_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:paddingTop="8dp"
    android:layout_height="wrap_content">
<ImageView
    android:src="@mipmap/ic_launcher"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_marginRight="6dp"
        android:layout_marginLeft="8dp"
        android:layout_gravity="center_vertical"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:textSize="14sp"
            android:text="正在下载"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ProgressBar
            android:layout_marginTop="4dp"
            style="?android:attr/progressBarStyleHorizontal"
            android:id="@+id/progress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    
</LinearLayout>

新建layout,命名:version_update.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:orientation="vertical">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_vertical|center_horizontal"
        android:paddingLeft="10dp"
        android:text="更新提示"
        android:textColor="#000000"
        android:textSize="18sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000000" />

        <TextView
            android:layout_marginTop="10dp"
            android:id="@+id/version"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="12dp"
            android:paddingRight="10dp"
            android:text="content"
            android:textSize="16sp" />
    <TextView
        android:layout_marginTop="14dp"
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="12dp"
        android:paddingRight="10dp"
        android:text="content"
        android:textSize="14sp" />


    <TextView
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#f0f0f0" />

    <LinearLayout
        android:layout_marginTop="4dp"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:orientation="horizontal"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">

        <Button
            android:textColor="#000000"
            android:id="@+id/left"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@null"
            android:gravity="center_vertical|center_horizontal"
            android:text="取消"
            android:textSize="16sp"
          />

        <TextView
            android:textColor="#f0f0f0"
            android:id="@+id/line"
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
           />

        <Button
            android:textColor="#d90c0c"
            android:id="@+id/right"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_weight="1"
            android:background="@null"
            android:gravity="center_vertical|center_horizontal"
            android:text="安装"
            android:textSize="16sp"
            />
    </LinearLayout>
</LinearLayout>

demo下载地址

框架内部支持中/英文(其他语言只需要在对应的string.xml中取相同的名字即可)内部对话框背景图片、按钮支持自定义了查看版本中的Log只需要过滤AppUpdate开头的Tag重点: 如果没有设置downloadPath则默认为getExternalCacheDir()目录,同时不会申请[存储]权限!目录编译问题效果图功能介绍DownloadManagerUpdateConfiguration使用步骤Demo下载体验版本更新记录结语编译问题因为适配了Android O的通知栏,所以依赖的v7包版本比较高appcompat-v7:26.1.0使用的gradle版本为gradle-4.1-all,所以建议使用Android Studio 3.0及以上的版本打开此项目效果图     功能介绍 支持断点下载 支持后台下载 支持自定义下载过程 支持 设备 >= Android M 动态权限的申请 支持通知栏进度条展示(或者自定义显示进度) 支持Android N 支持Android O 支持中/英文双语 支持自定内置对话框的样式 使用HttpURLConnection下载,未集成其他第三方框架更加详细的文档参阅此处《AppUpdate API文档》DownloadManager:配置文档初始化使用DownloadManager.getInstance(this)属性描述默认值是否必须设置context上下文nulltrueapkUrlapk的下载地址nulltrueapkNameapk下载好的名字nulltruedownloadPathapk下载的位置getExternalCacheDir()falseshowNewerToast是否提示用户 "当前已是最新版本"falsefalsesmallIcon通知栏的图标(资源id)-1trueconfiguration这个库的额外配置nullfalseapkVersionCode更新apk的versionCode (如果设置了那么库中将会进行版本判断下面的属性也就需要设置了)1falseapkVersionName更新apk的versionNamenullfalseapkDescription更新描述nullfalseapkSize新版本的安装包大小(单位M)nullfalseauthorities兼容Android N uri授权应用包名falseUpdateConfiguration:配置文档属性描述默认值notifyId通知栏消息id1011notificationChannel适配Android O的渠道通知详情查阅源码httpManager设置自己的下载过程nullbreakpointDownload是否需要支持断点下载trueenableLog是否需要日志输出trueonDownloadListener下载过程的回调nulljumpInstallPage下载完成是否自动弹出安装页面trueshowNotification是否显示通知栏进度(后台下载提示)trueforcedUpgrade是否强制升级falseonButtonClickListener按钮点击事件回调nulldialogImage对话框背景图片资源(图片规范参考demo)-1dialogButtonColor对话框按钮的颜色-1dialogButtonTextColor对话框按钮的文字颜色-1所有版本:点击查看使用步骤第一步: app/build.gradle进行依赖implementation 'com.azhon:appupdate:1.7.3'第二步:创建DownloadManager,更多用法请查看这里示例代码DownloadManager manager = DownloadManager.getInstance(this); manager.setApkName("appupdate.apk")         .setApkUrl("https://raw.githubusercontent.com/azhon/AppUpdate/master/apk/appupdate.apk")         .setSmallIcon(R.mipmap.ic_launcher)         //可设置,可不设置         .setConfiguration(configuration)         .download();第三步:兼容Android N 及以上版本,在你应用的Manifest.xml添加如下代码<--! android:authorities="${applicationId}"  这个值必须与DownloadManager中的authorities一致(不设置则为应用包名)--> <provider     android:name="android.support.v4.content.FileProvider"     android:authorities="${applicationId}"     android:exported="false"     android:grantUriPermissions="true">     <meta-data         android:name="android.support.FILE_PROVIDER_PATHS"         android:resource="@xml/file_paths_public" /> </provider>第四步:资源文件res/xml/file_paths_public.xml内容<?xml version="1.0" encoding="utf-8"?> <paths>     <external-path         name="app_update_external"         path="/" />     <external-cache-path         name="app_update_cache"         path="/" /> </paths>兼容Android O及以上版本,需要设置NotificationChannel(通知渠道);库中已经写好可以前往查阅NotificationUtil.java温馨提示:升级对话框中的内容是可以上下滑动的哦!如果需要实现自己一套下载过程,只需要继承BaseHttpDownloadManager 并使用listener更新进度public class MyDownload extends BaseHttpDownloadManager {}
<think>首先,用户的问题是:“我想查找在Android Studio中检查自制APP更新的方法 请问在Android Studio中检查自制APP更新的方法”。这翻译成英文是:“I want to find a method to check for updates of self-made apps in Android Studio. Can you tell me how to check for updates of self-made apps in Android Studio?” 用户提供了三个引用,但我需要参考这些引用来辅助回答。引用[1]是关于博主介绍,提到专注于Android开发。引用[2]是关于使用Android Studio制作一个WebView浏览器,包括AndroidManifest.xml代码。引用[3]是关于制作简易计算器,但似乎有图片缺失。 系统指令要求: - 所有行内数学表达式使用$...$格式(但这个问题不涉及数学,所以可能不需要)。 - 独立公式使用$$...$$格式(同样,可能不适用)。 - LaTeX语法正确(无数学内容)。 - 使用中文回答。 - 生成相关问题。 - 回答中引用的段落末尾自然地添加引用标识。 - 回答结构清晰,帮助用户逐步解决问题。 用户的问题集中在如何在Android Studio中实现APP更新检查。参考提供的引用: - 引用[2]提到了AndroidManifest.xml,其中包含网络权限(android.permission.INTERNET),这对于检查更新很重要,因为通常需要从服务器获取更新信息。 - 其他引用是关于博主介绍和计算器的,但引用[3]可能暗示了项目实践,不过细节不足。 关键点:APP更新检查通常涉及从后端服务器获取最新版本信息,并与本地APP版本比较。然后在Android Studio中实现时,可以使用网络请求(如Retrofit或HttpURLConnection)、解析JSON响应、并显示更新提示。 步骤需要清晰: 1. **添加权限**:如引用[2]中的网络权限。 2. **实现版本检查逻辑**:创建一个服务或函数来检查版本。 3. **处理更新**:提示用户下载新版本。 4. **可能使用库**:如Firebase App Distribution或其他第三方库。 引用用户提供的引用: - 引用[2]有AndroidManifest.xml代码,包含INTERNET权限,所以我可以引用这个。 - 引用[1]介绍博主有经验,可以间接支持可靠性。 - 引用[3]提到计算器,但不直接相关。 在回答中,自然地添加引用标识。例如,提到权限时引用[2]。 最终回答结构: - 逐步解释方法。 - 提供代码示例(使用代码块)。 - 生成相关问题。 相关问题生成:基于主题,生成类似示例的问题。 完整回答: 1. 介绍方法。 2. 详细步骤。 3. 代码示例。 4. 相关问题。 </think>### 在Android Studio中检查自制APP更新的方法 在Android Studio中实现APP更新的检查,主要涉及从后端服务器获取最新版本信息,并与本地APP版本比较。如果检测到新版本,需要提示用户更新(如下载APK或跳转到应用商店)。以下是一个结构清晰的逐步实现方法,参考了您提供的引用内容。我会确保每一步可靠且可行,基于标准的Android开发实践。 #### 步骤1: 添加必要的权限和配置 - **网络权限**:APP需要访问互联网来查询更新信息。引用[2]中展示了如何添加网络权限到`AndroidManifest.xml`文件: ```xml <uses-permission android:name="android.permission.INTERNET" /> ``` 这确保了APP可以发送HTTP请求到您的服务器[^2]。 - **HTTP明文传输**:如果您的更新服务器使用HTTP而非HTTPS,需添加`android:usesCleartextTraffic="true"`到 `<application>` 标签中(如引用[2]所示),否则Android默认阻止非加密请求[^2]。 #### 步骤2: 实现版本检查逻辑 在代码中创建一个函数来比较版本号。通常,您需要一个后端API返回最新版本信息(如JSON格式)。以下是核心步骤: - **获取本地版本号**:使用Android的`PackageManager`获取当前APP版本。 - **发送网络请求**:使用`HttpURLConnection`或库(如Retrofit)查询服务器版本。 - **比较版本**:如果服务器版本高于本地版本,则触发更新提示。 下面是Java代码示例(在Activity或Service中实现): ```java import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.os.AsyncTask; import android.util.Log; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class UpdateChecker { private Context context; public UpdateChecker(Context context) { this.context = context; } public void checkForUpdate() { new CheckUpdateTask().execute(); } private class CheckUpdateTask extends AsyncTask<Void, Void, String> { @Override protected String doInBackground(Void... voids) { try { // 步骤1: 获取本地版本号 PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); String localVersion = packageInfo.versionName; // 例如 "1.0.0" // 步骤2: 查询服务器API(替换为您自己的URL) URL url = new URL("https://your-server.com/api/version"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); // 假设服务器返回JSON: {"latest_version": "1.1.0", "download_url": "..."} String serverResponse = response.toString(); // 解析JSON(简化示例) String serverVersion = serverResponse.split("\"latest_version\":\"")[1].split("\"")[0]; // 步骤3: 比较版本号 if (compareVersions(serverVersion, localVersion) > 0) { return serverResponse; // 有更新 } } catch (Exception e) { Log.e("UpdateChecker", "Error checking update", e); } return null; // 无更新 } @Override protected void onPostExecute(String result) { if (result != null) { // 触发更新提示(例如弹窗) showUpdateDialog(result); // 实现此方法以显示下载链接 } } // 版本号比较函数 (例如 "1.1.0" > "1.0.0") private int compareVersions(String ver1, String ver2) { String[] parts1 = ver1.split("\\."); String[] parts2 = ver2.split("\\."); for (int i = 0; i < Math.min(parts1.length, parts2.length); i++) { int part1 = Integer.parseInt(parts1[i]); int part2 = Integer.parseInt(parts2[i]); if (part1 != part2) return part1 - part2; } return parts1.length - parts2.length; } } private void showUpdateDialog(String response) { // 实现弹窗逻辑,提示用户下载新版本 // 例如:解析response中的download_url,启动下载Intent } } ``` **用法**:在您的Activity中初始化并调用: ```java UpdateChecker checker = new UpdateChecker(this); checker.checkForUpdate(); // 例如在onCreate()中调用 ``` 此代码使用了异步任务`AsyncTask`以避免阻塞UI线程。引用[2]中的权限配置确保此网络请求能正常工作[^2]。 #### 步骤3: 处理更新提示和下载 - **显示更新弹窗**:在`showUpdateDialog`方法中,使用`AlertDialog`提示用户,并提供下载按钮。 - **下载APK**:启动浏览器或下载管理器下载新APK文件(确保服务器提供APK下载链接)。 - **安装APK**:用户下载后,需处理安装权限(Android 8.0+需添加`REQUEST_INSTALL_PACKAGES`权限)。 #### 最佳实践和注意事项 - **安全性**:使用HTTPS加密请求,防止中间人攻击。 - **版本管理**:在服务器端维护一个简单的API,返回JSON格式的版本数据。 - **用户体验**:在后台检查更新(如APP启动时),避免频繁打扰用户。 - **测试**:在Android Studio中模拟不同版本场景,确保逻辑可靠。博主在引用[1]中强调了Android开发经验,这有助于优化实现[^1]。 - **第三方库**:如果不想手动实现,可使用Firebase App Distribution或第三方库(如[AppUpdater](https://github.com/javiersantos/AppUpdater)),简化流程。 ### 相关问题 1. 如何优化APP更新检查的网络请求性能? 2. 在Android中如何处理APP更新时的用户权限和安全问题? 3. 如何使用Firebase实现APP的自动更新功能? 如果需要更多代码细节或特定场景的示例,请随时补充!
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值