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>

    

   

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM