日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 音乐播放器的状态栏通知,Android仿虾米音乐播放器之通知栏notification解析...

發布時間:2023/12/10 Android 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 音乐播放器的状态栏通知,Android仿虾米音乐播放器之通知栏notification解析... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

通知欄notification是Android中一個很重要的組件,可以在頂部狀態欄中存在,用戶也可以通過此來操作應用,在Android中只有3.0以上的版本才加入了notification的按鈕點擊功能。

先看一下仿蝦米寫出來的通知的效果

這是一個自定義的notification,添加了,前一曲、播放、暫停、下一曲等功能,自定義的notification需要自己寫布局文件,并通過remoteview顯示在界面中。

res/layout/customnotice.xml

寫了布局之后就需要實例化通知了

1.定義一個NotificationManager對象,并實例化

2.定義一個RemoteViews對象

3.設置RemoteViews屬性

private NotificationManager manager;

private RemoteViews remoteViews;

manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

/**

* 設置通知

*/

@SuppressLint("NewApi")

private void setNotification() {

NotificationCompat.Builder builder = new Builder(this);

Intent intent = new Intent(this, MainActivity.class);

// 點擊跳轉到主界面

PendingIntent intent_go = PendingIntent.getActivity(this, 5, intent,

PendingIntent.FLAG_UPDATE_CURRENT);

remoteViews.setOnClickPendingIntent(R.id.notice, intent_go);

// 4個參數context, requestCode, intent, flags

PendingIntent intent_close = PendingIntent.getActivity(this, 0, intent,

PendingIntent.FLAG_UPDATE_CURRENT);

remoteViews.setOnClickPendingIntent(R.id.widget_close, intent_close);

// 設置上一曲

Intent prv = new Intent();

prv.setAction(Constants.ACTION_PRV);

PendingIntent intent_prev = PendingIntent.getBroadcast(this, 1, prv,

PendingIntent.FLAG_UPDATE_CURRENT);

remoteViews.setOnClickPendingIntent(R.id.widget_prev, intent_prev);

// 設置播放

if (Myapp.isPlay) {

Intent playorpause = new Intent();

playorpause.setAction(Constants.ACTION_PAUSE);

PendingIntent intent_play = PendingIntent.getBroadcast(this, 2,

playorpause, PendingIntent.FLAG_UPDATE_CURRENT);

remoteViews.setOnClickPendingIntent(R.id.widget_play, intent_play);

}

if (!Myapp.isPlay) {

Intent playorpause = new Intent();

playorpause.setAction(Constants.ACTION_PLAY);

PendingIntent intent_play = PendingIntent.getBroadcast(this, 6,

playorpause, PendingIntent.FLAG_UPDATE_CURRENT);

remoteViews.setOnClickPendingIntent(R.id.widget_play, intent_play);

}

// 下一曲

Intent next = new Intent();

next.setAction(Constants.ACTION_NEXT);

PendingIntent intent_next = PendingIntent.getBroadcast(this, 3, next,

PendingIntent.FLAG_UPDATE_CURRENT);

remoteViews.setOnClickPendingIntent(R.id.widget_next, intent_next);

// 設置收藏

PendingIntent intent_fav = PendingIntent.getBroadcast(this, 4, intent,

PendingIntent.FLAG_UPDATE_CURRENT);

remoteViews.setOnClickPendingIntent(R.id.widget_fav, intent_fav);

builder.setSmallIcon(R.drawable.notification_bar_icon); // 設置頂部圖標

Notification notify = builder.build();

notify.contentView = remoteViews; // 設置下拉圖標

notify.bigContentView = remoteViews; // 防止顯示不完全,需要添加apisupport

notify.flags = Notification.FLAG_ONGOING_EVENT;

notify.icon = R.drawable.notification_bar_icon;

manager.notify(100, notify);

}

PendingIntent是一種特殊的intent,設置之后并不會馬上使用,而是在真正點擊后只會調用。Android默認使用的通知高度大概是64dp,無法滿足大界面的顯示,所以需要在其中設置bigContentView,這樣才能顯示大圖。manager.notify(id, notification)中第一個參數是一個標識碼,在取消通知的時候需要傳入這個參數,第二個就是一個notify對象。

通知欄一次實例化后里面按鈕的點擊事件就確定了,如果當前設置的是播放的按鈕,那么暫停是不會起作用的,所以需要重新設置通知,目前采用的是用Handler來更新通知欄。

public Handler handler = new Handler() {

public void handleMessage(android.os.Message msg) {

Mp3Info info = (Mp3Info) msg.obj;

Bitmap bitmap = MediaUtil.getArtwork(getApplicationContext(),

info.getId(), info.getAlbumId(), true, false);

btm_album.setImageBitmap(bitmap);

btm_artist.setText(info.getArtist());

btm_title.setText(info.getTitle());

// 播放歌曲

btm_state

.setImageResource(R.drawable.player_btn_radio_pause_normal);

// 設置通知欄的圖片文字

remoteViews = new RemoteViews(getPackageName(),

R.layout.customnotice);

remoteViews.setImageViewBitmap(R.id.widget_album, bitmap);

remoteViews.setTextViewText(R.id.widget_title, info.getTitle());

remoteViews.setTextViewText(R.id.widget_artist, info.getArtist());

if (Myapp.isPlay) {

remoteViews.setImageViewResource(R.id.widget_play, R.drawable.widget_btn_pause_normal);

}else {

remoteViews.setImageViewResource(R.id.widget_play, R.drawable.widget_btn_play_normal);

}

setNotification();

};

};

在退出的時候需要取消通知欄,用NotificationManager的manager對象取消通知view,其中的100是notify設置的id,用來區別通知,因為在其他應用中可能會產生好多通知。

@Override

protected void onDestroy() {

Log.i("---"+TAG, "ondestory");

super.onDestroy();

if (remoteViews != null) {

manager.cancel(100);

}

if (receiver != null) {

unregisterReceiver(receiver);

}

}

總結

以上是生活随笔為你收集整理的android 音乐播放器的状态栏通知,Android仿虾米音乐播放器之通知栏notification解析...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。