【问题标题】:Android - Show Sound NotificationAndroid - 显示声音通知
【发布时间】:2020-02-15 16:44:41
【问题描述】:

我是安卓编程新手。我所取得的成就是我已经设置了一个 Android Studio 应用程序,并在您的帮助下使其正常工作并播放音频文件。但我不知道如何让它以正确的方式着色。我希望它像 YouTube、Spotify 和 Amazon Musik 的声音通知。一切看起来完全一样,所以我认为它是内置的,但我不知道是什么以及如何设置它。提前致谢。

【问题讨论】:

标签: android android-studio android-intent android-activity


【解决方案1】:

使用这样的自定义操作名称创建一个 Intent

  Intent switchIntent = new Intent("com.example.app.ACTION_PLAY");

然后,注册PendingIntentBroadcast接收器

  PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 100, switchIntent, 0);

然后,为播放控件设置onClick,如果需要,对其他控件执行类似的自定义操作。

  notificationView.setOnClickPendingIntent(R.id.btn_play_pause_in_notification, pendingSwitchIntent);

接下来,像这样在AudioPlayerBroadcastReceiver注册自定义动作

   <receiver android:name="com.example.app.AudioPlayerBroadcastReceiver" >
        <intent-filter>
            <action android:name="com.example.app.ACTION_PLAY" />
        </intent-filter>
    </receiver>

最后,当在NotificationRemoteViews布局上点击播放时,你会收到play actionBroadcastReceiver

public class AudioPlayerBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();

    if(action.equalsIgnoreCase("com.example.app.ACTION_PLAY")){
        // do your stuff to play action;
    }
   }
}

您还可以像这样从注册的Broadcast receiver 的代码中设置Custom ActionIntent filter

    // instance of custom broadcast receiver
    CustomReceiver broadcastReceiver = new CustomReceiver();

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
    // set the custom action
    intentFilter.addAction("com.example.app.ACTION_PLAY");
    // register the receiver
    registerReceiver(broadcastReceiver, intentFilter); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    相关资源
    最近更新 更多