php 自动下载apk,Android 下载apk 自动 安装

Android N 后,由于不能访问私有路径,需要设置成共享文件

/**

* android N 执行此安装方法

*

* @param context 上下文

* @param file 文件路径

*/

public static void installAPK(Context context, File file) {

// 获取下载好的 apk 路径

Intent intentN = new Intent(Intent.ACTION_VIEW);

// 由于没有在Activity环境下启动Activity,设置下面的标签

intentN.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

//参数1 上下文, 参数2 Provider主机地址 和配置文件中保持一致 参数3 共享的文件

Uri apkUri = FileProvider.getUriForFile(context, "world.letsgo.booster.android.FileProvider", file);

//添加这一句表示对目标应用临时授权该Uri所代表的文件

intentN.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

intentN.setDataAndType(apkUri, "application/vnd.android.package-archive");

context.startActivity(intentN);

}

/**

* android N 以下安装apk

*/

public static void promptInstall(Context context, Uri date) {

Intent promptInstall = new Intent(Intent.ACTION_VIEW)

.setDataAndType(date, "application/vnd.android.package-archive");

// FLAG_ACTIVITY_NEW_TASK 可以保证安装成功时可以正常打开 app

promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(promptInstall);

}

有可能系统下载功能被禁用,故需要判断该系统功能是否可用

/**

* 判断 系统下载功能是否可用

*

* @return true 可用 false 不可用

*/

static boolean downLoadMangerIsEnable(Context context) {

try {

int state = context.getApplicationContext().getPackageManager()

.getApplicationEnabledSetting("com.android.providers.downloads");

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {

return !(state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED ||

state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER

|| state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED);

} else {

return !(state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED ||

state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER);

}

} catch (Exception e) {

LoggerUtils.debugAndSave(e.getMessage());

}

return false;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现 APK 内部自动更新的源代码步骤如下: 1. 在应用程序中创建一个版本检查服务,该服务将检查应用程序当前安装的版本和可用的最新版本之间的差异。 2. 为应用程序创建一个下载服务,该服务将从服务器下载新版本的 APK 文件。 3. 创建一个安装服务,该服务将下载APK 文件安装到用户的设备上。 4. 在应用程序的主 Activity 中添加代码,以便在应用程序启动时启动版本检查服务。 下面是一个简单的示例代码,可以帮助你了解如何实现 APK 内部自动更新功能: 1. 版本检查服务的代码: ```java public class VersionCheckService extends Service { private static final String TAG = "VersionCheckService"; private static final String UPDATE_URL = "http://yourserver.com/update.php"; private int currentVersionCode; private int latestVersionCode; @Override public int onStartCommand(Intent intent, int flags, int startId) { checkForUpdate(); return super.onStartCommand(intent, flags, startId); } private void checkForUpdate() { currentVersionCode = BuildConfig.VERSION_CODE; latestVersionCode = getLatestVersionCode(); if (latestVersionCode > currentVersionCode) { Intent intent = new Intent(this, DownloadService.class); intent.putExtra("updateUrl", UPDATE_URL); startService(intent); } } private int getLatestVersionCode() { // TODO: Implement version checking logic // This method should return the latest version code available on the server return -1; } @Nullable @Override public IBinder onBind(Intent intent) { return null; } } ``` 2. 下载服务的代码: ```java public class DownloadService extends Service { private static final String TAG = "DownloadService"; private String updateUrl; private int notificationId = 0; @Override public int onStartCommand(Intent intent, int flags, int startId) { updateUrl = intent.getStringExtra("updateUrl"); downloadUpdate(); return super.onStartCommand(intent, flags, startId); } private void downloadUpdate() { NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle(getString(R.string.app_name)) .setContentText(getString(R.string.downloading_update)) .setProgress(0, 0, true) .setOngoing(true); notificationManager.notify(notificationId, builder.build()); DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); DownloadManager.Request request = new DownloadManager.Request(Uri.parse(updateUrl)); request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, "update.apk"); long downloadId = downloadManager.enqueue(request); BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { long completedDownloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); if (completedDownloadId == downloadId) { notificationManager.cancel(notificationId); installUpdate(); } } }; registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); } private void installUpdate() { File apkFile = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "update.apk"); Uri apkUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileprovider", apkFile); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); stopSelf(); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } } ``` 3. 在应用程序的主 Activity 中添加以下代码: ```java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(this, VersionCheckService.class); startService(intent); } ``` 以上就是实现 APK 内部自动更新功能的基本步骤和示例代码。需要注意的是,示例代码中的一些细节可能需要根据你的应用程序的实际情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值