【发布时间】:2014-04-27 15:24:49
【问题描述】:
所以我已经查看了相当多的 Stackoverflow 问题,但我仍然没有任何运气让它发挥作用。我正在玩弄并尝试学习如何创建和使用通知(尝试在尝试 Wear API 之前关闭当前系统)并且我遇到了一些障碍。对于 Jellybean+,我有一个包含三个按钮的扩展通知。我希望能够允许用户单击通知中的这些按钮以执行操作(此时它可能只是一个日志语句)。到目前为止,我无法让按钮触发任何东西。现在我的通知是使用以下代码创建的:
private void showExpandedNotification() {
RemoteViews expandedView = new RemoteViews(getPackageName(),
R.layout.view_notification);
expandedView.setImageViewResource(R.id.large_icon, R.drawable.ic_launcher);
expandedView.setImageViewResource(R.id.ib_rewind, R.drawable.ic_rewind);
expandedView.setImageViewResource(R.id.ib_play_pause, R.drawable.ic_play);
expandedView.setImageViewResource(R.id.ib_fast_forward, R.drawable.ic_fast_forward);
Intent intent = new Intent( getApplicationContext(), CustomNotificationService.class );
PendingIntent pendingIntent = PendingIntent.getService( getApplicationContext(), 1, intent, 0);
expandedView.setOnClickPendingIntent( R.id.ib_play_pause, pendingIntent );
Notification notification = new NotificationCompat.Builder(getApplicationContext())
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.build();
notification.bigContentView = expandedView;
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification);
}
我只在一个按钮上设置了意图。该服务非常简单,因为我现在只是想让它工作:
public class CustomNotificationService extends Service {
@Override
public IBinder onBind(Intent intent) {
Log.e( "CustomNotificationService", "onBind" );
handleIntent( intent );
return null;
}
private void handleIntent( Intent intent ) {
if( intent != null && intent.getAction() != null ) {
Log.e( "CustomNotificationService", intent.getAction().toString() );
}
}
@Override
public void onCreate() {
super.onCreate();
Log.e( "CustomNotificationService", "onCreate()" );
}
}
如果有人有任何建议或教程可以指点我,以便让这些按钮发送意图或有与之关联的侦听器,我将不胜感激。
【问题讨论】:
标签: android button android-intent notifications