/**
* 下载APK
**/
private void downloadApk() {
// String apkUrl = "http://192.168.1.1/downloadtest.apk";
String apkUrl = "http://apk-cdn.zhangxinhulian.com/com.zxhl.weather-guanwang-1.0.6_106_jiagu.apk";
Uri uri = Uri.parse(apkUrl);
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
// 设置允许使用的网络类型,这里是移动网络和wifi都可以
request.setAllowedNetworkTypes(request.NETWORK_MOBILE | request.NETWORK_WIFI);
//设置是否允许漫游
request.setAllowedOverRoaming(false);
//设置文件类型
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(apkUrl));
request.setMimeType(mimeString);
//在通知栏中显示
request.setNotificationVisibility(request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setTitle("download...");
request.setVisibleInDownloadsUi(true);
//sdcard目录下的download文件夹
// SDCardUtils.deleteFile(qq_otherList.get(i).getPath());
String installPath="sdcard/download/"+pathFile+".apk";
if (fileIsExists(installPath)){
SDCardUtils.deleteFile(installPath);
}
request.setDestinationInExternalPublicDir("/sdcard/download/", pathFile + ".apk");
// 将下载请求放入队列
cachedThreadPool.submit(new Runnable() {
@Override
public void run() {
downloadManager.enqueue(request);
}
});
}
/**
* 判断文件是否存在
* */
public boolean fileIsExists(String strFile) {
try {
File f=new File(strFile);
if(!f.exists()) {
return false;
}
}
catch (Exception e) {
return false;
}
return true;
}
/**
* 删除文件
* @param path
*/
public static void deleteFile(String path){
File file = new File(path);
if (file.exists()) {
file.delete();
}
}
/**
* 下载类型广告执行下载任务
*/
private void startDownload() {
String apkUrl = "http://apk-cdn.zhangxinhulian.com/com.zxhl.weather-guanwang-1.0.6_106_jiagu.apk";
Uri uri = Uri.parse(click_url);
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
// 设置允许使用的网络类型,这里是移动网络和wifi都可以
request.setAllowedNetworkTypes(request.NETWORK_MOBILE | request.NETWORK_WIFI);
//设置是否允许漫游
request.setAllowedOverRoaming(false);
//设置文件类型
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(apkUrl));
request.setMimeType(mimeString);
//在通知栏中显示
request.setNotificationVisibility(request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setTitle("download...");
request.setVisibleInDownloadsUi(true);
//sdcard目录下的download文件夹
// SDCardUtils.deleteFile(qq_otherList.get(i).getPath());
FileUtils.saveFile(context,"ad.apk");
request.setDestinationInExternalPublicDir("/sdcard/download/", "ad" + ".apk");
// 将下载请求放入队列
fixedThreadPool.execute(new Runnable() {
@Override
public void run() {
downloadManager.enqueue(request);
}
});
}
package com.example.myadsdk.utils;
import android.content.Context;
import android.util.Log;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
/**
* author : zhangjian
* time : 2020/12/11
* description :
*/
public class FileUtils {
/**
* @param content 保存字符串
*/
public static void saveContent(Context context,String content){
// /data/data/包名/files
String save_path=context.getFilesDir() +"/log.txt";
try {
File f = new File(save_path);
FileWriter fw = new FileWriter(f, true);
PrintWriter pw = new PrintWriter(fw);
pw.println(content);
pw.flush();
fw.flush();
pw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
Log.e("TAG", "saveContent写入成功: " );
}
/**
* 保存文件到报名下
* @param context
* @param fileName
*/
public static void saveFile(Context context,String fileName){
String save_path=context.getFilesDir() +"";
File dirFile = new File(save_path);
File file = new File(fileName);
if (!dirFile.exists()) {
dirFile.mkdirs();
try {
file.createNewFile();
} catch (Exception e) {
}
}
}
}