Android Studio的代码笔记--APK下载安装

清单AndroidManifest

权限添加

<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"/>

在application里添加

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

这个里面的apkdownloadapplication是我项目的包名,要改成自己项目的包名

xml/file_paths

具体怎么新建可以看这个链接: 安装apk.

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

文件名为file_paths.xml

DownloadService

新建new-Service-Service(IntentService)命名为DownloadService
在这里插入图片描述
添加定义

public static final String BROADCAST_ACTION = "com.example.android.threadsample.BROADCAST";
public static final String EXTENDED_DATA_STATUS = "com.example.android.threadsample.STATUS";
private LocalBroadcastManager mLocalBroadcastManager;

将onHandleIntent(Intent intent){}内容修改,并添加安装apk函数

@Override
protected void onHandleIntent(Intent intent) {
	String url = intent.getDataString();//下载地址
	Log.i("LXH", "url"+url);
    DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
	DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
	//设置APK缓存路径和应用名称:/storage/emulated/0/Download/Android_8.7.0.5295_537068059.apk
	request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Android_8.7.0.5295_537068059.apk");
	String UU =  Environment.DIRECTORY_DOWNLOADS+"/Android_8.7.0.5295_537068059.apk";
	Log.i("LXH", "位置:"+UU);
	//设置网络下载环境WIFI/移动数据
	request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
	//设置显示通知栏,下载完成后通知栏自动消失
	request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
	request.setTitle("下载");//通知栏标题
    request.setDescription("应用正在下载");//下载描述
    request.setMimeType("application/vnd.android.package-archive");//类型为.apk
    //获得唯一下载id
    long requestId = downloadManager.enqueue(request);
    //将id放进Intent
    Intent localIntent = new Intent(BROADCAST_ACTION);
    localIntent.putExtra(EXTENDED_DATA_STATUS, requestId);
    //查询下载信息
    DownloadManager.Query query = new DownloadManager.Query();
    //只包括带有给定id的下载
	query.setFilterById(requestId);
    try {
		boolean isGoging = true;Log.i("LXH", "给定id的下载");
        while (isGoging) {
        	Cursor cursor = downloadManager.query(query);
            if (cursor != null && cursor.moveToFirst()) {
            	int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
				switch (status) {//下载状态为成功
                	case DownloadManager.STATUS_SUCCESSFUL:
                    	isGoging = false;
                        Log.i("LXH", "cg");
                        String UUU="/storage/emulated/0/"+UU;
                        //String UUU="/storage/emulated/0/mwh/Android_8.7.0.5295_537068059.apk";
                        installApk(UUU);
                        //调用LocalBroadcastManager.sendBroadcast将intent传递回去
                        mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
                        mLocalBroadcastManager.sendBroadcast(localIntent);
                        break;
					default://Log.i("LXH", "----");
                        break;
                }
            }
            if (cursor != null) {
                cursor.close();
            }
        }
     } catch (Exception e) {
         e.printStackTrace();
     }
}
private void installApk(String fileSavePath) {//安装
    File file = new File(fileSavePath);
    Log.i("LXH", "fileSavePath:"+fileSavePath);
    Intent intent1= new Intent(Intent.ACTION_VIEW);
    intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Uri data;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//版本大于等于7.0
        // 通过FileProvider创建一个content类型的Uri
        data = FileProvider.getUriForFile(DownloadService.this, "sven.com.apkdownloadapplication.fileprovider", file);
        intent1.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);// 临时授权
    } else {data = Uri.fromFile(file); }
    intent1.setDataAndType(data, "application/vnd.android.package-archive");
    startActivity(intent1);
}

这个里面的apkdownloadapplication是我项目的包名,要改成自己项目的包名

main.xml

添加一个按钮

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/btApk"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下载安装" />
</LinearLayout>

MainActivity

public class MainActivity extends AppCompatActivity {
    MyReceiver receiver = new MyReceiver();
    Button btApk;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btApk=findViewById(R.id.btApk);
        btApk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent serviceIntent = new Intent(MainActivity.this, DownloadService.class);
                //apk下载地址
                serviceIntent.setData(Uri.parse("https://down.qq.com/qqweb/QQ_1/android_apk/Android_8.7.0.5295_537068059.apk"));
                startService(serviceIntent);//开启服务
            }
        });
        requestPermission();
        regist();
    }
    private void requestPermission() {
        Log.i("LXH", "进入动态申请权限");
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            Log.i("LXH", "动态申请权限开启");
        }
    }
    public class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "Android_8.7.0.5295_537068059.apk");
            try {//获取权限
                Runtime.getRuntime().exec("chmod 777" + file.getCanonicalPath());
            } catch (IOException e) {
                e.printStackTrace();
            }
            intent = new Intent(Intent.ACTION_VIEW); //设置标签
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//设置,新任务的开始
            if (Build.VERSION.SDK_INT >= 24) {//版本是否在7.0以上
                Uri apkUri = FileProvider.getUriForFile(MainActivity.this, "sven.com.apkdownloadapplication.fileprovider", file);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//对目标应用临时授权该Uri所代表的文件
                intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
            } else {
                intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            }
            startActivity(intent);
        }
    }
    private void regist() {//注册广播
        IntentFilter intentFilter = new IntentFilter(DownloadService.BROADCAST_ACTION);
        intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
        LocalBroadcastManager.getInstance(this).registerReceiver(receiver, intentFilter);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
    }
}

这个里面的apkdownloadapplication是我项目的包名,要改成自己项目的包名

运行&代码下载

效果:点击按钮下载并安装apk,这里我用的是qq的apk
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
日志
cg是下载成功
在这里插入图片描述
找不到之前看到的参考资源网页,不然可以接着往下
推荐可以先看安装apk那篇: 安装apk.

有需要项目: 下载安装APK.也可以私信邮箱我看到就发
欢迎指错,一起学习

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值