转载请注明出处:王亟亟的大牛之路
上周写完那篇Blog之后就一直做着被分配到的Web任务,也就没继续捯饬N那些事,然后今天还在看Notification这部分,然后看到了LNotification这个包,读完源码之后继续给大家做简单的分析,因为源码涉及翻墙所以代码和自己加的注释丢到Git了
安理下我的收纳库:https://github.com/ddwhan0123/Useful-Open-Source-Android
首先我们来看下这个项目在哪个目录下
地址如下:https://developer.android.com/samples/LNotifications/index.html
跟我们上次的Messaging Service是邻居。
那我们来看下演示效果(部分)
他在桌面icon也有 类似 待处理事件的通知
实现点击触发复杂事件以及通知排序
OK,因为是Sample所以好看难看我们不管,我们来看看分别能做什么,有3个功能块(都是Fragment)的,一个一个来分析
首先是HeadsUpNotificationFragment(这部分注释很详细了,就不过多分析了)
主要是实现这一点 用户点击通知会在Activity Task中生成一个新的实例
/**
* Fragment that demonstrates options for displaying Heads-Up Notifications.
* 这种模式下,用户点击通知会在Activity Task中生成一个新的实例
*/
public class HeadsUpNotificationFragment extends Fragment {
/**
* NotificationId用于从此Fragment的通知。
*/
private static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
/**
* 按钮用于点击显示通知
*/
private Button mShowNotificationButton;
/**
* 如果选中,该Fragment创建的通知将显示为Heads-Up通知。
*/
private CheckBox mUseHeadsUpCheckbox;
/**
* 使用此工厂方法来创建新实例
* 该Fragment使用提供的参数。
*
* @return 新的HeadsUpNotificationFragment的实例.
*/
public static HeadsUpNotificationFragment newInstance() {
HeadsUpNotificationFragment fragment = new HeadsUpNotificationFragment();
fragment.setRetainInstance(true);
return fragment;
}
public HeadsUpNotificationFragment() {
// 空构造
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//初始化NotificationManager
mNotificationManager = (NotificationManager) getActivity().getSystemService(Context
.NOTIFICATION_SERVICE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// 填充UI
return inflater.inflate(R.layout.fragment_heads_up_notification, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mShowNotificationButton = (Button) view.findViewById(R.id.show_notification_button);
//点击按钮就Toast一段话,告知用户已经触发点击事件
mShowNotificationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mNotificationManager.notify(NOTIFICATION_ID, createNotification(
mUseHeadsUpCheckbox.isChecked()));
Toast.makeText(getActivity(), "Show Notification clicked", Toast.LENGTH_SHORT).show();
}
});
mUseHeadsUpCheckbox = (CheckBox) view.findViewById(R.id.use_heads_up_checkbox);
}
/**
* 创建一个新的取决于参数的通知。
*
* @param makeHeadsUpNotification 一个布尔值判断是否通知将作为heads-up通知来创建。
* <ul>
* <li>true : heads-up通知</li>
* <li>false : 普通通知</li>
* </ul>
* @return 一个Notification 实例.
*/
//@VisibleForTesting
Notification createNotification(boolean makeHeadsUpNotification) {
//这里就是一些普通的配置
Notification.Builder notificationBuilder = new Notification.Builder(getActivity())
.setSmallIcon(R.drawable.ic_launcher_notification)
.setPriority(Notification.PRIORITY_DEFAULT)
.setCategory(Notification.CATEGORY_MESSAGE)
.setContentTitle("Sample Notification")
.setContentText("This is a normal notification.");
//如果是heads-up通知
if (makeHeadsUpNotification) {
Intent push = new Intent();
push.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
push.setClass(getActivity(), LNotificationActivity.class);
PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(getActivity(), 0,
push, PendingIntent.FLAG_CANCEL_CURRENT);
notificationBuilder
.setContentText("Heads-Up Notification on Android L or above.")
.setFullScreenIntent(full