我正在尝试在Android中创建一个自定义状态栏通知,除了文本之外还有一个按钮.该按钮可以执行与单击通知本身不同的操作.这有可能吗?我也可以在那里放一个按钮的图像.我知道如何放置图像,但不知道如何处理嵌入在RemoteViews中的图像的OnClick.非常感谢您的帮助.
非常感谢提前.
我用它NotificationCompat.Builder来通过Android版本显示我的通知,并使用自定义布局进行通知.
自定义布局在Android 3及更高版本(API级别11)上运行良好,但不会出现在API级别10或更低级别.我在模拟器中测试了2.3和2.2.
继承我的代码:
Builder builder = new NotificationCompat.Builder(getApplicationContext());
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_layout);
contentView.setImageViewResource(R.id.notImage, R.drawable.stat_icon);
contentView.setTextViewText(R.id.notTitle, getResources().getString(R.string.streamPlaying));
contentView.setTextViewText(R.id.notText, StartActivity.streamName + " " + getResources().getString(R.string.playing));
builder
.setContentTitle(getResources().getString(R.string.streamPlaying))
.setContentText(StartActivity.streamName + " " + getResources().getString(R.string.playing))
.setSmallIcon(R.drawable.stat_icon)
.setContentIntent(pendingIntent)
.setOngoing(true)
.setWhen(0)
.setTicker(StartActivity.streamName + " " + getResources().getString(R.string.playing))
.setContent(contentView);
not = builder.build();
Run Code Online (Sandbox Code Playgroud)
真的很基本.布局文件是正确的,它与android.com上的通知教程相同,以确保我没有在那里犯错.;)
记住:3.0及以上工作正常,但不是2.3及更低.