android获取第三方音乐信息

获取第三方音乐信息

1、通过notificationListener

//mainfest 注册
<service android:name=".service.MusicNotificationListenerService"
   android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
   <intent-filter>
       <action android:name="android.service.notification.NotificationListenerService" />
   </intent-filter>
</service>

2、 在通知栏中获取音乐信息

public class MusicNotificationListenerService extends NotificationListenerService{

    private int musicInfoCount=0;
    private String musicTitle,musicSinger;
    private String mediaFocusPackageName;
    @Override
    public void onCreate() {
        Log.d("TAG", "onCreate: ");
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {

        // Log.e(TAG, "onNotificationPosted...");
        Log.d("TAG", "onNotificationPosted: "+mediaFocusPackageName);
        // 获取接收消息APP的包名
        String notificationPkg = sbn.getPackageName();
        Log.d("TAG", "onNotificationPosted: ");
        if (notificationPkg!=null&&notificationPkg.equals("需要获取音乐信息的包名")) {
           
              Bundle extras = sbn.getNotification().extras;
              musicTitle = extras.getString(Notification.EXTRA_TITLE);
              musicSinger = extras.getString(Notification.EXTRA_TEXT);
              if (musicTitle==null||"".equals(musicTitle.trim())){//是否得到音乐信息,没有则遍历通知ViewGroup
                  if (sbn.getNotification().bigContentView != null) {
                      ViewGroup view = (ViewGroup) sbn.getNotification().bigContentView.apply(this, null);
                      musicInfoCount=0;
                      getMusicInfo(view);

                  }
              }
              //通过广播更新音乐信息
              Intent intent=new Intent();
              intent.setAction("com.media.play");
              intent.putExtra("package_name",mediaFocusPackageName);
              intent.putExtra("music_title",musicTitle);
              intent.putExtra("music_singer",musicSinger);
              sendBroadcast(intent);
              Log.d("TAG", "onNotificationPosted: name: "+mediaFocusPackageName+
                      "title: "+musicTitle+
                      "singer: "+musicSinger);
              mediaFocusPackageName=notificationPkg;
            
        }
    }



    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        String notificationPkg = sbn.getPackageName();
        if (notificationPkg!=null) {
            if (mediaFocusPackageName.equals(notificationPkg)){
               //通知信息栏 移除音乐
                Intent intent=new Intent();
                intent.setAction("com.media.remove");
                sendBroadcast(intent);
               
            }
        }
    }
	
	//得到所有的子View
    private void getAllChildView(View view){
        ViewGroup viewGroup= (ViewGroup) view;
        for (int i=0;i<viewGroup.getChildCount();i++){
            View viewchild = viewGroup.getChildAt(i);
            if (viewchild instanceof TextView){
                Log.d("TAG", "getAllChildView: "+((TextView) viewchild).getText().toString());
            }
            if (!(viewchild instanceof ViewGroup)){
//                allchilden.add(viewchild);
                continue;
            }
            getAllChildView(viewchild);
        }
    }
	//取前两个TextView  
    private void getMusicInfo(View view){
        ViewGroup viewGroup= (ViewGroup) view;
        for (int i=0;i<viewGroup.getChildCount();i++){
            View viewchild = viewGroup.getChildAt(i);
            if (musicInfoCount>1){
                return;
            }
            if (viewchild instanceof TextView){
                Log.d("TAG", "getAllChildView: "+((TextView) viewchild).getText().toString());
                if (!"".equals(((TextView) viewchild).getText().toString().trim())){
                    if (musicInfoCount==0){
                        musicTitle=((TextView) viewchild).getText().toString();
                    }else {
                        musicSinger=((TextView) viewchild).getText().toString();
                    }
                    musicInfoCount++;
                }
            }

            if (!(viewchild instanceof ViewGroup)){
//                allchilden.add(viewchild);
                continue;
            }
            getMusicInfo(viewchild);
        }
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值