翻看谷歌源码 那些让人感兴趣的东西--通知的好用功能

转载请注明出处:王亟亟的大牛之路

上周写完那篇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
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值