Android 通知块用户界面

Android 通知块用户界面,android,android-asynctask,android-notifications,android-intentservice,Android,Android Asynctask,Android Notifications,Android Intentservice,我的应用程序有一个下载文件的选项。它是这样工作的: Activity->IntentService->AsyncTask(文件正在下载)。此外,我使用通知以百分比表示进度,这是我的问题:在Android 2.3上一切都很好,但在Android 4.2或5.0上,UI被阻止 以前我的代码中有一个bug(我每次都更新循环中的进度条),现在我只在oldProgress!=实际上,它运行良好(100多个操作,而不是2000多个操作)。但为什么它在Android 2.3上运行良好,即使有2000多个操作

我的应用程序有一个下载文件的选项。它是这样工作的: Activity->IntentService->AsyncTask(文件正在下载)。此外,我使用通知以百分比表示进度,这是我的问题:在Android 2.3上一切都很好,但在Android 4.2或5.0上,UI被阻止

以前我的代码中有一个bug(我每次都更新循环中的进度条),现在我只在oldProgress!=实际上,它运行良好(100多个操作,而不是2000多个操作)。但为什么它在Android 2.3上运行良好,即使有2000多个操作

这是我的通知类:

public class NotificationProgressHelper {
private static final int DOWNLOAD_PROGRESS_NOTIFICATION_ID = 1;

private Context mContext;
private Notification mNotification;
private NotificationManager mNotificationManager;
private PendingIntent mContentIntent;
private CharSequence mContentTitle;

public NotificationProgressHelper(Context context) {
    mContext = context;
}

public void createNotification() {
    mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    int icon = R.drawable.download_icon;
    CharSequence contentName = mContext.getString(R.string.notification_content_name);
    mNotification = new Notification(icon, contentName, System.currentTimeMillis());

    mContentTitle = mContext.getString(R.string.notification_title);
    CharSequence contentText = mContext.getString(R.string.notification_percent_completed, 0);

    //pending intent left blank till the whole apk file will be downloaded
    Intent notificationIntent = new Intent();
    mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);

    mNotification.setLatestEventInfo(mContext, mContentTitle, contentText, mContentIntent);
    mNotification.flags = Notification.FLAG_ONGOING_EVENT;

    mNotificationManager.notify(DOWNLOAD_PROGRESS_NOTIFICATION_ID, mNotification);
}

public void progressUpdate(int percentageComplete) {
    CharSequence contentText = mContext.getString(R.string.notification_percent_completed, percentageComplete);
    mNotification.setLatestEventInfo(mContext, mContentTitle, contentText, mContentIntent);
    mNotificationManager.notify(DOWNLOAD_PROGRESS_NOTIFICATION_ID, mNotification);
}

public void completed() {
    mNotificationManager.cancel(DOWNLOAD_PROGRESS_NOTIFICATION_ID);
}

public void removeDownloadSuccessNotification() {
    mNotificationManager.cancel(ApplicationConstants.NotificationID.APP_UPDATE_NOTIFICATION_ID);
}
当然,downloadFile方法是在doInBackground()中调用的

我知道这段代码很旧,现在我应该使用生成器,但我尝试过,它与问题无关


我做错什么了吗?为什么在安卓2.3上所有操作的版本都运行良好?我认为它在主线上有效,但为什么?

在甜甜圈和till Honeycomb(1.6–3.0)之后,直到3.0版 默认情况下,它并行使用多个AsyncTask,这可能就是它工作正常的原因。。
有关这方面的详细说明,请参见,

请粘贴您的AsyncTask代码。在我的情况下,是否可以在单独的线程中运行通知?