类似于酷狗播放器的消息通知栏切换歌曲的效果,实现原理: 先自定义一个Notification,通知栏中Button的点击时间是通过广播发送出去,广播接收后,
做相应的处理,如上一首,下一首,暂停等。
第一步:先写好通知栏的布局文件
<LinearLayout
android:id="@+id/music_notifi_lay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center_vertical"
>
<ImageView
android:id="@+id/songer_pic"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="150dp"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="left|center_vertical"
android:layout_marginLeft="5dp"
>
<TextView
android:id="@+id/title_title"
android:text="@string/snow_music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
/>
<TextView
android:id="@+id/title_music_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:textSize="15sp"
android:textColor="#000000"
/>
</LinearLayout>
<ImageView
android:id="@+id/last_music"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:src="@drawable/music_previous" />
<ImageView
android:id="@+id/paly_pause_music"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:src="@drawable/music_play" />
<ImageView
android:id="@+id/next_music"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:src="@drawable/music_next" />
</LinearLayout>
</LinearLayout>
第二步:写广播接收器
public class ServiceReceiver extends BroadcastReceiver {
public static final String NOTIFICATION_ITEM_BUTTON_LAST
= "com.example.notification.ServiceReceiver.last";//----通知栏上一首按钮
public static final String NOTIFICATION_ITEM_BUTTON_PLAY
= "com.example.notification.ServiceReceiver.play";//----通知栏播放按钮
public static final String NOTIFICATION_ITEM_BUTTON_NEXT
= "com.example.notification.ServiceReceiver.next";//----通知栏下一首按钮
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(NOTIFICATION_ITEM_BUTTON_LAST)) {//----通知栏播放按钮响应事件
Toast.makeText(context, "上一首", Toast.LENGTH_LONG).show();
}
else if (action.equals(NOTIFICATION_ITEM_BUTTON_PLAY)) {//----通知栏播放按钮响应事件
Toast.makeText(context, "暂停", Toast.LENGTH_LONG).show();
}
else if (action.equals(NOTIFICATION_ITEM_BUTTON_NEXT)) {//----通知栏下一首按钮响应事件
Toast.makeText(context, "下一首", Toast.LENGTH_LONG).show();
}
}
}
第三步:动态注册广播
receiver = new ServiceReceiver();//----注册广播
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ServiceReceiver.NOTIFICATION_ITEM_BUTTON_LAST);
intentFilter.addAction(ServiceReceiver.NOTIFICATION_ITEM_BUTTON_PLAY);
intentFilter.addAction(ServiceReceiver.NOTIFICATION_ITEM_BUTTON_NEXT);
registerReceiver(receiver, intentFilter);
第四步:创建通知栏通知
private void showCustomView(String name) {
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.music_notification);
remoteViews.setTextViewText(R.id.title_music_name, name); //设置textview
//设置按钮事件 -- 发送广播 --广播接收后进行对应的处理
Intent buttonPlayIntent = new Intent(ServiceReceiver.NOTIFICATION_ITEM_BUTTON_LAST); //----设置通知栏按钮广播
PendingIntent pendButtonPlayIntent = PendingIntent.getBroadcast(this, 0, buttonPlayIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.last_music, pendButtonPlayIntent);//----设置对应的按钮ID监控
Intent buttonPlayIntent1 = new Intent(ServiceReceiver.NOTIFICATION_ITEM_BUTTON_PLAY); //----设置通知栏按钮广播
PendingIntent pendButtonPlayIntent1 = PendingIntent.getBroadcast(this, 0, buttonPlayIntent1, 0);
remoteViews.setOnClickPendingIntent(R.id.paly_pause_music, pendButtonPlayIntent1);//----设置对应的按钮ID监控
Intent buttonPlayIntent2 = new Intent(ServiceReceiver.NOTIFICATION_ITEM_BUTTON_NEXT); //----设置通知栏按钮广播
PendingIntent pendButtonPlayIntent2 = PendingIntent.getBroadcast(this, 0, buttonPlayIntent2, 0);
remoteViews.setOnClickPendingIntent(R.id.next_music, pendButtonPlayIntent2);//----设置对应的按钮ID监控
NotificationCompat.Builder builder = new Builder(MainActivity.this);
builder.setContent(remoteViews).setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(icon).setOngoing(true)
.setTicker("music is playing");
manager.notify(1, builder.build());
}
附件下载链接:/detail/luohuazhi1991/7347427
到这里就可以触发通知栏中按钮的点击事件了, 但是点击暂停的时候,没有比较好的方法去切换图片,虽然可以实现,但是个人觉得不怎么好,有晓得的童鞋麻烦告诉一声,谢谢哦。