这个小demo有bug?就是点击下载后,在通知栏那块很卡?因为大神指点,多谢
点击打开链接,下载demo......................
activity_main.xml主界面只有一个Button
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <Button android:id="@+id/intentService" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginTop="10dp" android:text="intentService" /> </LinearLayout>
package com.example.indownloaddemo; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { Context context; Button intentButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = this; initView(); } private void initView() { intentButton = (Button) findViewById(R.id.intentService); intentButton.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.intentService: Intent intent = new Intent(context, MyService.class); startService(intent); break; } } }
package com.example.indownloaddemo; import com.thin.downloadmanager.DownloadManager; import com.thin.downloadmanager.DownloadRequest; import com.thin.downloadmanager.DownloadStatusListener; import com.thin.downloadmanager.ThinDownloadManager; import com.thin.downloadmanager.DownloadRequest.Priority; import android.app.IntentService; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.util.Log; public class MyService extends IntentService { //环境变量 Context context; //apk存放路徑 String urlPath; //apk的url地址 private static final String FILE1 = "http://gdown.baidu.com/data/wisegame/f98d235e39e29031/baiduxinwen.apk"; //下載管理器 private ThinDownloadManager downloadManager; //線程池數量 private static final int DOWNLOAD_THREAD_POOL_SIZE = 3; //下载监听 MyDownloadListner myDownloadStatusListener = new MyDownloadListner(); int downloadId1 = 0; private DownloadRequest request1; //通知管理类 EppNotificationControl notificationControl; public MyService() { super("MyService"); } @Override public void onCreate() { context = this; super.onCreate(); } //IntentService里面是可以进行耗时的操作的 @Override protected void onHandleIntent(Intent intent) { urlPath = Util.createSDCardDir("epp.apk"); //创建通知对象包含 (时间、图标) notificationControl = new EppNotificationControl(urlPath, context); downloadManager = new ThinDownloadManager(DOWNLOAD_THREAD_POOL_SIZE); initDownload(); if (downloadManager.query(downloadId1) == DownloadManager.STATUS_NOT_FOUND) { downloadId1 = downloadManager.add(request1); } notificationControl.showProgressNotify(); } private void initDownload() { Uri downloadUri = Uri.parse(FILE1); Uri destinationUri = Uri.parse(urlPath); request1 = new DownloadRequest(downloadUri) .setDestinationURI(destinationUri).setPriority(Priority.HIGH) .setDownloadListener(myDownloadStatusListener); } class MyDownloadListner implements DownloadStatusListener { @Override public void onDownloadComplete(int id) { if (id == downloadId1) { Log.e("TAG", "download completed"); } } @Override public void onDownloadFailed(int id, int errorCode, String errorMessage) { if (id == downloadId1) { Log.e("TAG", "DownloadFailed" + errorMessage); } } @Override public void onProgress(int id, long totalBytes, long downloadedBytes, int progress) { if (id == downloadId1) { Log.e("TAG", progress + ""); notificationControl.updateNotification(progress); } } } }
package com.example.indownloaddemo; import android.app.Notification; import android.app.NotificationManager; import android.content.Context; import android.support.v4.app.NotificationCompat; import android.util.Log; public class EppNotificationControl { NotificationManager mNotificationManager; NotificationCompat.Builder mBuilder; String urlPath; int progress; final int NOTIFYCATIONID = 1001; Context context; public EppNotificationControl(String urlPath, Context context) { this.urlPath = urlPath; this.context = context; initNotifycation(); } /** * 初始化通知对象 * 时间、图标 */ private void initNotifycation() { mNotificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); mBuilder = new NotificationCompat.Builder(context); //通知产生的时间,会在通知信息里显示,一般是系统获取到的时间 mBuilder.setWhen(System.currentTimeMillis()).setSmallIcon( R.drawable.chevron_default); } public void showProgressNotify() { mBuilder.setContentTitle("等待下载").setContentText("进度:") .setTicker("开始下载");// 通知首次出现在通知栏,带上升动画效果的 Notification mNotification = mBuilder.build(); // 确定进度的 mBuilder.setProgress(100, progress, false); // 这个方法是显示进度条 设置为true就是不确定的那种进度条效果 mNotificationManager.notify(NOTIFYCATIONID, mNotification); } /** 设置下载进度 */ public void updateNotification(int progress) { Notification mNotification = mBuilder.build(); mNotification.flags = Notification.FLAG_ONGOING_EVENT;// mBuilder.setProgress(100, progress, false); // 这个方法是显示进度条 mBuilder.setContentText("下载中...").setContentTitle("AAAAA"); mNotificationManager.notify(NOTIFYCATIONID, mNotification); } /** 下载完毕 */ public void doneNotification() { Notification mNotification = mBuilder.build(); mNotification.flags = Notification.FLAG_ONGOING_EVENT;// mBuilder.setProgress(100, progress, false); // 这个方法是显示进度条 mBuilder.setContentText("下载完毕...").setContentTitle("完成"); mNotificationManager.cancel(NOTIFYCATIONID); mNotificationManager.cancelAll(); Util.installApk(urlPath, context, mNotificationManager, NOTIFYCATIONID); } }
Util工具类
package com.example.indownloaddemo; import java.io.File; import android.app.NotificationManager; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Environment; import android.util.Log; public class Util { public static String createSDCardDir(String name) { File sdcardDir = Environment.getExternalStorageDirectory(); String path = sdcardDir.getPath() + "/MUDOWN"; File file = null; try { if (Environment.MEDIA_MOUNTED.equals(Environment .getExternalStorageState())) { File dir = new File(path); if (!dir.exists()) { dir.mkdirs(); } file = new File(dir, name); if (file.exists()) { file.delete(); } file.createNewFile(); } } catch (Exception e) { } return file.getPath(); } public static void installApk(String urlPath, Context context, NotificationManager mNotificationManager, int NOTIFYCATIONID) { Intent apkIntent = new Intent(); apkIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); apkIntent.setAction(android.content.Intent.ACTION_VIEW); File apkFile = new File(urlPath); Uri uri = Uri.fromFile(apkFile); apkIntent .setDataAndType(uri, "application/vnd.android.package-archive"); context.startActivity(apkIntent); mNotificationManager.cancel(NOTIFYCATIONID);// 删除一个特定的通知ID对应的通知 }; }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.indownloaddemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="14" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INSTALL_PACKAGES" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.indownloaddemo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.example.indownloaddemo.MyService"/> </application> </manifest>