notification⼏种点击事件
notification :以另⼀种UI界⾯形式展⽰出来,点击时可以对其应⽤做处理。
pgl
其常⽤的⼀些基本属性:tSmallIcon()设置图标 、tContentTitle()设置题⽬、tContentText()设置内容、tAutoCancel()设置点击后是否⾃动销毁、tTicker()设置通知时的滚动⽂本
1、构造通知、跳转界⾯
// 构造通知界⾯
NotificationCompat.Builder builder = new NotificationCompat.Builder(
加油英文NotifyActivity.this);
builder.tSmallIcon(R.drawable.ic_launcher)
.tContentTitle(getString(R.string.hello_world))
缺欠
.tContentText(getString(R.string.hint_text))
.tAutoCancel(true) // 点击跳转后⾃动销毁
.tTicker(getString(R.string.hello_world)); // 设置滚动⽂本
// 通知⾏为(点击后能进⼊应⽤界⾯)
Intent intent = new Intent(NotifyActivity.this,
Marquee_Test.class);
PendingIntent pendingIntent = Activity(
NotifyActivity.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.tContentIntent(pendingIntent);
// 发送通知
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
2、导航功能,返回应⽤⾸页
此时若点击返回时则直接跳出应⽤,若想其不退出应⽤还应在manifest中注册其⽗类路径(尽量使⽤4.1以上版本的模拟器,以下容易出错。)
daffy<activity android:name="ample.ui.widget.Marquee_Test" <!-- 记住应在跳转界⾯下注册其⽗类 -->
android:parentActivityName="ample.layout.LayoutAll"> <!-- 1、注册⽅式⼀⽀持4.1以上版本 -->
sounds是什么意思<meta-data android:name= "android.support.PARENT_ACTIVITY"<!-- 2、注册⽅式⼆ -->
android:value="ample.layout.LayoutAll" />
</activity>
此时只改变上⽅通知⾏为部分即可
<span > </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.tContentIntent(pendingIntent);
3、BigView 系统按钮监听事件
此时也只⽤改变上⽅通知⾏为部分即可
<span > </span>//播放部分
<span > </span>Intent playIntent = new Intent(NotifyActivity.this,
PlayMusicService.class);
playIntent.tAction("1");
playIntent.putExtra(
"PATH","file://"+ ExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)
+ "/wy.mp3");
playIntent.putExtra("FLAG", 1);
小码王PendingIntent playPending = Service(
NotifyActivity.this, 0, playIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
<span > </span>//暂停部分
Intent pauIntent = new Intent(NotifyActivity.this,
PlayMusicService.class);
pauIntent.putExtra("PATH","file://"+ Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+ "/wy.mp3"); //将播放⽂件传递到服务端 pauIntent.tAction("2"); //此时必须设置action,不然不能播放,单个控件时可以不⽤设置,参数⾃定义
pauIntent.putExtra("FLAG", 2); //判断播放、暂停标记
PendingIntent pauPending = Service(
NotifyActivity.this, 0, pauIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(R.drawable.bg_actionbar, "播放", playPending); //系统添加按钮控件
builder.addAction(R.drawable.bg_actionbar, "暂停", pauPending);
4、⾃定义通知界⾯
<span > </span>NotificationCompat.Builder builder = new NotificationCompat.Builder(
NotifyActivity.this);
builder.tSmallIcon(R.drawable.ic_launcher); //此条件必须设置,不然不能显⽰
builder.tTicker("开始播放⾳乐");
RemoteViews view = new RemoteViews(getPackageName(), ify_play_music_layout); //⾃定义布局
commodorebuilder.tContent(view);
Intent playIntent = new Intent(NotifyActivity.this,
PlayMusicService.class); //启动服务
playIntent.tAction("1");
职称英语论坛
playIntent.putExtra(
ideo
"PATH","file://"+ ExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)
+ "/wy.mp3");
playIntent.putExtra("FLAG", 1);
PendingIntent playPending = Service(
NotifyActivity.this, 0, playIntent,parasite
PendingIntent.FLAG_UPDATE_CURRENT);
view.tOnClickPendingIntent(R.id.music_play_img, playPending); //设置监听事件
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);