0

我正在开发一个Android基本上是音乐应用程序的应用程序。根据音乐播放器播放歌曲时的要求之一,该notification栏也应该出现通知,在设备锁定时也可以看到。现在我已经为它创建了一个自定义layoutnotification它有一些按钮可以与音乐播放器进行交互。我可以在歌曲播放时看到通知,但我无法处理他们的点击。我也创建了一个BroadcastReceiver。我已经在 SO 上搜索了这个,谷歌搜索并应用了那里给出的许多解决方案,但我仍然无法处理它。我什至通过这个视频来做到这一点,但到目前为止还是徒劳: 如何在 Android 中创建自定义通知

我在这里粘贴我的代码,请让我知道我在哪里做错了。

Notification UI如下: 通知

这是设置的方法notification

public void setNotification(String songName,final String image,String songNamear){
    String ns = Context.NOTIFICATION_SERVICE;
    notificationManager = (NotificationManager) getSystemService(ns);

    Thread th=new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                URL url = new URL(image);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 8;
                image12 = BitmapFactory.decodeStream(input, null, options);
                BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(input, false);
                image12 = decoder.decodeRegion(new Rect(10, 10, 50, 50), null);
            } catch(IOException e) {
                System.out.println(e);
            }
        }
    });
    th.start();
    Notification.Builder mNotifyBuilder = new Notification.Builder(this);
    notification = mNotifyBuilder.setContentTitle(songName)
            .setContentText(songNamear)
            .setSmallIcon(R.drawable.app_icon)
            .setLargeIcon(image12)
            .build();
    mNotifyBuilder.setPriority(Notification.PRIORITY_HIGH);

    notificationView = new RemoteViews(getPackageName(), R.layout.notification_mediacontroller);
    notificationView.setImageViewResource(R.id.btn_play_pause_in_notification,R.drawable.home_stop_icon);
    Log.d("imageuri",""+Uri.parse(image));
    notificationView.setImageViewBitmap(R.id.img_user,image12);
    notificationView.setTextViewText(R.id.txt_songname,songName);
    notificationView.setTextViewText(R.id.txt_songnamear,songNamear);
    SharedPreferences.Editor editor = getSharedPreferences("juke1", MODE_PRIVATE).edit();
    editor.putString("pos",""+intPOs);
    editor.commit();
    Intent notificationIntent = new Intent(this, MusicPlayerActivity.class);
    notificationIntent.putExtra("Position",intPOs);
    notificationIntent.putExtra("page","not");
    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.contentView = notificationView;
    notification.contentIntent = pendingNotificationIntent;
    notification.flags |= Notification.FLAG_NO_CLEAR;
    
    //this is the intent that is supposed to be called when the button is clicked

    Intent switchIntent = new Intent(Constants.NOTIFICATION_CLICK_ACTION);
    switchIntent.setAction(Constants.ACTION_PLAY);
    PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(MusicPlayerActivity.this, 100, switchIntent, 0);

    Intent switchIntent1 = new Intent(Constants.NOTIFICATION_CLICK_ACTION);
    switchIntent1.setAction(Constants.ACTION_NEXT);
    PendingIntent pendingSwitchIntent1 = PendingIntent.getBroadcast(MusicPlayerActivity.this, 101, switchIntent1, 0);

    Intent switchIntent2 = new Intent(Constants.NOTIFICATION_CLICK_ACTION);
    switchIntent2.setAction(Constants.ACTION_PREV);
    PendingIntent pendingSwitchIntent2 = PendingIntent.getBroadcast(MusicPlayerActivity.this, 102, switchIntent2, 0);

    Intent switchIntent3 = new Intent(Constants.NOTIFICATION_CLICK_ACTION);
    switchIntent3.setAction(Constants.ACTION_CLOSE);
    PendingIntent pendingSwitchIntent3 = PendingIntent.getBroadcast(MusicPlayerActivity.this, 103, switchIntent3, 0);

    notificationView.setOnClickPendingIntent(R.id.btn_play_pause_in_notification, pendingSwitchIntent);
    notificationView.setOnClickPendingIntent(R.id.btn_next, pendingSwitchIntent1);
    notificationView.setOnClickPendingIntent(R.id.btn_prev, pendingSwitchIntent2);
    notificationView.setOnClickPendingIntent(R.id.btn_close, pendingSwitchIntent3);
    notificationManager.notify(1, notification);

以下是BroadcastReceiver. 我尝试了它单独的类以及内部classMusicPlayerActivity. 有了inner class,我可以很容易地调用functions下一首歌曲、上一首歌曲、播放/暂停等buttons clicks

public class NotificationListener extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        MakeToast.show("Clcik");
        String action = intent.getAction();
        if(action.equalsIgnoreCase(Constants.ACTION_NEXT)){
            MakeToast.show("Next");
        }
        if(action.equalsIgnoreCase(Constants.ACTION_PREV)){
            MakeToast.show("Previous");
        }
        if(action.equalsIgnoreCase(Constants.ACTION_CLOSE)){
            MakeToast.show("Close");
        }
        if(action.equalsIgnoreCase(Constants.ACTION_PLAY)){
            MakeToast.show("Play");
        }

    }
}

Android Manifest中,接收者声明如下。目前它是分开的class

<receiver android:name=".listeners.NotificationListener">
        <intent-filter>
            <action android:name="notification_button_clicked" />
        </intent-filter>
    </receiver>

任何帮助都将不胜感激,因为我的日程安排很晚。

谢谢

4

1 回答 1

0

您可以使用服务类通过通知远程视图处理按钮单击。

public class MusicPlayerService extends Service {
public static final String ACTION_PAUSE_PLAY = PACKAGE_NAME + ".playpause";
public static final String ACTION_NEXT = PACKAGE_NAME + ".next";
public static final String ACTION_PREVIOUS = PACKAGE_NAME + ".prev";


@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null && intent.getAction() != null) {

        switch (intent.getAction()) {
            case ACTION_PREVIOUS:
                // go to previous song
                break;
            case ACTION_NEXT:
                // go to next song
                break;
            case ACTION_PAUSE_PLAY:
                // pause or play song
                break;
        }
    }
    return START_NOT_STICKY;
}

此外,您必须将通知按钮与未决意图链接

 PendingIntent pendingIntent;

    final ComponentName serviceName = new ComponentName(service, MusicPlayerService.class);

    // Previous track
    pendingIntent = buildPendingIntent(service, ACTION_PREVIOUS, serviceName);
    notificationLayout.setOnClickPendingIntent(R.id.prev, pendingIntent);

    // Play and pause
    pendingIntent = buildPendingIntent(service, ACTION_PAUSE_PLAY, serviceName);
    notificationLayout.setOnClickPendingIntent(R.id.playpause, pendingIntent);

    // Next track
    pendingIntent = buildPendingIntent(service, ACTION_NEXT, serviceName);
    notificationLayout.setOnClickPendingIntent(R.id.next, pendingIntent);
于 2018-07-20T06:03:32.957 回答