日拱一卒(十三)

Android之Notification:

(1)要发送一个Notification需要一个NotificationManager:

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

(2)初始化Notification:

Notification notification = new Notification(R.drawable.icon, "This is ticker text",System.currentTimeMillis());
Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(this, "This is content title", "This is content text", pi);
manager.notify(1, notification);

注:

   1.NotificationsetLatestEventInfo()

第一个参数:上下文;

第二个参数:通知标题内容

第三个参数:通知的正文

第四个参数:PendingIntent,用于相应通知点击跳转。

    2.PendingIntent用法:

Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);

可以根据需求来选择是使用getActivity()方法、getBroadcast()方法、还是getService()方法。

   3.NotificationManagernotify():

第一个参数:id,要保证为每个通知所指定的id都是不同的

第二个参数:Notification对象

(3)Notification的取消(消除提示):

NotificationManager manager = (NotificationManager) 
getSystemService(NOTIFICATION_SERVICE);
manager.cancel(1);
NotificationManager中cancel中的参数就是上面定义的Notification的id;


(4)Notification高级编程:

1.指定声音:

Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones/ Basic_tone.ogg"));
notification.sound = soundUri;

2.设定震动:

long[] vibrates = {0, 1000, 1000, 1000};
notification.vibrate = vibrates;
vibrates中参数:

第一个:手机静止的时长

第二个:手机震动时长

第三个:又表示手机静止的时长

...以此类推

手机震动需要权限:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.notificationtest"
    android:versionCode="1"
    android:versionName="1.0" >
	……
    <uses-permission android:name="android.permission.VIBRATE" />
	……
</manifest>

3.设置通知Led:

比如实现LED灯以绿色灯光一闪一闪效果:

notification.ledARGB = Color.GREEN;
notification.ledOnMS = 1000;
notification.ledOffMS = 1000;
notification.flags = Notification.FLAG_SHOW_LIGHTS;

     当然可以设置按照默认的方式显示通知:

notification.defaults = Notification.DEFAULT_ALL;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值