码迷,mamicode.com
首页 > 移动开发 > 详细

Android DownloadManager下载并自动弹出安装

时间:2015-05-25 16:27:55      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:

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>

    技术分享

   技术分享

  技术分享

 

Android DownloadManager下载并自动弹出安装

标签:

原文地址:http://www.cnblogs.com/qstext/p/4527929.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!