迷你主机做网站,wordpress和织梦哪个好,青海省安建设管理部门网站,设计作品图片上一篇讲述了Notification的基础用法#xff0c;本篇将介绍#xff0c;自定义通知栏#xff0c;并利用讲到的内容#xff0c;实现一个简单的音乐播发器。 1.自定义通知的实现#xff1b; Notification有一个contentView属性#xff0c;该属性接受的对象是RemoteView对象本篇将介绍自定义通知栏并利用讲到的内容实现一个简单的音乐播发器。 1.自定义通知的实现 Notification有一个contentView属性该属性接受的对象是RemoteView对象用它即可实现自定义布局. 获取RemoteView对象的方法 span stylefont-size:14px;RemoteView remoteViews  new RemoteViews(getPackageName(),R.layout.notify_view);/span2.添加通知栏响应事件 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 version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:orientationverticaltools:ignoreContentDescription RelativeLayoutandroid:layout_widthmatch_parentandroid:layout_height64dpandroid:orientationvertical ImageViewandroid:idid/icon_picandroid:layout_width60dpandroid:layout_height60dpandroid:layout_centerVerticaltrue /ImageButtonandroid:idid/closeandroid:layout_width30dpandroid:layout_height30dpandroid:layout_alignParentRighttrueandroid:layout_alignParentToptrueandroid:layout_marginRight10dpandroid:layout_marginTop10dpandroid:backgrounddrawable/status_bgandroid:srcdrawable/statusbar_close /TextViewandroid:idid/songNameandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginLeft10dpandroid:layout_marginRight5dpandroid:layout_marginTop10dpandroid:layout_toLeftOfid/closeandroid:layout_toRightOfid/icon_picandroid:ellipsizeendandroid:singleLinetrueandroid:text歌名android:textSize15dp /ImageButtonandroid:idid/prewandroid:layout_width30dpandroid:layout_height30dpandroid:layout_alignParentBottomtrueandroid:layout_centerInParenttrueandroid:backgrounddrawable/status_bgandroid:srcdrawable/statusbar_btn_prev /ImageButtonandroid:idid/playandroid:layout_width30dpandroid:layout_height30dpandroid:layout_alignTopid/prewandroid:layout_marginLeft10dpandroid:layout_toRightOfid/prewandroid:backgrounddrawable/status_bgandroid:srcdrawable/statusbar_btn_play /ImageButtonandroid:idid/nextandroid:layout_width30dpandroid:layout_height30dpandroid:layout_alignTopid/prewandroid:layout_marginLeft10dpandroid:layout_toRightOfid/playandroid:backgrounddrawable/status_bgandroid:srcdrawable/statusbar_btn_next //RelativeLayout/LinearLayout我将会把项目放在CSDN上大家可以下载获取相应的资源文件在这个小项目中我将播放的service做为的是后台服务然后广播也提出来了作为一个公用的广播如果只是做简单播放用可以将广播放在播放界面。 项目中用到的广播 public class MusicBroadCast extends BroadcastReceiver {Overridepublic 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;Overridepublic 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;}NullableOverridepublic IBinder onBind(Intent intent) {return null;}Overridepublic 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);}Overridepublic 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 {Overridepublic 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;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);player();}public void player() {Intent musicServiceIntent  new Intent(MainActivity.this,MusicService.class);// 设置播放控制命令:playCommandmusicServiceIntent.putExtra(MusicService.MusicCommandKey,  MusicService.MusicPlayCommand);MainActivity.this.startService(musicServiceIntent);}
}布局跟通知栏布局差不多这里就不写了。完整项目源码可以去http://download.csdn.net/detail/caihuajian235/9365027下载。