创建图片背景的通知,由于google未提供固定的模板,只能自己创建。先创建一个布局文件,只要一个ImageView即可:
1 2 3 4 5 6 <ImageView android:id ="@+id/image" android:layout_width ="match_parent" android:layout_height ="wrap_content" android:scaleType ="centerCrop" />
创建通知栏的api因为google的原因更改的太多,所以我们使用v7包下的api来创建:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 final NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context); final RemoteViews mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.layout_notification_custom); mBuilder.setContent(mRemoteViews) .setWhen(System.currentTimeMillis()) .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setSmallIcon(R.drawable.ic_qq_login_normal).setAutoCancel(true ); Intent buttonIntent = new Intent(); buttonIntent.setAction(Intent.ACTION_VIEW); buttonIntent.setData(Uri.parse(bean.getUrl())); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 , buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(pendingIntent); Notification notify = mBuilder.build(); Picasso.with(context).load(bean.getImg()).into(mRemoteViews, R.id.image, 0 , notify); mNotificationManager.notify(0 , notify);
展示图片使用了Picasso库。所以在build.gradle文件中要添加:
1 2 3 4 dependencies { compile 'com.squareup.picasso:picasso:2.5.2' compile 'com.android.support:appcompat-v7:25.0.0' }
这样上面的代码就可以展示一个远程图片的通知了!