Android:使用扩展自定义布局中的通知按钮

时间:2014-04-27 15:24:49

标签: android button android-intent notifications

所以我已经查看过相当多的Stackoverflow问题,但是我仍然没有运气这么做。我正在玩弄并试图学习如何创建和使用Notifications(尝试在尝试使用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()" );
}
}

如果有人有任何建议或教程,他们可以指示我为了让这些按钮发送意图或让听众与他们相关联,我真的很感激。

2 个答案:

答案 0 :(得分:1)

您可以通过以下方式设置单击通知中的按钮:

 Intent playIntent = new Intent( getApplicationContext(), CustomNotificationService.class );
 playIntent.putExtra("Button","Play");  
 PendingIntent playPendingIntent = PendingIntent.getService( getApplicationContext(), 1, playIntent, PendingIntent.FLAG_UPDATE_CURRENT);
 expandedView.setOnClickPendingIntent( R.id.ib_play_pause, playPendingIntent );

 Intent rewindIntent = new Intent( getApplicationContext(), CustomNotificationService.class );
 rewindIntent.putExtra("Button","Rewind");  
 PendingIntent rewindPendingIntent = PendingIntent.getService( getApplicationContext(), 2, rewindIntent, PendingIntent.FLAG_UPDATE_CURRENT);
 expandedView.setOnClickPendingIntent( R.id.ib_rewind, rewindPendingIntent );

 Intent fastForwardIntent = new Intent( getApplicationContext(), CustomNotificationService.class );
 fastForwardIntent.putExtra("Button","FastForward");    
 PendingIntent fastForwardPendingIntent = PendingIntent.getService( getApplicationContext(), 3, fastForwardIntent, PendingIntent.FLAG_UPDATE_CURRENT);
 expandedView.setOnClickPendingIntent( R.id.ib_fast_forward, fastForwardPendingIntent );

现在您在handleInent()内的服务中执行以下操作:

if( intent != null && intent.getAction() != null ) {
    String action= (String)getIntent().getExtras().get("Button");
    if(action.equalsIgnoreCase("Play")){
        Log.i("Info","Play Button clicked");           
    }else if(action.equalsIgnoreCase("Rewind")){
            Log.i("Info","Rewind Button clicked");
    }else if(action.equalsIgnoreCase("Forward")){
            Log.i("Info","Fast Forward Button clicked");
    }
}

答案 1 :(得分:0)

想出来。添加了intent.setAction(" playpause");在创建pendingintent之前,然后为我使用的服务

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    handleIntent( intent );
    return super.onStartCommand(intent, flags, startId);
}

为了获取意图并将其传递给我的函数,我可以在其中确定与之关联的操作类型并执行我的操作。