Android DownloadManager下载并自动弹出安装

1.版本2.3以上

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

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

    <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION"/>

只能贴代码,本想上传demo的,找不到地方

public class MainActivity extends Activity {

    private Button mBut_download;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

 

mBut_download = (Button)findViewById(R.id.mBut_download);

 

mBut_download.setOnClickListener(new OnClickListener() {

 

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

intoDownloadManager();

}

});

}

 

 

@SuppressLint("NewApi")

@TargetApi(Build.VERSION_CODES.GINGERBREAD)

private void intoDownloadManager(){

DownloadManager dManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

         Uri uri = Uri.parse("http://dingphone.ufile.ucloud.com.cn/apk/guanwang/time2plato.apk");

         Request request = new Request(uri);

          // 设置下载路径和文件名

         request.setDestinationInExternalPublicDir("download", "time2plato.apk");

         request.setDescription("柏拉图新版本下载");

         request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

         request.setMimeType("application/vnd.android.package-archive");

          // 设置为可被媒体扫描器找到

         request.allowScanningByMediaScanner();

         // 设置为可见和可管理

         request.setVisibleInDownloadsUi(true);

         long refernece = dManager.enqueue(request);

         // 把当前下载的ID保存起来

         SharedPreferences sPreferences = getSharedPreferences("downloadplato", 0);

         sPreferences.edit().putLong("plato", refernece).commit();

 

}

 

public class DownLoadBroadcastReceiver  extends BroadcastReceiver {

 

    @SuppressLint("NewApi")

    public void onReceive(Context context, Intent intent) {

        long myDwonloadID = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);

        SharedPreferences sPreferences = context.getSharedPreferences("downloadplato", 0);

        long refernece = sPreferences.getLong("plato", 0);

        if (refernece == myDwonloadID) {

            String serviceString = Context.DOWNLOAD_SERVICE;

            DownloadManager dManager = (DownloadManager) context.getSystemService(serviceString);

            Intent install = new Intent(Intent.ACTION_VIEW);

            Uri downloadFileUri = dManager.getUriForDownloadedFile(myDwonloadID);

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

            install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            context.startActivity(install);        }

    }

 

}

最后记者一定要注册广播

 

<receiver

                   android:name="com.example.apkdownloadmanager.DownLoadBroadcastReceiver">

                    <intent-filter>

                         <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />

                     </intent-filter>

                 </receiver>

    技术分享

   技术分享

  技术分享

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。