Android中通知(Notification)的简单使用
2014-10-11 15:00
120 查看
现在的很多应用中都会存在通知的使用,例如,新消息的提醒、广告推送、新闻消息等。下面通过一个实例来熟悉一下通知的使用。
1、布局文件
源码下载
使用上面代码就基本能够实现的通知要求,如果你的需求更加的繁琐,可以去看看下面的链接。
详细内容参考地址:
/article/3516899.html
http://blog.csdn.net/vipzjyno1/article/details/25248021 http://www.bkjia.com/Androidjc/924831.html
1、布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="显示通知" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/button1" android:text="取消通知" /> </RelativeLayout>2、通知使用实现
package com.cx.notificatest; import android.annotation.TargetApi; import android.app.Activity; import android.app.Notification; import android.app.Notification.Builder; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener{ private NotificationManager manager; private int notification_id; //对应的Notification的id private Button button1; private Button button2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button1.setOnClickListener(this); button2.setOnClickListener(this); //通知控制这类,用于显示通知,NotificationManager是系统常用服务 manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.button1: sendNotification(); break; case R.id.button2: manager.cancel(notification_id); break; } } /** * 构造Notification并发送到通知栏 */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void sendNotification(){ //用于点击跳转 ,点击通知跳转到本页 Intent openintent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, openintent, 0); Builder builder = new Notification.Builder(this); builder.setSmallIcon(R.drawable.ic_launcher);//设置图标 builder.setTicker("新消息");//手机状态栏提示 builder.setWhen(System.currentTimeMillis());//设置时间 builder.setContentTitle("通知标题");//设置标题 builder.setContentText("通知内容");//设置通知内容 builder.setContentIntent(pendingIntent);//设置点击后的意图 // builder.setDefaults(Notification.DEFAULT_SOUND);//设置提示声音 builder.setDefaults(Notification.DEFAULT_LIGHTS);//设置提示指示灯,需要权限 builder.setDefaults(Notification.DEFAULT_VIBRATE);//设置震动,需要权限 // builder.setDefaults(Notification.DEFAULT_ALL);//上面三个全设置 builder.setOnlyAlertOnce(true);//更新信息不再重新弹出 builder.setAutoCancel(true);//单击后自动取消 builder.setProgress(100, 50, false);//进度条 builder.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.xxx));//自定义提示音 Notification notification = builder.build();//Android4.1以上使用 // Notification notification = builder.getNotification();//Android4.1以下使用 notification.icon = R.drawable.notice; //设置弹出图片 notification.flags = Notification.FLAG_NO_CLEAR; //设置不可取消 /** * 同时上面的某些属性也可以在此处设置 */ // notification.defaults=Notification.DEFAULT_SOUND; //添加系统声音 // notification.sound=Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.xxx);//添加自定义声音 //显示通知 manager.notify(notification_id, notification); } }3、添加指示灯和震动权限
<!-- 指示灯权限 --> <uses-permission android:name="android.permission.FLASHLIGHT"/> <!-- 震动权限 --> <uses-permission android:name="android.permission.VIBRATE"/>
源码下载
使用上面代码就基本能够实现的通知要求,如果你的需求更加的繁琐,可以去看看下面的链接。
详细内容参考地址:
/article/3516899.html
http://blog.csdn.net/vipzjyno1/article/details/25248021 http://www.bkjia.com/Androidjc/924831.html
相关文章推荐
- Android---60---Notification 通知栏的简单使用
- Android---60---Notification 通知栏的简单使用
- Android 通知栏 NotificationManager和Notification的使用总结
- Android Notification 使用详解之三:通过服务更新进度通知&在Activity中监听服务进度
- Android中通知的使用-----Notification详解
- Android的通知(Notification)使用详解
- android Notification(通知) 的使用
- Android 音乐播放器的开发教程(十)通知栏Notification的使用 ----- 小达
- Android NotificationManager与Notification(通知栏) 的使用
- Android--使用Notification在通知栏显示消息
- Android Notification使用系统通知栏布局出现的图标问题
- Android Notification 通知的使用
- android Notification 的使用(锁定通知栏)
- Android状态栏通知Notification使用小结
- Android Notification 使用详解之二:可更新进度的通知
- android通知的使用——Notification
- android通知(Notification)的使用
- Android中通知的使用-----Notification详解
- 【Android】使用广播BroadcastReceiver拦截通知Notification
- 状态栏通知Notification的简单使用