Android音乐播放器的开发教程(十)通知栏Notification的使用---小达...

dinch增塑剂
Android⾳乐播放器的开发教程(⼗)通知栏Notification的使
⽤-----⼩达
通知栏Notification的使⽤
牙签机在这⼀⽚博客中,⼩达将⾃⼰学习的⼀些想法和⼤家分享⼀哈,学的不是很深,所有有些东西可能解释的不是特别到位,还请各位谅解哈.所谓Notification
即 通知,是⼀种让你的应⽤程序在不使⽤Activity的情况下警⽰⽤户。它是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity)警⽰⽤户有需要注意的事件发⽣的最好途径。后⾯的⼀⼤段⼀⼤段是接着前⾯的项⽬在继续,⼤家可以选择性的看⼀看.
Notification 是由NotificationManager(系统服务)统⼀管理的。挂在下拉通知栏上⾯,就像下图所⽰,
上⾯显⽰的都是所谓的Notification,今天我们就来⼀步步的创建,添加按钮响应,再将Notification和我们的播放器联系起来,话不多说,现在就开始咯.
先给Notification创建了个单独的布局⽂件,当notification被激活的时候,就在通知栏上⾯显⽰这个布局⽂件的样式,
l:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="schemas.android/apk/res/android"
android:id="@+id/notification_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_photo_01">
螺旋焊缝钢管<ImageView                                                                                //显⽰歌⼿头像的,,,,,基本没怎么⽤上,
专辑图⽚不知道怎么弄得出不来
android:id="@+id/notification_artist_image"
android:layout_width="64.0dip"
android:layout_height="64.0dip"
android:background="#00000000"/>
<TextView                                                                                    //在通知栏上显⽰⾳乐名称的TextView
android:id="@+id/notification_music_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginStart="5.0dp"
android:layout_toRightOf="@+id/notification_artist_image"
android:layout_toEndOf="@+id/notification_artist_image"
android:focusable="true"
android:textColor="#FFADABFF"
android:textSize="18sp"
/>
<TextView<span >                                                                                  //在通知栏上显⽰歌⼿的TextView</span>        android:id="@+id/notification_music_Artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/notification_music_title"
android:layout_alignStart="@+id/notification_music_title"
android:layout_marginTop="5.0dip"
android:layout_below="@id/notification_music_title"
尼龙抛光轮android:textColor="#FFADABFF"
/>
<ImageButton                                                                                                      //通知栏上的退出按钮
android:id="@+id/notification_exit_button"
一叶荻布局⽂件⼤致弄好了后,就在播放歌曲的同时,直接将这个布局⽂件显⽰到通知栏上去,这⼀步需要Notification和NotificationManager,⼩达这⾥是将notification放在了service⾥⾯进⾏操作了,其他地⽅也可以的,这是service⾥⾯的⼀部分代码,后⾯会有整个service的代码的,:
private void initMyNotification() {
/*
第⼀个参数是显⽰在通知栏上的⼩图标的图⽚
第⼆个参数是在启动notification时,在状态栏上滚动的⼀句话
第三个参数是在状态栏上显⽰的时间
*/
myNotification = new Notification(ification_artist_default_image, "⼩卷⽑⾳乐", System.currentTimeMillis());
/*
NotificationManager⽤来管理notification的显⽰和消失等
*/
myNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/*
对刚才创建的⼀个notification实例设置各种参数
其中的⼀个参数是flags
这个参数可以设置该notification是何种状态
这⾥设置的是ONGOING,表⽰这个notification会⼀直呆在通知栏上⾯
也可以设置滑动后就消失的样式,看个⼈的不同需求
*/
myNotification.flags = Notification.FLAG_ONGOING_EVENT;
/*
在我们创建的notification中有⼀个View⼦类的实例
下⾯创建了⼀个RemoteViews的实例
并且配置好各种显⽰的信息后
将该实例传给notification
往后⾯看会有⼀句
这个就是传递实例了
*/
RemoteViews contentViews = new RemoteViews(getPackageName(), ification_layout);
contentViews.setImageViewResource
(ification_artist_image, ification_artist_default_image);
if(mediaPlayer.isPlaying()){
contentViews.setImageViewResource(ification_play_button,R.drawable.play_photo);
}IKRTV
else{
contentViews.setImageViewResource(ification_play_button,R.drawable.pause_photo);
}
/*
将处理好了的⼀个View传给notification
让其在通知栏上显⽰
*/
”),
</pre><pre name="code" class="java">        Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = Activity(this, 0, notificationIntent, 0);
/*
⽤notificationManager来启动notification
通过调⽤notify这个函数来启动
*/
}
第⼆步:          实例化⼀个PendingIntent,把上⾯的intent包装起来,并说明是⽤来发⼴播的intent,
PendingIntent pendPreviousButtonIntent = Broadcast(this, 0, previousButtonIntent, 0);
第三步:          都猜中了把,就是将pendingIntent和notification上的按钮联系起来
contentViews.setOnClickPendingIntent(ification_previous_song_button, pendPreviousButtonIntent);
有没有很快,,,,这样点击之后就可以发送⼀个⼴播出去了,再怎么接受纯属个⼈喜好问题了哈.....
下⾯是service的源码,好像有点长额,重点看红⾊的地⽅,是关于notification的:
application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
t.BroadcastReceiver;
t.BroadcastReceiver;
t.Context;
t.Intent;
t.IntentFilter;
dia.MediaPlayer;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.view.animation.AnimationUtils;
import android.widget.RemoteViews;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class PlayerService extends Service implements AppConstant {
private int current_position;
private String musicPath;
private String music_artist;
private String music_title;
<span >    private String notification_msg;</span>
private boolean isPause = true;
private boolean isChangToNext;
<span >    private NotificationManager myNotificationManager;                    //通知栏
private Notification myNotification;</span>
private ChangeToNextReceiver changeToNextReceiver;
private PlayReceiver playReceiver;
private ProgressChangeReceiver progressChangeReceiver;
public static MediaPlayer mediaPlayer = new MediaPlayer();
private Intent intent_to_activity = new Intent("amplemunication.RECEIVER");
private Intent intent_to_progressBar = new Intent("amplemunication.BAR");
<span >    private Intent notification_to_activity = new Intent("amplemunication.NOTIFICATION_TO_ACTIVITY");</span>    private Handler myHandler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == PlayerMsg.PLAY_MSG) {
current_position = CurrentPosition();
intent_to_progressBar.putExtra("position",  current_position);
sendBroadcast(intent_to_progressBar);
myHandler.sendEmptyMessageDelayed(PlayerMsg.PLAY_MSG, 1000);

本文发布于:2024-09-22 07:08:29,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/2/277047.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:参数   设置   起来   按钮
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议