上一篇讲述了Notification的基础用法,本篇将介绍,自定义通知栏,并利用讲到的内容,实现一个简单的音乐播发器。
1.自定义通知的实现;
Notification有一个contentView属性,该属性接受的对象是RemoteView对象,用它即可实现自定义布局.
获取RemoteView对象的方法:
<span style="font-size:14px;">RemoteView remoteViews = new RemoteViews(getPackageName(),R.layout.notify_view);</span>
2.添加通知栏响应事件
Intent buttonplayIntent = new Intent("play");
PendingIntent pendplayButtonIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, buttonplayIntent, 0);
mRemoteViews.setOnClickPendingIntent(R.id.play, pendplayButtonIntent);
其中关于PendingIntent
PendingIntent用于描述Intent及其最终的行为.
你可以通过getActivity(Context context, int requestCode, Intent intent, int flags)系列方法从系统取得一个用于启动一个Activity的PendingIntent对象,
可以通过getService(Context
context, int requestCode, Intent intent, int flags)方法从系统取得一个用于启动一个Service的PendingIntent对象
可以通过getBroadcast(Context
context, int requestCode, Intent intent, int flags)方法从系统取得一个用于向BroadcastReceiver的Intent广播的PendingIntent对象
返回的PendingIntent可以递交给别的应用程序,然后继续处理。这里的话你可以稍后才处理PendingIntent中描述的Intent及其最终行为。
3.完整的创建自定义通知方法:
Notification mNotification;
RemoteViews mRemoteViews;
NotificationManager notificationManager;
public void createNotifiView() {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotification = new Notification(R.drawable.ic_launcher, "MusicDemo",
System.currentTimeMillis());
mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClass(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
getApplicationContext(), 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
mNotification.contentIntent = pendingIntent;
if (mRemoteViews == null) {
mRemoteViews = new RemoteViews(getPackageName(),
R.layout.notify_view);
}
Intent buttoncloseIntent = new Intent("close");
PendingIntent pendcloseButtonIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, buttoncloseIntent, 0);
mRemoteViews.setOnClickPendingIntent(R.id.close, pendcloseButtonIntent);
Intent buttonplayIntent = new Intent("play");
PendingIntent pendplayButtonIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, buttonplayIntent, 0);
mRemoteViews.setOnClickPendingIntent(R.id.play, pendplayButtonIntent);
//
Intent buttonnextIntent = new Intent("next");
PendingIntent pendnextButtonIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, buttonnextIntent, 0);
mRemoteViews.setOnClickPendingIntent(R.id.next, pendnextButtonIntent);
Intent buttonprewtIntent = new Intent("prew");
PendingIntent pendprewButtonIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, buttonprewtIntent, 0);
mRemoteViews.setOnClickPendingIntent(R.id.prew, pendprewButtonIntent);
mRemoteViews.setTextViewText(R.id.songName, "歌曲名");
if (!MainActivity.isPlay) {
mRemoteViews.setImageViewResource(R.id.play,
R.drawable.statusbar_btn_play);
mRemoteViews.setOnClickPendingIntent(R.id.play,
pendplayButtonIntent);
} else {
mRemoteViews.setImageViewResource(R.id.play,
R.drawable.statusbar_btn_pause);
mRemoteViews.setOnClickPendingIntent(R.id.play,
pendplayButtonIntent);
}
mNotification.contentView = mRemoteViews;
notificationManager.notify(0, mNotification);
}
因为这里用了getBroadCast()获取PendingIntent,所以该Intent是通过发送广播来告知通知栏控件发生了触发事件。
所以,需要在响应界面过滤action,注册响应广播。
先给出通知布局文件notifiy_view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:ignore="ContentDescription" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="64dp"
android:orientation="vertical" >
<ImageView
android:id="@+id/icon_pic"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerVertical="true" />
<ImageButton
android:id="@+id/close"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/status_bg"
android:src="@drawable/statusbar_close" />
<TextView
android:id="@+id/songName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:layout_toLeftOf="@+id/close"
android:layout_toRightOf="@+id/icon_pic"
android:ellipsize="end"
android:singleLine="true"
android:text="歌名"
android:textSize="15dp" />
<ImageButton
android:id="@+id/prew"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:background="@drawable/status_bg"
android:src="@drawable/statusbar_btn_prev" />
<ImageButton
android:id="@+id/play"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignTop="@+id/prew"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/prew"
android:background="@drawable/status_bg"
android:src="@drawable/statusbar_btn_play" />
<ImageButton
android:id="@+id/next"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignTop="@+id/prew"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/play"
android:background="@drawable/status_bg"
android:src="@drawable/statusbar_btn_next" />
</RelativeLayout>
</LinearLayout>
我将会把项目放在优快云上,大家可以下载,获取相应的资源文件
在这个小项目中,我将播放的service做为的是后台服务,然后广播也提出来了,作为一个公用的广播,如果只是做简单播放用,可以将广播放在播放界面。
项目中用到的广播:
public class MusicBroadCast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent musicServiceIntent = new Intent(context, MusicService.class);
if (intent.getAction().equals("play")) {
musicServiceIntent.putExtra(MusicService.MusicCommandKey, ""
+ MusicService.MusicPlayCommand);
context.startService(musicServiceIntent);
} else if (intent.getAction().equals("next")) {
musicServiceIntent.putExtra(MusicService.MusicCommandKey, ""
+ MusicService.MusicNextCommand);
context.startService(musicServiceIntent);
} else if (intent.getAction().equals("prew")) {
musicServiceIntent.putExtra(MusicService.MusicCommandKey, ""
+ MusicService.MusicNextCommand);
context.startService(musicServiceIntent);
} else if (intent.getAction().equals("close")) {
musicServiceIntent.putExtra(MusicService.MusicCommandKey, "");
context.startService(musicServiceIntent);
} else if (intent.getAction().equals(
"android.media.AUDIO_BECOMING_NOISY")) {// 耳机拔出事件
musicServiceIntent.putExtra(MusicService.MusicCommandKey,
MusicService.MusicPauseCommand
+ MusicService.MusicPauseCommand);
context.startService(musicServiceIntent);
}
}
}
我这里是在service注册的广播,这个应该有点夸张了,由于项目需要全局播放,然后由通知栏也可以控制,就这样做了。
service文件代码:
package com.example.musicdemo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.RemoteViews;
import java.io.IOException;
/**
* Create by gaylen on 2015/12/9 11:07
*/
public class MusicService extends Service {
// 其他对象控制Service进行音频播放操作
// 全部采用命令+命令参数的形式处理
public static final int MusicInitCommand = 0;
public static final int MusicPlayCommand = 10;
public static final int MusicStopCommand = 20;
public static final int MusicPauseCommand = 30;
public static final int MusicSeekToCommand = 40;
public static final int MusicNextCommand = 50;
public static final int MusicPrevCommand = 60;
public static final int MusicNotificationCommand = 70;
// 所有的命令参数都String传递
// 非String类型,采取转型处理
public static String MusicCommandKey = "CtrlCommand";
public static String MusicParameterKey = "CommandParameter";
private MusicBroadCast broadCast;
@Override
public void onCreate() {
super.onCreate();
broadCast = new MusicBroadCast();
IntentFilter filter = new IntentFilter();
filter.addAction("play");
filter.addAction("next");
filter.addAction("prew");
filter.addAction("close");
// 耳机
filter.addAction("android.media.AUDIO_BECOMING_NOISY");
// 短信
// mSystemFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(broadCast, filter);
regeisterCallLisenler();
/**
* 为了防止通知栏点击时出现闪屏,在开启service就将一些对象实例化了
*/
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotification = new Notification(R.drawable.ic_launcher, "MusicDemo",
System.currentTimeMillis());
mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClass(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
getApplicationContext(), 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
mNotification.contentIntent = pendingIntent;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 分析传入的命令,根据不同命令调用不同方法
String commandValue = intent.getStringExtra(MusicCommandKey);
if (commandValue != null) {
int command = Integer.parseInt(commandValue);
switch (command) {
case MusicService.MusicPlayCommand:
// 得到play命令对应的参数
String Param = intent.getStringExtra(MusicParameterKey);
// 执行doPlayCommand方法
doPlayCommand(Param);
break;
case MusicService.MusicStopCommand:
stopPlayCommand();
break;
case MusicService.MusicSeekToCommand:
// 解析命令参数执行相关方法
// 得到SeekTo的进度(字符串形式)
String strProgressSet = intent
.getStringExtra(MusicService.MusicParameterKey);
doSeekToCommand(strProgressSet);
break;
case MusicService.MusicNextCommand:
doNextCommand();
break;
case MusicService.MusicPrevCommand:
doPreCommand();
break;
case MusicService.MusicNotificationCommand:
createNotifiView();
break;
}
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
// Service 被结束,则音乐停止
unregisterReceiver(broadCast);
}
/**
* 停止播放
*/
private void stopPlayCommand() {
Log.i("play", "stop");
createNotifiView();
}
private void doPause() {
Log.i("play", "pause");
}
/**
* 播放
*
* @param param
*/
private void doPlayCommand(String param) {
if (MainActivity.isPlay) {
doPause();
} else {
Log.i("play", "play");
}
createNotifiView();
}
// 播放下一首命令处理
private void doNextCommand() {
Log.i("play", "next");
}
// 播放上一首命令处理
private void doPreCommand() {
Log.i("play", "pre");
}
private void doSeekToCommand(String seekToValue) {
int seekTo = Integer.parseInt(seekToValue);
// 设置跳转进度
// mMediaPlayer.seekTo(seekTo);
}
Notification mNotification;
RemoteViews mRemoteViews;
NotificationManager notificationManager;
public void createNotifiView() {
if (mRemoteViews == null) {
mRemoteViews = new RemoteViews(getPackageName(),
R.layout.notify_view);
}
Intent buttoncloseIntent = new Intent("close");
PendingIntent pendcloseButtonIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, buttoncloseIntent, 0);
mRemoteViews.setOnClickPendingIntent(R.id.close, pendcloseButtonIntent);
Intent buttonplayIntent = new Intent("play");
PendingIntent pendplayButtonIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, buttonplayIntent, 0);
mRemoteViews.setOnClickPendingIntent(R.id.play, pendplayButtonIntent);
//
Intent buttonnextIntent = new Intent("next");
PendingIntent pendnextButtonIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, buttonnextIntent, 0);
mRemoteViews.setOnClickPendingIntent(R.id.next, pendnextButtonIntent);
Intent buttonprewtIntent = new Intent("prew");
PendingIntent pendprewButtonIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, buttonprewtIntent, 0);
mRemoteViews.setOnClickPendingIntent(R.id.prew, pendprewButtonIntent);
mRemoteViews.setTextViewText(R.id.songName, "歌曲名");
if (!MainActivity.isPlay) {
mRemoteViews.setImageViewResource(R.id.play,
R.drawable.statusbar_btn_play);
mRemoteViews.setOnClickPendingIntent(R.id.play,
pendplayButtonIntent);
} else {
mRemoteViews.setImageViewResource(R.id.play,
R.drawable.statusbar_btn_pause);
mRemoteViews.setOnClickPendingIntent(R.id.play,
pendplayButtonIntent);
}
mNotification.contentView = mRemoteViews;
notificationManager.notify(0, mNotification);
}
private AudioManager mAudioManager;
public void regeisterCallLisenler() {
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// 添加来电监听事件
TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); // 获取系统服务
telManager.listen(new MobliePhoneStateListener(),
PhoneStateListener.LISTEN_CALL_STATE);
}
/**
* @author wwj 电话监听器类
*/
private class MobliePhoneStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE: // 挂机状态
// doPlayCommand("");
break;
case TelephonyManager.CALL_STATE_OFFHOOK: // 通话状态
case TelephonyManager.CALL_STATE_RINGING: // 响铃状态
Log.i("tag", "接收到来电-->");
doPause();
break;
default:
break;
}
}
}
}
最后就是播放主界面:
public class MainActivity extends ActionBarActivity {
public static boolean isPlay = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
player();
}
public void player() {
Intent musicServiceIntent = new Intent(MainActivity.this,
MusicService.class);
// 设置播放控制命令:playCommand
musicServiceIntent.putExtra(MusicService.MusicCommandKey, ""
+ MusicService.MusicPlayCommand);
MainActivity.this.startService(musicServiceIntent);
}
}
布局跟通知栏布局差不多,这里就不写了。
完整项目源码可以去:http://download.youkuaiyun.com/detail/caihuajian235/9365027下载。