Android-显示声音通知

问题描述 投票:0回答:1

我是android编程的新手。我所取得的成就是,我已经安装了Android Studio应用程序,并在您的帮助下使其能够正常工作并播放音频文件。但是我不知道如何使它正确地着色。我希望它就像YouTube,Spotify和Amazon Musik发出的声音通知一样。外观看起来完全一样,所以我认为它是内置的,但是我无法确定要设置什么以及如何设置。预先感谢。

android android-studio android-intent android-activity
1个回答
1
投票

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

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

然后,注册PendingIntent Broadcast接收器

  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>

最后,在Notification RemoteViews布局上单击播放时,play action会收到BroadcastReceiver

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

您也可以像这样从已注册的Custom Action的代码中设置Intent filterBroadcast receiver

    // 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); 
© www.soinside.com 2019 - 2024. All rights reserved.