执行操作按钮上单击自定义通知:安卓 [英] Perform Action On Button Click in Custom Notification : Android

查看:145
本文介绍了执行操作按钮上单击自定义通知:安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图执行像暂停音乐的一些动作,就在Android的自定义通知按钮,点击播放音乐。 目前,我做它通过这种方式,

I am trying to perform some action like pause music , play music on button click of a custom notification in 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.

例如..比方说,我养notifcation,并在其上​​的用户preSS的话,而不是带我去一些活动,它在我的当前活动/服务调用一些正规方法

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

推荐答案

首先分配意图的按钮:

    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();
         }
    }

最后,你应该确定活动的 AndroidManifest.xml中。你也可以检查此链接

Finally, you should define activity in AndroidManifest.xml. Also you can check this link.

我希望这对您有所帮助。

I hope this is helpful to you.

这篇关于执行操作按钮上单击自定义通知:安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆