通知栏的使用

需要弄清楚的点

  • Remoteview作用,使用方法
  • PendingIntent和Intent的区别
  • Notication和广播BroadCastReceiver组件差异

1 目前掌握的是RmeoteView在自定义通知的时候用来设置自定义通知的布局资源。

2 Intent即时启动,随着所在Activity的消失而消失。Intent一般用作在Activity,Service,BroadCastReceiver之间传递数据。
PendingIntent是Intent的包装,通常通过getActivity,getService,getBroadCast来实例化。当前Activity不能发生启动它所包含的Intent,只有在外部执行PendingIntent时调用Intent。PendingIntent拥有当前App的Context,既是当前App不存在了,也能通过其包含的Context执行Intent。

3 BroadCastReceiver组件没有提供可视化界面显示广播消息,而Noticification和Notication Manager的组合可显示消息的内容及图标、震动等信息到状态栏。

实现步骤:

1 获得服务

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

2 实例化Notification

   Notification n = new Notification(R.drawable.new_app_icon, title, when);

when值为要显示的时间,如果不想显示时间可以直接设置0

单独设置进行实例化

Notification n = new Notification();

    n.icon = R.drawable.ic_launcher;
    n.tickerText = "通知";
    //不设置没有时间显示
    n.when = System.currentTimeMillis();
     n.vibrate = new long[] { 5000, 2000, 1000, 10000 };

vibrate参数:延时,震动,暂停,震动

    Uri url = Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"iloveu.mp3");

    n.sound = url;

设置自定义的声音

flag设置使其不能被删除掉,只有点击才能从通知栏删除

n.flags = Notification.FLAG_SHOW_LIGHTS|Notification.FLAG_NO_CLEAR;

通知一直进行,如果是音乐那么一直播放一直到用户响应

notification.flags |= Notification.FLAG_INSISTENT;

3 设置事件消息

PendingIntent mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,MainActivity.class), 0);
    //设置事件信息
    n.setLatestEventInfo(this, null, "内容", mPendingIntent);

setLastEventInfo参数类型:context,title,content,pendingintent

4 发出通知

mNotificationManager.notify();

直接用,报错java.lang.IllegalMonitorStateException: object not locked by thread before notify()

发出id为1的通知n,id为了区分多个通知,单独发出

    mNotificationManager.notify(1,n);

5 取消通知

    //取消指定id=2的通知
    mNotificationManager.cancel(2);

#

拓展实现类似播放器控制播放上一首,下一首

设置RemoteViews布局为播放控制面板,监听播放控制事件,事件发生后发送广播

RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notice);
        Intent frontSong = new Intent("frontSong");
        Intent nextSong = new Intent("nextSong");
        PendingIntent leftmenuIntent = PendingIntent.getBroadcast(this, 1, frontSong, 1);
        PendingIntent rightmenuIntent = PendingIntent.getBroadcast(this, 2, nextSong, 2);
        remoteViews.setOnClickPendingIntent(R.id.left_menu, leftmenuIntent);
        remoteViews.setOnClickPendingIntent(R.id.right_menu, rightmenuIntent);
        n.contentView = remoteViews;

广播接收器

public class PlayReciver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub
        //接到广播,播放下一条或上一条
        if(arg1.getAction()=="nextSong"){
            System.out.println("____nextSong");
        }
        if(arg1.getAction()=="frontSong"){
            System.out.println("____frontSong");
        }
    }

}

不要忘记加注册还有拦截action的声明

  <receiver android:name=".PlayReciver" >
<intent-filter>
<action android:name="frontSong" />
<action android:name="nextSong" >
</action>
</intent-filter>
</receiver>

需要注意的地方

6/3/2015 4:42:04 PM

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值