notification :以另一种UI界面形式展示出来,点击时可以对其应用做处理。
其常用的一些基本属性:setSmallIcon()设置图标 、setContentTitle()设置题目、setContentText()设置内容、setAutoCancel()设置点击后是否自动销毁、setTicker()设置通知时的滚动文本
1、构造通知、跳转界面
// 构造通知界面 NotificationCompat.Builder builder = new NotificationCompat.Builder( NotifyActivity.this); builder.setSmallIcon(R.drawable.ic_launcher) .setContentTitle(getString(R.string.hello_world)) .setContentText(getString(R.string.hint_text)) .setAutoCancel(true) // 点击跳转后自动销毁 .setTicker(getString(R.string.hello_world)); // 设置滚动文本 // 通知行为(点击后能进入应用界面) Intent intent = new Intent(NotifyActivity.this, Marquee_Test.class); PendingIntent pendingIntent = PendingIntent.getActivity( NotifyActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); // 发送通知 NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.notify(1, builder.build()); // id不变则只显示一个,若id变化则可以重复显示多个
2、导航功能,返回应用首页
此时若点击返回时则直接跳出应用,若想其不退出应用还应在manifest中注册其父类路径(尽量使用4.1以上版本的模拟器,以下容易出错。)<activity android:name="com.example.ui.widget.Marquee_Test" <!-- 记住应在跳转界面下注册其父类 -->
android:parentActivityName="com.example.layout.LayoutAll"> <!-- 1、注册方式一支持4.1以上版本 -->
<meta-data
android:name=
"android.support.PARENT_ACTIVITY"<!-- 2、注册方式二 -->
android:value=
"com.example.layout.LayoutAll" />
此时只改变上方通知行为部分即可
<span style="white-space:pre"> </span>Intent intent = new Intent(NotifyActivity.this, Marquee_Test.class); //要跳转的界面 TaskStackBuilder taskStackBuilder = TaskStackBuilder .create(NotifyActivity.this); taskStackBuilder.addParentStack(Marquee_Test.class); //参数为跳转界面 taskStackBuilder.addNextIntent(intent); PendingIntent pendingIntent = taskStackBuilder .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent);3、BigView 系统按钮监听事件
此时也只用改变上方通知行为部分即可
<span style="white-space:pre"> </span>//播放部分
<span style="white-space:pre"> </span>Intent playIntent = new Intent(NotifyActivity.this, PlayMusicService.class); playIntent.setAction("1"); playIntent.putExtra( "PATH","file://"+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + "/wy.mp3"); playIntent.putExtra("FLAG", 1); PendingIntent playPending = PendingIntent.getService( NotifyActivity.this, 0, playIntent, PendingIntent.FLAG_UPDATE_CURRENT); <span style="white-space:pre"> </span>//暂停部分 Intent pauseIntent = new Intent(NotifyActivity.this, PlayMusicService.class); pauseIntent.putExtra("PATH","file://"+ Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+ "/wy.mp3"); //将播放文件传递到服务端 pauseIntent.setAction("2"); //此时必须设置action,不然不能播放,单个控件时可以不用设置,参数自定义 pauseIntent.putExtra("FLAG", 2); //判断播放、暂停标记 PendingIntent pausePending = PendingIntent.getService( NotifyActivity.this, 0, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.addAction(R.drawable.bg_actionbar, "播放", playPending); //系统添加按钮控件 builder.addAction(R.drawable.bg_actionbar, "暂停", pausePending);4、自定义通知界面
<span style="white-space:pre"> </span>NotificationCompat.Builder builder = new NotificationCompat.Builder( NotifyActivity.this); builder.setSmallIcon(R.drawable.ic_launcher); //此条件必须设置,不然不能显示 builder.setTicker("开始播放音乐"); RemoteViews view = new RemoteViews(getPackageName(), R.layout.notify_play_music_layout); //自定义布局 builder.setContent(view); Intent playIntent = new Intent(NotifyActivity.this, PlayMusicService.class); //启动服务 playIntent.setAction("1"); playIntent.putExtra( "PATH","file://"+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + "/wy.mp3"); playIntent.putExtra("FLAG", 1); PendingIntent playPending = PendingIntent.getService( NotifyActivity.this, 0, playIntent, PendingIntent.FLAG_UPDATE_CURRENT); view.setOnClickPendingIntent(R.id.music_play_img, playPending); //设置监听事件 NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.notify(4, builder.build());