《安卓教程:手机影音自定义通知栏》由会员分享,可在线阅读,更多相关《安卓教程:手机影音自定义通知栏(23页珍藏版)》请在金锄头文库上搜索。
1、手机影音手机影音自定义通知栏自定义通知栏1.显示普通通知栏。新建一个 module,在界面上写两个按钮,一个显示通知,一个隐藏通知。【文件文件 3-10】3-10】MainActivity.java1.package com.itheima.notificationdemo;2.import android.app.Activity;3.import android.app.Notification;4.import android.app.NotificationManager;5.import android.app.PendingIntent;6.import android.conte
2、nt.Intent;7.import android.os.Bundle;8.import android.view.Menu;9.import android.view.MenuItem;10. import android.view.View;11. import android.widget.Toast;12. public class MainActivity extends Activity 13.Override2814.protected void onCreate(Bundle savedInstanceState) 15.super.onCreate(savedInstanc
3、eState);16.setContentView(R.layout.activity_main);17.String msg=getIntent().getStringExtra(“msg“);18.if (msg!=null)19. Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();20.21.22.public void show(View view)23.showNotification();24.25./* 显示通知栏*/26.private void showNotification() 27.Not
4、ification notification=new Notification(R.drawable.icon,“正在播放:28.dida“,System.currentTimeMillis();29.notification.setLatestEventInfo(this, “滴答“, “侃侃“, getPendingIntent();30./不允许删除通知栏31.notification.flags=Notification.FLAG_ONGOING_EVENT;32.NotificationManager notificationManger= (NotificationManager)
5、33.getSystemService(NOTIFICATION_SERVICE);34.notificationManger.notify(0,notification);35.36.private PendingIntent getPendingIntent() 37.Intent intent=new Intent(this,MainActivity.class);38.intent.putExtra(“msg“,“notification 发出的通知“);39.return PendingIntent.getActivity(this,0,40.intent,PendingIntent
6、.FLAG_UPDATE_CURRENT);41.42.public void cancel(View view)43.cancelNotification();44.45./* 取消通知栏*/46.private void cancelNotification() 47.NotificationManager notificationManger= (NotificationManager)48.getSystemService(NOTIFICATION_SERVICE);49.notificationManger.cancel(0);50.51. 运行结果:292.使用新的 API 显示通
7、知栏。1./* 使用新的 api*/2.private Notification getNewNotification() 3.Notification.Builder notification=new Notification.Builder(this);4.notification.setSmallIcon(R.drawable.icon);5.notification.setTicker(“正在播放:beijingbeijing“);6.notification.setOngoing(true);7.notification.setContentTitle(“北京北京“);8.notif
8、ication.setContentText(“汪峰“);9.notification.setWhen(System.currentTimeMillis();10.notification.setContentIntent(getPendingIntent();11.return notification.getNotification();12. 运行结果:303.自定义通知栏。写一个通知栏的布局,分别填充上面的文字和点击事件。【文件文件 3-11】3-11】notification.xml1.8.12.17.24.32.33.40.45. 自定义通知栏代码的实现。1./* 使用新的 API
9、 自定义一个通知*/2.private Notification getCustomNotification() 3.Notification.Builder notification=new Notification.Builder(this);4.notification.setSmallIcon(R.drawable.icon);5.notification.setTicker(“正在播放:我相信“);6.notification.setOngoing(true);7./notification.setWhen(System.currentTimeMillis();8./notifica
10、tion.setContentTitle(“北京北京“);9./notification.setContentText(“汪峰“);10. /notification.setContentIntent(getPendingIntent();、11.notification.setContent(getRemoteView();12.return notification.getNotification();13.14./* 自定义通知栏显示的布局*/15.private RemoteViews getRemoteView() 16.RemoteViews remoteViews=new17.R
11、emoteViews(getPackageName(),R.layout.notification);18./填充文字19.remoteViews.setTextViewText(R.id.audioplay_tv_notification_name,“我相信“);20.remoteViews.setTextViewText(R.id.audioplay_tv_notification_artist,“unknow“);21./设置点击事件22.remoteViews.setOnClickPendingIntent(R.id.audioplay_ll_notification,23.getPe
12、ndingIntent();24.remoteViews.setOnClickPendingIntent(R.id.audioplay_iv_notification_pre,3225.getPrePendingIntent();26.remoteViews.setOnClickPendingIntent(R.id.audioplay_iv_notification_next,27.getNextPendingIntent();28.return remoteViews;29.30./* 下一首*/31.private PendingIntent getNextPendingIntent()
13、32.Intent intent=new Intent(this,MainActivity.class);33.intent.putExtra(“msg“, “下一首“);34.return PendingIntent.getActivity(this,2,35.intent,PendingIntent.FLAG_UPDATE_CURRENT);36.37./* 上一首*/38.private PendingIntent getPrePendingIntent() 39.Intent intent=new Intent(this,MainActivity.class);40.intent.pu
14、tExtra(“msg“, “上一首“);41./注意有多个 PendingIntent 的时候第二个参数不能一样42.return PendingIntent.getActivity(this,1,43.intent,PendingIntent.FLAG_UPDATE_CURRENT);44.45. private PendingIntent getPendingIntent() 46.Intent intent=new Intent(this,MainActivity.class);47.intent.putExtra(“msg“, “notification 发出的通知“);48.ret
15、urn PendingIntent.getActivity(this,0, intent,PendingIntent.FLAG_UPDATE_CURRENT);49. 运行结果:4.将自定义的通知集成到项目中去。将布局文件和代码直接拷贝到项目中,代码需要放在服务中。最后将歌名和歌手名的数据初始化一下。331./* 音乐暂停*/2.public void pause()3.mediaPlayer.pause();4.cancelNotification();5.6./* 音乐开始*/7.public void start()8.mediaPlayer.start();9.showNotifica
16、tion();10. 11. private class OnAudioPreparedListener implements MediaPlayer.OnPreparedListener 12.Override13.public void onPrepared(MediaPlayer mediaPlayer) 14./准备完成,开始播放15.mediaPlayer.start();16./服务发广播来更新界面的图片17.Intent intent=new Intent(“com.itheima.mediaplay.updateplaybtn“);18./获取当前正在播放的音乐19.AudioItem audioItem=audioItems.get(position);20.intent.putExtra(“audioItem“,audioItem);21.sendBroadcast(intent);22./显示通知23.showNotif