android:使用RemoteView自定義Notification


//網上相關內容較少,遂記錄下來,備忘.
//依然以音樂播放器demo為例.

效果截圖

//錘子手機上的效果

step1 准備自定義layout

常規的實現方式,並不會因為是用於notification的而在實現上有所不同.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_margin="10dp"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <LinearLayout
        android:layout_gravity="center"
        android:layout_marginLeft="10dp"
        android:orientation="vertical"
        android:layout_width="170dp"
        android:layout_height="wrap_content">
        <TextView
            android:layout_marginLeft="10dp"
            android:id="@+id/music_name"
            android:textSize="20dp"
            android:text="要怎么辦"
            android:maxLines="1"
            android:ellipsize="end"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:layout_marginLeft="10dp"
            android:layout_marginTop="6dp"
            android:id="@+id/singer_name"
            android:textSize="16dp"
            android:text="李柏凝"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>

    <LinearLayout
        android:layout_gravity="center"
        android:layout_marginTop="5dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <ImageButton
            android:id="@+id/btn_prev"
            android:background="@drawable/desk_pre"
            android:layout_width="40dp"
            android:layout_height="40dp"/>
        <ImageButton
            android:layout_marginLeft="10dp"
            android:id="@+id/btn_play"
            android:src="@drawable/note_btn_play"
            android:background="#00ffffff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <ImageButton
            android:layout_marginLeft="10dp"
            android:id="@+id/btn_next"
            android:background="@drawable/desk_next"
            android:layout_width="40dp"
            android:layout_height="40dp"/>
    </LinearLayout>
</LinearLayout>

//以下內容均為service中的實現

step2 使用以上layout文件創建一個RemoteView實例

    private void initRemoteView() {
        //創建一個RemoteView實例
        mRemoteViews = new RemoteViews(getPackageName(), R.layout.music_notification);
        mRemoteViews.setTextViewText(R.id.music_name, mMusicDatas.get(i).getName());
        mRemoteViews.setTextViewText(R.id.singer_name, mMusicDatas.get(i).getSinger());

        //實例化一個指向MusicService的intent
        Intent intent = new Intent(this, MusicService.class);
        intent.setAction(ACTION_NOTIFICATION);

        //設置play按鈕的點擊事件
        intent.putExtra(BUTTON_INDEX, BUTTON_PLAY);
        PendingIntent pendingIntent = PendingIntent.getService(this, 2, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        mRemoteViews.setOnClickPendingIntent(R.id.btn_play, pendingIntent);

        //設置next按鈕的點擊事件
        intent.putExtra(BUTTON_INDEX, BUTTON_NEXT);
        pendingIntent = PendingIntent.getService(this, 3, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        mRemoteViews.setOnClickPendingIntent(R.id.btn_next, pendingIntent);

        //設置prev按鈕的點擊事件
        intent.putExtra(BUTTON_INDEX, BUTTON_PREV);
        pendingIntent = PendingIntent.getService(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        mRemoteViews.setOnClickPendingIntent(R.id.btn_prev, pendingIntent);
    }

step3 使用RemoteView實例創建Nitification

    private void initNotification() {

        //實例化一個Builder
        mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setSmallIcon(R.drawable.default_pic);
        //將remoteView設置進去
        mBuilder.setContent(mRemoteViews);
        //獲取NotificationManager實例
        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }

step4 重寫onStartCommand()用於處理Notification中按鈕的點擊事件,舉例如下:

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        String action = intent.getAction();
        String stringExtra = intent.getStringExtra(BUTTON_INDEX);
        
        //校驗action 
        if(TextUtils.equals(action, ACTION_NOTIFICATION)) {
            //校驗stringExtra 
            if (TextUtils.equals(stringExtra, BUTTON_NEXT)) {
                i = (i+1)>=mMusicDatas.size()? 0 : i+1;
                mMediaPlayer.stop();
                mMediaPlayer = MediaPlayer.create(MusicService.this, mMusicDatas.get(i).getSrc());
                if(isPlaying) {
                    mMediaPlayer.start();
                }
                 
                //重置Notification顯示的內容
                mRemoteViews.setTextViewText(R.id.music_name, mMusicDatas.get(i).getName());
                mRemoteViews.setTextViewText(R.id.singer_name, mMusicDatas.get(i).getSinger());
                mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
            } else if(TextUtils.equals(stringExtra, BUTTON_PLAY)) {
               //...
            } else {
               //...
            }

        }
        return super.onStartCommand(intent, flags, startId);
    }

以上.

github地址:https://github.com/zhangbz/MusicPlayer


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM