- Notification的使用:
Notification开发主要涉及3个类:
1.Notification.Builder:用于动态的设置Notification 的一些属性。
其他方法:
设置属性 | 说明 |
setAutoCancel(boolean autocancel) | 设置点击信息后自动清除通知 |
setContent(RemoteView view) | 设置自定义通知 |
setContentTitle(String string) | 设置标题 |
setContentText(String string) | 设置内容 |
SetContentIntent(PendingIntent intent) | 设置点击信息后的跳转(意图) |
setWhen(long when) | 设置时间 |
setPriority(int pri) | 设置通知的重要程度 |
setStyle(Style style) | 设置样式 |
setVisibility(int visibility) | 设置锁屏显示 |
setDefault(int defaults) | 设置默认 |
setLight(int argb, int onMs, int offMs) | 设置呼吸灯闪烁效果 |
setSound(Uri sound) | 设置通知音效 |
setVibrate(long [] pattern) | 设置震动效果 |
setCategory(String category) | 设置通知类别 |
setColor(int argb) | 设置通知栏颜色 |
setFullScreenIntent(PendingIntent intent,boolean b) | 设置弹窗显示 |
2.NotificationManager:负责将Notification 在状态显示出来和取消;
3.Notification:设置Notification 的相关属性。
4.点击notification,就可以跳转到我们intent 中指定的activity。主要使用到setContentIntent 与PendingIntent(是比intent更加先进的一个类,它不同于intent,它必须满足一定条件下,才会触发放在里面的intent操作。可以在程序外部执行。即使程序已经退出了)
- 静态广播:
1.注册静态广播接收器
<receiver android:name=".MyReceiver"
android:exported="true">
<intent-filter>
<action android:name="MyStaticFilter"/>
</intent-filter>
</receiver>
2.重写onReceive方法
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//获取通知栏管理
NotificationManager notificationManager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
//实例化通知栏实例
Notification.Builder builder=new Notification.Builder(context);
//配置builder
Bundle extras=intent.getExtras();
builder.setContentText("新商品热卖").setContentText(extras.getString("infoName")+"仅售"+extras.getString("infoPrice")+"!")
.setTicker("新商品热卖").setPriority(Notification.PRIORITY_DEFAULT)
.setSmallIcon(extras.getInt("infoImage"))
.setContentTitle("新商品热卖")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true);
//绑定intent,点击图标进入商品详情activity
Intent mIntent=new Intent(context,GoodsInfoActivity.class);
mIntent.putExtras(extras);
PendingIntent mPendingIntent=PendingIntent.getActivity(context,0,mIntent,PendingIntent.FLAG_UPDATE_CURRENT);//注意这里
builder.setContentIntent(mPendingIntent);
//绑定Notification,发送通知请求
Notification notify=builder.build();
notificationManager.notify(0,notify);
}
}
3.发送广播
Intent intentBroadcast=new Intent("MyStaticFilter");
Bundle bundle=new Bundle();
intentBroadcast.putExtras(bundle);
sendBroadcast(intentBroadcast);
- 动态广播:
与静态广播不同的地方是动态注册广播而不是静态注册。
1.广播接收器,和静态广播差不多
public class DynamicReceiver extends BroadcastReceiver {
private static final String DYNAMICACTION="com.example.lab4.MyDynamicFilter";
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(DYNAMICACTION)) {
Bundle extras=intent.getExtras();
//获取通知栏管理
NotificationManager notificationManager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
//实例化通知栏实例
Notification.Builder builder=new Notification.Builder(context);
//配置builder
builder.setContentText("马上下单").setContentText(extras.getString("infoName")+"已添加到购物车")
.setTicker("马上下单").setPriority(Notification.PRIORITY_DEFAULT)
.setSmallIcon(extras.getInt("infoImage"))
.setContentTitle("马上下单")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true);
//绑定intent,点击图标进入商品详情activity
Intent mIntent=new Intent(context,MainActivity.class);
PendingIntent mPendingIntent=PendingIntent.getActivity(context,0,mIntent,PendingIntent.FLAG_UPDATE_CURRENT);//注意这里
builder.setContentIntent(mPendingIntent);
//绑定Notification,发送通知请求
Notification notify=builder.build();
notificationManager.notify(0,notify);
}
}
}
2.注册广播
DynamicReceiver dynamicReceiver=new DynamicReceiver();
IntentFilter dynamic_filter=new IntentFilter();
dynamic_filter.addAction(DYNAMICACTION);
registerReceiver(dynamicReceiver,dynamic_filter);
3.注销广播
unregisterReceiver(dynamicReceiver)
4.发送方法与静态的差不多
Intent intentBroadcast=new Intent(DYNAMICACTION);
ps:DYNAMICACTION是约定好的一个识别字符串(练习1与4中的DYNAMICACTION)
5.注意在 Android 主界面中将 launchMode 设置为 singleInstance,使得点击Notification 后不会另外新建一个activity
- EventBus的使用
参考:https://www.jianshu.com/p/a040955194fc
利用所学可以实现一个项目:
https://github.com/KingsonKai/AndroidProject/tree/master/Lab4