【问题标题】:How to use notification lisenter?如何使用通知监听器?
【发布时间】:2015-09-23 16:44:01
【问题描述】:

我正在创建一个媒体播放器应用程序。我想在通知栏上显示一个通知,具有播放、暂停、前进、后退和关闭功能。通知已成功显示在通知栏上。但我无法对此提供任何点击事件..

通知面板

public class NotificationPanel {

    Context mContext;
    NotificationManager mManager;
    NotificationCompat.Builder builder;
    RemoteViews remoteViews;
    int NOTIFICATION_ID = 1234;
    ImageButton play_pause;

    public NotificationPanel(Context mContext) {
        this.mContext = mContext;
        builder = new NotificationCompat.Builder(mContext).
                setContentTitle("SONORE").setSmallIcon(R.mipmap.dark_logo2).setOngoing(true);

        remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.notification_bar_layout);

        setListeners(remoteViews);

        builder.setContent(remoteViews);

        mManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

        mManager.notify(NOTIFICATION_ID, builder.build());

    }

    private void setListeners(RemoteViews remoteViews) {

        //listener to pause song
        Intent pause = new Intent(mContext, NotificationReturnSlot.class);
        pause.putExtra("DO", "pause_play");
        PendingIntent playPausebtn = PendingIntent.getActivity(mContext, 1, pause, 0);
        remoteViews.setOnClickPendingIntent(R.id.fab_play, playPausebtn);

        //listener to remove notification bar
        Intent remove_notification = new Intent(mContext, NotificationReturnSlot.class);
        remove_notification.putExtra("DO", "remove_notification");
        PendingIntent remove_notification_btn = PendingIntent.getActivity(mContext, 2, remove_notification, 0);
        remoteViews.setOnClickPendingIntent(R.id.remove_notification, remove_notification_btn);

        //listener to forward song
        Intent forward = new Intent(mContext, NotificationReturnSlot.class);
        forward.putExtra("DO", "forward");
        PendingIntent forwardbtn = PendingIntent.getActivity(mContext, 3, forward, 0);
        remoteViews.setOnClickPendingIntent(R.id.forward, forwardbtn);

        //listener to backward song
        Intent backward = new Intent(mContext, NotificationReturnSlot.class);
        forward.putExtra("DO", "backward");
        PendingIntent backbtn = PendingIntent.getActivity(mContext, 4, backward, 0);
        remoteViews.setOnClickPendingIntent(R.id.forward, backbtn);

    }

    public void notificationCancle() {
        mManager.cancel(NOTIFICATION_ID);
    }

    public void changeToPlayIcon() {


    }
}

从这个活动中,我得到了 click lisenter 事件,并启动了一个广播管理器来从我的主要活动中调用我的方法。

NotificationReturnSlot.activity

public class NotificationReturnSlot extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        int position;

        String action = (String) getIntent().getExtras().get("DO");
        if (action.equals("pause_play")) {
            position = 1;
            callMethodOfDrawerActivityFromNotificationBar(position);

        } else if (action.equals("remove_notification")) {
            position = 2;
            callMethodOfDrawerActivityFromNotificationBar(position);

        } else if (action.equals("forward")) {
            position = 3;
            callMethodOfDrawerActivityFromNotificationBar(position);

        } else if (action.equals("backward")) {
            position = 4;
            callMethodOfDrawerActivityFromNotificationBar(position);

        }


        finish();

    }

    public void callMethodOfDrawerActivityFromNotificationBar(int pos) {
        Intent intent = new Intent("notification-event");
        // add data
        intent.putExtra("notification", pos);

        LocalBroadcastManager.getInstance(this).

                sendBroadcast(intent);
    }
}

从 MainActivity 我通过以下方式获取广播事件

     @Override
        protected void onResume() {
            super.onResume();

            //Register Notification receiver
            LocalBroadcastManager.getInstance(this).registerReceiver(mNotificationReceiver, new IntentFilter("notification-event"));
        }


     private BroadcastReceiver mNotificationReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                int pos = intent.getIntExtra("notification", 0);

                switch (pos) {

                    case 0:
                        //do nothing
 LocalBroadcastManager.getInstance(MainActivity.this).unregisterReceiver(mNotificationReceiver);
                        break;
                    case 1:
                        if (isSongPlaying()) {
                            pauseSong();
 LocalBroadcastManager.getInstance(MainActivity.this).unregisterReceiver(mNotificationReceiver);
                        } else {
                            playSong();
 LocalBroadcastManager.getInstance(MainActivity.this).unregisterReceiver(mNotificationReceiver);
                        }
                        break;
                    case 2:
                        mPanel.notificationCancle();
 LocalBroadcastManager.getInstance(MainActivity.this).unregisterReceiver(mNotificationReceiver);
                        break;
                    case 3:
                        mService.playNextSong();
 LocalBroadcastManager.getInstance(MainActivity.this).unregisterReceiver(mNotificationReceiver);
                        break;
                    case 4:
                        mService.playPreviousSong();
 LocalBroadcastManager.getInstance(MainActivity.this).unregisterReceiver(mNotificationReceiver);
                }
            }
        };

【问题讨论】:

  • 实际上,当我点击通知布局时,它没有调用给定的方法
  • @ajameswolf 这不属于代码审查。损坏的代码在那儿是题外话,包含它的问题将被关闭。 请阅读help center there
  • @ajameswolf 如果您不太了解那里的主题,请停止推荐代码审查。
  • @janos 好的,在这种情况下你会推荐什么?它太具体而没有任何用处?类似于有人在这里发布他们的编码作业。

标签: android


【解决方案1】:

我得到了答案。问题是,我正在从 pause() 中调用删除广播。但应该在从通知布局执行的每个操作之后调用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2021-04-21
    • 2015-09-22
    • 2017-11-15
    • 1970-01-01
    • 2022-10-14
    相关资源
    最近更新 更多