基于android的网络音乐播放器-通知栏控制(RemoteViews)(十)

来源:互联网 发布:魔音通话软件 编辑:程序博客网 时间:2024/05/21 09:57
到这里音乐播放器该有的功能基本都有了,最后再添加一个通知栏控制功能——当我们后台运行的时候可以在通知栏看到我们的音乐播放状态并且可以控制音乐的暂停/播放/上一首/下一首;点击通知图标即可返回音乐播放器。要实现通知栏的显示功能需要用到Notification,他的视图是通过使用RemoteViews来实现的,Notification的显示和取消需要通过NotificationManager来控制。下面代码实现了通知栏的显示:
public class MainActivity extends Activity {    ......    private NotificationManager nm;    private RemoteViews contentViews;    private Notification notify;    private int NOTIFICATION_ID = 123;    private boolean showNotification;    ......    protected void onCreate(Bundle savedInstanceState) {        ......       initNotification();    }private void initNotification() {        //NotificationManager的获取        nm = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);        Intent mainIntent = new Intent(MainActivity.this, MainActivity.class);          PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, mainIntent, 0);        notify = new Notification();        notify.when = System.currentTimeMillis();        notify.icon = R.drawable.music;        notify.contentIntent = pi;//点击通知跳转到MainActivity        notify.flags = Notification.FLAG_AUTO_CANCEL;               contentViews = new RemoteViews(getPackageName(), R.layout.notification);         contentViews.setOnClickPendingIntent(R.id.playtag, pi);        contentViews.setOnClickPendingIntent(R.id.currentmusic, pi);        //上一首图标添加点击监听        Intent previousButtonIntent = new Intent(ACTION_PRE_SONG);        PendingIntent pendPreviousButtonIntent = PendingIntent.getBroadcast(this, 0, previousButtonIntent, 0);          contentViews.setOnClickPendingIntent(R.id.pre, pendPreviousButtonIntent);        //播放/暂停添加点击监听        Intent playPauseButtonIntent = new Intent(ACTION_PLAY_AND_PAUSE);        PendingIntent playPausePendingIntent = PendingIntent.getBroadcast(this, 0, playPauseButtonIntent, 0);          contentViews.setOnClickPendingIntent(R.id.playandpause, playPausePendingIntent);        //下一首图标添加监听        Intent nextButtonIntent = new Intent(ACTION_NEXT_SONG);        PendingIntent pendNextButtonIntent = PendingIntent.getBroadcast(this, 0, nextButtonIntent, 0);          contentViews.setOnClickPendingIntent(R.id.next, pendNextButtonIntent);                //退出监听        Intent exitButton = new Intent(ACTION_EXIT);          PendingIntent pendingExitButtonIntent = PendingIntent.getBroadcast(this,0,exitButton,0);          contentViews.setOnClickPendingIntent(R.id.close,pendingExitButtonIntent);    }private void showNotification() {        showNotification = true;        if(isPlaying){              contentViews.setImageViewResource(R.id.playandpause,android.R.drawable.ic_media_pause);          }        else{            contentViews.setImageViewResource(R.id.playandpause,android.R.drawable.ic_media_play);          }        contentViews.setTextViewText(R.id.currentmusic, currentMusicTitle + "—" + currentMusicArtist);        String filePath = MainActivity.downloadedPath + "/album/"+ currentMusicTitle + "-" + currentMusicArtist + ".jpg";        if (new File(filePath).exists()) {            Bitmap bitmap = BitmapUtil.getScropBitmap(filePath, 60, 60);            contentViews.setImageViewBitmap(R.id.playtag, bitmap);        }        notify.contentView = contentViews;        nm.notify(NOTIFICATION_ID, notify);//调用notify方法后即可显示通知    }private BroadcastReceiver playMusicReceiver = new BroadcastReceiver() {        public void onReceive(Context context, Intent intent) {            String action = intent.getAction();            Log.d(TAG, "action = " + action);            if (action.equals(ACTION_NEXT_SONG)) {                nextSong();            } else if (action.equals(ACTION_PLAY_SONG)) {                playMusic(currentMusicPos);            } else if (action.equals(ACTION_PAUSE)) {                pauseMusic();            } else if (action.equals(ACTION_PRE_SONG)) {                preSong();            } else if (action.equals(ACTION_CONTINUE_PLAYING_SONG)) {                continuePlaying();            } else if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {//在播放器中点击home键时显示通知栏图标                showNotification();            } else if (action.equals(ACTION_EXIT)) {//通知栏点击退出图标                finish();            } else if (action.equals(ACTION_PLAY_AND_PAUSE)) {                if (pause) {                    continuePlaying();                    pause = false;                } else if (isPlaying) {                    pauseMusic();                } else {                    playMusic(currentMusicPos);                }            }        }    };@Override    protected void onStart() {    //回到音乐播放器时关闭通知        if (showNotification) {            nm.cancel(NOTIFICATION_ID);            showNotification = false;        }        updatePlayMusicInfo();        super.onStart();    }Notification的显示和关闭主要是通过NotificationManager.notify()和NotificationManager.cancel()实现的。}

Notification的布局文件notification.xml如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"            android:id="@+id/notification"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="horizontal"            android:gravity="center_vertical"            android:background="#65cb65"            android:alpha="0.7">        <!--android:background="#65cb65"  -->        <ImageView            android:id="@+id/playtag"            android:layout_width="0dp"            android:layout_height="60dp"            android:layout_weight="1"            android:src="@drawable/launcher"            android:layout_marginLeft="5dp" />        <TextView             android:id="@+id/currentmusic"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="3"            android:layout_marginLeft="10dp"            android:gravity="center_vertical"            android:textSize="16dp"            android:textColor="#f00"            android:singleLine="true"             android:ellipsize="marquee"             android:marqueeRepeatLimit="marquee_forever"            android:scrollHorizontally = "true"             android:focusable ="true"            android:focusableInTouchMode="true"/>        <ImageButton            android:id="@+id/pre"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:src="@android:drawable/ic_media_previous"            android:layout_weight="1"             android:background="#dd00dd"/>        <ImageButton            android:id="@+id/playandpause"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_marginLeft="10dp"            android:layout_marginRight="10dp"            android:layout_weight="1"            android:background="#dd00dd"            android:src="@android:drawable/ic_media_play" />        <ImageButton            android:id="@+id/next"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_marginRight="10dp"            android:layout_weight="1"            android:background="#dd00dd"            android:src="@android:drawable/ic_media_next" />        <ImageView            android:id="@+id/close"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:src="@android:drawable/ic_delete" /></LinearLayout>

音乐播放器已完成,下载地址:
Android音乐播放器

0 0
原创粉丝点击