Android notifications: compatibility with APIs

随声附和 提交于 2019-12-07 06:58:10

问题


I'm trying to show notifications in my app along with a progress bar in it.

My app specs are that I'm targeting API 15 with minimum SDK set to 8. I'm using the following code to show notifications:

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

mBuilder.setContentTitle("My Application")
        .setContentText("Downloading...")
        .setProgress(0, 0, true)
        .setSmallIcon(R.drawable.ic_launcher);

PendingIntent in = PendingIntent.getActivity(getApplicationContext(), 0, getIntent(), 0);
mBuilder.setContentIntent(in);

mNotificationManager.notify(0, mBuilder.build());

With the above code the notifications appear in the notification drawer but no progress bar (on Android 2.2, and 2.3.3). But the progress bar appears fine on Android 4.1. So its obvious that its a compatibility issue.

How do I code that my notification with an indefinite progress bar appear on the APIs that I'm targeting. I tried using Notification.Builder but this class is not available on my current specs.

Thanks in advance!

-Faraz Azhar


回答1:


Have you tried using http://developer.android.com/reference/android/widget/RemoteViews.html ?




回答2:


I encourage you to use Notification Compat 2. It has been created to handle this kind of problem.
If you want to use a custom RemoteViews for your notifications, have a look at my fork. I have made a pull request but it has not been integrated yet.



来源:https://stackoverflow.com/questions/12914126/android-notifications-compatibility-with-apis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!