简体   繁体   English

按钮执行操作单击自定义通知:Android

[英]Perform Action On Button Click in Custom Notification : Android

I am trying to perform some action like pause music , play music on button click of a custom notification in android. 我正在尝试执行一些动作,如暂停音乐,在按钮点击Android中的自定义通知播放音乐。 Currently I am doing it in this way , 目前我这样做,

    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon, "Custom Notification", when);

    NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.layout);
    contentView.setTextViewText(R.id.textView1, "Custom notification");
    contentView.setOnClickPendingIntent(R.id.button1, pIntent);
    notification.contentView = contentView;

    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.contentIntent = contentIntent;

    notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
    notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
    notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
    notification.defaults |= Notification.DEFAULT_SOUND; // Sound

    mNotificationManager.notify(1, notification);

But this one takes me to another activity. 但是这个带我去另一个活动。 Is there anyway to implement notification action on the same activity. 无论如何都要对同一活动实施通知操作。

for example.. let's say i raise a notifcation, and when the user press on it, then instead of take me to some activity, it invoke some regular method in my current activity/service 例如..让我说我提出了一个通知,当用户按下它时,然后在我当前的活动/服务中调用一些常规方法而不是带我去做某些活动

First of All assign a intent to your buttons: 首先为您的按钮分配一个意图:

    RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.player_notify_layout);
    Intent buttonsIntent = new Intent(context, NotifyActivityHandler.class);
    buttonsIntent.putExtra("do_action", "play");
    contentView.setOnClickPendingIntent(R.id.imgPlayPause, PendingIntent.getActivity(context, 0, buttonsIntent, 0));

Then create an activity to handle every action that occurred by notification: 然后创建一个活动来处理通知发生的每个操作:

    public class NotifyActivityHandler extends Activity {
           public static final String PERFORM_NOTIFICATION_BUTTON = "perform_notification_button";

           @Override
           protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);

               String action = (String) getIntent().getExtras().get("do_action");
               if (action != null) {
                   if (action.equals("play")) {
                       // for example play a music
                   } else if (action.equals("close")) {
                       // close current notification
                   }
               }

               finish();
         }
    }

Finally, you should define activity in AndroidManifest.xml . 最后,您应该在AndroidManifest.xml中定义活动。 Also you can check this link . 您也可以查看此链接

I hope this is helpful to you. 我希望这对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM