【问题标题】:Android: Working with Notification Buttons in Expanded Custom LayoutAndroid:在扩展的自定义布局中使用通知按钮
【发布时间】: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


    【解决方案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");
        }
    }
    

    【讨论】:

    • 哈,太棒了。我得出了一个非常相似的结论,并在您发布后几秒钟发布了它。非常感谢。
    • @PaulRuiz 很高兴听到您的问题已经解决了.. :)
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多