您的位置:首页 > 移动开发 > Android开发

Android创建使用默认布局的通知

2015-11-24 19:50 966 查看
创建一个使用默认布局的通知:

[code]NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

Notification notification = new Notification(R.drawable.ic_launcher,"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);  // 用于指定通知布局显示内容

Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones/Basic_tone.ogg"));
notification.sound = sound; // 通知到来时铃声提醒

/**
 * 需要震动权限
 */
long[] vibrates = {0,1000,1000,1000};  // 0,2,4...表示静止时长,1,3,5...表示震动时长
notification.vibrate = vibrates;   // 设置震动提醒(这个数组的意思是,来了同时马上震动1秒,静止一秒,再震动一秒)

/**
 * 用于实现有未读通知时LED灯闪烁
 */
notification.ledARGB = Color.GREEN;  // 一般有红绿蓝可选
notification.ledOnMS = 1000;         // 指定LED灯亮起的时长,以毫秒为单位
notification.ledOffMS = 1000;        // 指定LED灯暗去的时长,以毫秒为单位
notification.flags = Notification.FLAG_SHOW_LIGHTS;   // flags用于指定通知一些行为,其中就有显示LED灯这一项

manager.notify(1,notification);  // 显示通知,每个通知的id(第一个参数)要唯一
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: