在 Android 中自定义通知区域
Posted
技术标签:
【中文标题】在 Android 中自定义通知区域【英文标题】:Customize Notification Area in Android 【发布时间】:2014-08-09 13:56:07 【问题描述】:我正在使用RemoteViews
创建自定义通知。有什么方法可以自定义通知区域特别高度......???我不想使用BigView
因为我需要将它用于小于 11 的 API。
截图是:
这是我的代码:
public class Main extends Activity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.notificationmain);
Button bnotify = (Button) findViewById(R.id.notification);
Button bcustomnotify = (Button) findViewById(R.id.customnotification);
// Click For Default Notification
bnotify.setOnClickListener(new OnClickListener()
public void onClick(View arg0)
Notification();
);
// Click For Custom Notification
bcustomnotify.setOnClickListener(new OnClickListener()
public void onClick(View arg0)
CustomNotification();
);
// Default Notification
public void Notification()
String strtitle = getString(R.string.notificationtitle);
String strtext = getString(R.string.notificationtext);
Intent intent = new Intent(this, NotificationView.class);
intent.putExtra("title", strtitle);
intent.putExtra("text", strtext);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logosmall)
.setTicker(getString(R.string.notificationticker))
.setContentTitle(getString(R.string.notificationtitle))
.setContentText(getString(R.string.notificationtext))
.addAction(R.drawable.ic_launcher, "Action Button", pIntent)
.setContentIntent(pIntent)
.setAutoCancel(true);
// Create Notification Manager
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationmanager.notify(0, builder.build());
// Custom Notification
public void CustomNotification()
// Using RemoteViews to bind custom layouts into Notification
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.customnotification);
String strtitle = getString(R.string.customnotificationtitle);
String strtext = getString(R.string.customnotificationtext);
Intent intent = new Intent(this, NotificationView.class);
intent.putExtra("title", strtitle);
intent.putExtra("text", strtext);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logosmall)
.setTicker(getString(R.string.customnotificationticker))
.setAutoCancel(true)
.setContentIntent(pIntent)
.setContent(remoteViews);
remoteViews.setImageViewResource(R.id.imagenotileft,R.drawable.ic_launcher);
remoteViews.setImageViewResource(R.id.imagenotiright,R.drawable.androidhappy);
remoteViews.setTextViewText(R.id.title,getString(R.string.customnotificationtitle));
remoteViews.setTextViewText(R.id.text,getString(R.string.customnotificationtext));
Calendar cal = Calendar.getInstance();
cal.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
remoteViews.setTextViewText(R.id.time,sdf.format(cal.getTime()) );
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationmanager.notify(1, builder.build());
@Override
public boolean onCreateOptionsMenu(Menu menu)
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
【问题讨论】:
您可以通过修改布局customnotification
来控制高度,并使用更小尺寸的图片。
我想增加高度...
自定义通知布局的可用高度取决于通知视图。普通视图布局仅限于64 dp
,展开视图布局仅限于256 dp
。
@JoelFernandes ,我在customnotification
布局中将图像帧大小增加到 80dp,但高度仍然相同......
【参考方案1】:
看起来 Android 支持库 v4 有一个支持这种东西的通知生成器:http://developer.android.com/reference/android/support/v4/app/NotificationCompat.InboxStyle.html
NotificationCompat.InboxStyle 似乎是 4.1 之前设备的 BigView 等效项。
这允许您创建一个包含最多五行文本的较大通知,这可能会满足您的需求。
Notification noti = new Notification.Builder()
.setContentTitle("5 New mails from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.setStyle(new Notification.InboxStyle()
.addLine(str1)
.addLine(str2)
.setSummaryText("+3 more"))
.setContentTitle("")
.build();
这里有一个简短的教程:http://doandroidcoding.blogspot.com/2014/01/bigview-style-notification.html?m=1
【讨论】:
我将这些行合并到我的代码中作为默认通知...但它不起作用... 经过多次尝试,我终于在您的帮助下弄清楚了...thnx 太棒了,很高兴听到它!【参考方案2】:通知显示区域在android中是系统控制的 请参考this链接
【讨论】:
以上是关于在 Android 中自定义通知区域的主要内容,如果未能解决你的问题,请参考以下文章
带有 RemoteViews 的 Android 自定义通知布局