【问题标题】:Custom status bar notification for background download用于后台下载的自定义状态栏通知
【发布时间】:2011-10-08 19:07:01
【问题描述】:

我是 android 开发的新手,我尝试为我的应用创建后台下载功能。我按照这个http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView 创建了我的自定义通知。

下载完成,我检查了sdcard中的下载文件。此外,状态栏图标和标题已正确更改。

问题是我为通知提供的自定义布局没有出现(在栏下展开)。这是私有AsyncTask类中的相关代码部分:

    @Override
    protected void onPreExecute() {
        // create and configure the notification
        notification = new Notification(R.drawable.download, "Downloading map..", System.currentTimeMillis());
        notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;

        //create a custom layout for the notification
        myContentView = new RemoteViews(appContext.getPackageName(), R.layout.download_progress);
        myContentView.setImageViewResource(R.id.status_icon, R.drawable.ic_menu_save);
        myContentView.setTextViewText(R.id.status_text, "download in progress");
        myContentView.setProgressBar(R.id.status_progress, 100, 0, false);
        notification.contentView = myContentView;
        notification.contentView.apply(appContext, dl.getListView());

        //instantiate the pending intent
        Intent myIntent = new Intent(appContext, DownloadList.class);
        myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        int requestID = (int) System.currentTimeMillis();
        PendingIntent myPendingIntent = PendingIntent.getActivity(appContext, requestID, myIntent, 0);
        notification.contentIntent = myPendingIntent;

        //add the Notification object to the notification manager
        notificationManager = (NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIF_ID, notification);
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        //update progress bar
        notification.contentView.setProgressBar(R.id.status_progress, 100, progress[0], false);
        notificationManager.notify(NOTIF_ID, notification);
    }

}

请注意,我的 DownloadList 类扩展了 ListActivity。

除了“notification.contentView = myContentView;”之外,我还需要做更多的事情吗?为了让布局膨胀?

【问题讨论】:

  • 那是一大堆代码要在那里整理!尝试更仔细地查明问题;它会帮助你找到答案
  • 按照您的建议,我隔离了与通知相关的代码部分以帮助解决问题。

标签: android layout notifications progress-bar status


【解决方案1】:

嗯...嗯,我将您的代码与我已经工作的代码进行了比较...我没有看到很多差异...但是,这些细微差异之一可能很重要。

final Notification notification = new Notification(R.drawable.icon, "Downloading", System.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;

notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.download_progress);

notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.ic_status);
notification.contentView.setTextViewText(R.id.status_text, "Downloading in progress");
notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false);
Intent notificationIntent = new Intent(MainPage.mainActivity, MainPage.class);
PendingIntent contentIntent = PendingIntent.getActivity(MainPage.mainActivity, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;

//getApplicationContext();
final NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(
                Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_MESSAGE, notification);

首先,我查看了您的旧代码并注意到 NOTIF_ID = 1 我不太确定这是一个好主意,因为如果其他人的 ID 为 1 怎么办。当然我可能弄错了,但我只是输入了一个像 792489743 这样的数字,我希望没有其他人会拥有相同的数字。我想只是一种预防措施。

其次,我没看资源是否正确?堆栈跟踪说明了什么?我想如果那里有问题,它就会退出它。

第三,我把我自己的任务作为Service 有点如下

public class DownloadService extends IntentService {
    //initializing code and stuff
    private class DownloadTask extends AsyncTask<String, Void, Boolean> {

我是在doInBackground 中做到的

最后,我从未使用过apply,我个人不知道它会有什么伤害,但我也没有看到使用它的示例。

希望对大家有所帮助!

【讨论】:

  • 首先感谢您的帮助。我将 NOTIF_ID 更改为一个随机的大数字,并删除了应用的调用以防万一......不幸的是,仍然不起作用。
  • 还尝试在私有类之外声明 Notification 和 RemoteViews 对象。还是不行!
  • 嗯...我认为这里发生了一些我不完全理解的事情。我想您可以尝试只打开 Eclipse 调试器,然后一步一步地通过它,看看情况如何。对不起,我的建议对你不起作用!我还检查了您的布局代码,这对我来说看起来很正常。不过,我不是这些方面的专家……
  • 我怀疑它与 PendingIntent 有关。如果有人可以提供明确的解释,它可能会有所帮助。
【解决方案2】:

毕竟是模拟器问题.....

当我“拖下”通知时它滞后了!我在我的 PC 上杀死了一些 CPU 广泛的进程,从而获得了更快的模拟器。

经验教训。将繁重的多任务处理留给专业人士或另一台 PC。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 2020-09-15
    • 1970-01-01
    相关资源
    最近更新 更多