Android学习之路(一)通知Notification

这是调用通知的函数

 private void showNotification(PlayWarper mPlayWarper) {
        mNotificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
        intent = new Intent(mContext, FileListLocalActivity.class);

        pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        RemoteViews rv = new RemoteViews(getPackageName(), R.layout.notice);
        String name = mPlayWarper.musicInfo.getName();
  mNotification = new Notification(android.R.drawable.ic_media_play, name, System.currentTimeMillis());

        mNotification.contentIntent = pendingIntent;
        mNotification.contentView = rv;
        // mNotification.setLatestEventInfo(this, "正在播放:" + name, name,
        // pendingIntent);
         mNotification.flags |= Notification.FLAG_ONGOING_EVENT; //
        // 将此通知放到通知栏的"Ongoing"即"正在运行"组中
        mNotification.flags |= Notification.FLAG_NO_CLEAR; //
        mNotification.flags |= Intent.FLAG_ACTIVITY_SINGLE_TOP;

        // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用
         mNotificationManager.notify(Constant.NOTICE_ID, mNotification);
    }

 

 

开发Notification遇到问题

 

1,点击通知无Activity返回,如同没有点击(有些人倒想实现这样的效果)

这个问题很二,哈哈,原因就是AndroidManifest.xml没有定义NoticeActivity.

 这行代码决定点击通知后跳转到哪个Activity

intent = new Intent(mContext, FileListLocalActivity.class);

 

2,点击通知后有Activity了,但是再次点击无任何操作.

这个原因是创建PendingIntent时最后一个参数flag定义不正确,应该是

PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
FLAG_ONE_SHOT是只能点击通知一次

FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT,

 

3,通知内容不是定义的layout,而是文字信息

 // mNotification.setLatestEventInfo(this, "正在播放:" + name, name,
        // pendingIntent);
注释掉这样的代码就可以显示R.layout.notice的layout到通知里面.

 

4,点击通知后每次都新建一个Activity,不能把后台的Activity直接显示到前台

 解决方法是修改点击后出现的Activity在AndroidManifest.xml中加上launchMode,代码如下:

     <activity
            android:name=".FileListLocalActivity"
            android:launchMode="singleTask" />

 

launchMode的解释相见SDK文档http://developer.android.com/reference/android/R.attr.html#launchMode

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值