以下是我的通知代码: -
public void showNotification(String Name, String Rate, int Image_Source, int PandP, int Repeat) {
RemoteViews remoteview = new RemoteViews(getPackageName(), R.layout.notification_layout);
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, SongsListActivity.class), 0);
notification = new NotificationCompat.Builder(this)
.setContent(remoteview)
.setPriority(2)
.setTicker(NameD.getText())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(NameD.getText())
.setContentText(RateD.getText())
.setContentIntent(pi)
.setAutoCancel(false)
.setCustomBigContentView(remoteview)
.build();
notification.bigContentView = remoteview;
remoteview.setImageViewResource(R.id.Repeat_N, Repeat);
remoteview.setImageViewResource(R.id.P_and_P_N, PandP);
remoteview.setTextViewText(R.id.Name_N, Name);
remoteview.setTextViewText(R.id.Rate_N, Rate);
remoteview.setImageViewResource(R.id.Image_N, Image_Source);
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(111111, notification);
}
这是我的xml通知: -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#2196F3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#2196F3"
android:orientation="horizontal">
<ImageView
android:id="@+id/Image_N"
android:layout_width="100dp"
android:layout_height="100dp"
tools:ignore="ContentDescription" />
<LinearLayout
android:id="@+id/text_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/Name_N"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@android:color/white"
android:textStyle="bold"
android:maxLength="25"/>
<TextView
android:id="@+id/Rate_N"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#F44336" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ImageButton
android:id="@+id/Repeat_N"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_repeat_white_48dp"
android:background="#2196F3"/>
<ImageButton
android:id="@+id/Previous_N"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_skip_previous_white_48dp"
android:background="#2196F3"/>
<ImageButton
android:id="@+id/P_and_P_N"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_play_arrow_white_48dp"
android:background="#2196F3"/>
<ImageButton
android:id="@+id/Next_N"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_skip_next_white_48dp"
android:background="#2196F3"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
我的通知布局
现在如何向通知中的4个按钮添加操作,以便: -
a)暂停图像按钮暂停MediaPlayer媒体播放器,然后将其更改为播放。
b)下一个和上一个Imagebuttons调用方法mediaplayer(int i)
c)Repet Imagebutton调用方法changeRepeat()
答案 0 :(得分:0)
用于将侦听器添加到远程视图中
remoteViews.setOnClickPendingIntent(R.id.someView, pendingIntent);
答案 1 :(得分:0)
您可以将remoteViews.setOnClickPendingIntent(R.id.someView, pendingIntent);
与BroadCastReceiver结合使用并构建如下通知:
private void showNotification(){private void showNotification(){
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
PendingIntent pendIntent = PendingIntent.getActivity
(this, 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// notification's layout
RemoteViews mRemoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification_small);
mRemoteViews.setImageViewResource(R.id.notif_icon, R.mipmap.ic_launcher);
mRemoteViews.setTextViewText(R.id.notif_title, "playMusic");
mRemoteViews.setTextViewText(R.id.notif_content, "pauseMusic");
Intent intentPlay = new Intent(this, MyReceiver.class);
intentPlay.setAction(Constants.PLAY_ACTION);
PendingIntent pIntentPlay = PendingIntent.getBroadcast
(this, Constants.PENDING_PLAY_CODE, intentPlay, PendingIntent.FLAG_UPDATE_CURRENT);
Intent intentPause = new Intent(this, MyReceiver.class);
intentPause.setAction(Constants.PAUSE_ACTION);
PendingIntent pIntentPause = PendingIntent.getBroadcast
(this, Constants.PENDING_PAUSE_CODE, intentPause, PendingIntent.FLAG_UPDATE_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.notif_title, pIntentPlay);
mRemoteViews.setOnClickPendingIntent(R.id.notif_content, pIntentPause);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.icon_play)
.setAutoCancel(false)
.setOngoing(true)
.setContentIntent(pendIntent)
.setContent(mRemoteViews)
.setTicker("musicStarted");
startForeground(NOTIFY_ID, mBuilder.build());
}
在BroadCast接收器触发方法中你需要管理媒体播放器,在我的情况下我有一个绑定到MainActivity的服务,所以我通过接口连接了Main和Broadcast Receiver,当MainActivity的方法触发它时管理服务,服务管理它的MediaPlayer