Android 音乐播放器的通知栏

时间:2021-04-10 10:15:58

Android音乐播放器的通知栏

效果说明
我点击×按钮会关闭通知栏和应用,
点击停止按钮会在界面显示停止并且这个按钮的图片换成播放图片,再点击一次界面显示播放,且图片换回来
点击上一首和下一首按钮会在界面上分别显示上一首和下一首的字符串。

制作过程

首先我先贴一些关键代码,源码地址最后给出

public class MainActivity extends AppCompatActivity {

    private static final int NOTIFICATION_ID = 1; // 如果id设置为0,会导致不能设置为前台service
    public static NotificationManager manager;
    RemoteViews remoteViews;
    Notification notification;
    private Button btn;
    private TextView tv;
    static MainActivity appCompatActivity;
    int state = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(appCompatActivity == null){
            appCompatActivity = this;
        }

        btn = (Button)findViewById(R.id.btn);
        tv = (TextView)findViewById(R.id.tv);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                createNotifcation();
            }
        });

    }

    public void createNotifcation(){
        notification = new Notification();
        notification.icon =R.drawable.musicfile;
        notification.tickerText = "title";
        notification.when = System.currentTimeMillis();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        remoteViews = new RemoteViews(getPackageName(),
                R.layout.widget);
        remoteViews.setTextViewText(R.id.wt_title, "title");
        remoteViews.setImageViewResource(R.id.icon1, R.drawable.musicfile);
        if(state == 0){
            remoteViews.setImageViewResource(R.id.wt_play,R.drawable.pause);
        }else {
            remoteViews.setImageViewResource(R.id.wt_play,R.drawable.play);
        }

        Intent previous=new Intent("com.example.broadcasttest.PREVIOUS");
        PendingIntent pi_previous = PendingIntent.getBroadcast(MainActivity.this,0,
                previous,PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.wt_previous,pi_previous);


        Intent play=new Intent("com.example.broadcasttest.PLAY");
        PendingIntent pi_play = PendingIntent.getBroadcast(MainActivity.this,0,
                play,PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.wt_play,pi_play);

        Intent next=new Intent("com.example.broadcasttest.NEXT");
        PendingIntent pi_next = PendingIntent.getBroadcast(MainActivity.this,0,
                next,PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.wt_next,pi_next);

        Intent clear=new Intent("com.example.broadcasttest.CLEAR");
        PendingIntent pi_clear = PendingIntent.getBroadcast(MainActivity.this,0,
                clear,PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.wt_clear,pi_clear);

        notification.contentView = remoteViews;
        manager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE);

        manager.notify(NOTIFICATION_ID,notification);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(manager != null ){
            manager.cancel(NOTIFICATION_ID);
        }
    }

    public static MainActivity getInstance(){

        return appCompatActivity;
    }

    public void nextMusic(){
        tv.setText("下一首");
    }

    public void playMusic(){

        if(state == 0){
            tv.setText("播放");
            state =1;
        }else {
            tv.setText("暂停");
            state = 0;
        }

        createNotifcation();


    }
    public void previousMusic(){
        tv.setText("上一首");
    }

    public void clearMusic(){
        finish();
    }
}

然后是广播的代码,我就贴出一个,其他的广播都一个样

public class NextBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent){
        final MainActivity musicActivity = MainActivity.getInstance();
        musicActivity.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                musicActivity.nextMusic();
            }
        });

    }
}

我们可以看到我们使用RemoteView来做通知栏,因为这样可以制作自定义通知栏,并且能够给控件设置点击事件,而且还能改变控件的一些属性,比如图片和字符串之类的,但是我们需要注意我们只能改变TextView和ImageView的样式。

还有就是这个点击事件我是通过广播来实现,广播通过获取activity的实例来改变activity的界面

然后我们在onDestroy()设置取消通知栏,以防出现Force Close

其实可以用service来管理通知栏,但是我有点懒,大家可以自己去改改,不懂的来问

最后源代码地址
979451341/MusicNotification: 音乐播放器的通知栏
https://github.com/979451341/MusicNotification