日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Android app下载并安装

發布時間:2023/12/24 综合教程 36 生活家
生活随笔 收集整理的這篇文章主要介紹了 Android app下载并安装 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1 下載功能

//下載apk
    private void downloadApk(String apkUrl) throws PackageManager.NameNotFoundException {

        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(true);
        //設置文件類型
        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文件夾
        request.setDestinationInExternalPublicDir("/download", "SanjuScanApp_Android.apk");
        // 將下載請求放入隊列
        downloadManager.enqueue(request);
    }

2 設置接收廣播

public class InstallReceiver extends BroadcastReceiver {

    // 安裝下載接收器
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
            installApk(context);
        }
    }

    // 安裝Apk
    private void installApk(Context context) {

        try {
            Intent i = new Intent(Intent.ACTION_VIEW);
            String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/download/SanjuScanApp_Android.apk";
            i.setDataAndType(Uri.parse("file://" + filePath), "application/vnd.android.package-archive");
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

3 注冊廣播(AndroidManifest.xml的receiver節點)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tyler.myapplication">

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

    <application>
        <activity
            android:name=".LoginActivity"
        </activity>
        
        <receiver
            android:name="com.example.tyler.HelperTool.InstallReceiver">
            <intent-filter>
                <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
            </intent-filter>
        </receiver>
    </application>

總結

以上是生活随笔為你收集整理的Android app下载并安装的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。