安卓通知的使用系列2:状态栏通知和自定义状态栏通知通知
2016-02-29 21:27
405 查看
状态栏通知是android开发中常见的一种通知形式,下面我们来介绍一下它的使用方法。
整体思路:在xml文件中放置两个button控件,分别设置它们的点击事件,声明NotificationManager对象和Notification.Builder对象,在onCreate方法中实例化这两个对象,在第一个点击事件中设置通知的各个属性并发送通知;在第二个点击事件中绑定一个自定义的xml'文件并发送通知。
activity_main.xml文件:
整体思路:在xml文件中放置两个button控件,分别设置它们的点击事件,声明NotificationManager对象和Notification.Builder对象,在onCreate方法中实例化这两个对象,在第一个点击事件中设置通知的各个属性并发送通知;在第二个点击事件中绑定一个自定义的xml'文件并发送通知。
activity_main.xml文件:
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="135dp" android:text="普通的通知使用" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button1" android:layout_centerHorizontal="true" android:layout_marginTop="47dp" android:text="自定义通知的使用" />custom_notification.xml文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:layout_marginRight="100dp" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/image" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/image" android:layout_below="@+id/title" /> </RelativeLayout>MainActivity.java文件:
public class MainActivity extends Activity { private Button button, button2; private NotificationManager manager; private Notification.Builder builder; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); // 创建一个通知管理类 manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); builder = new Notification.Builder(this); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this, MainActivity.class); // 第二个参数表示请求的状态码, PendingIntent contentIntent = PendingIntent.getActivity( MainActivity.this, 0, intent, 0); builder.setContentIntent(contentIntent); builder.setContentTitle("new notification is coming"); builder.setContentText("hello world"); builder.setTicker("有通知来了");// 第一次出现在状态栏的内容 builder.setSmallIcon(R.drawable.ic_launcher); // 通知的形式:闪光灯、声音、震动、自己设置的声音 // 也可以定义震动的形式,在api中都有介绍 // Notification.DEFAULT_ALL表示所有的提示都是默认的 // DEFAULT_LIGHTS默认的闪光,DEFAULT_SOUND默认的声音,DEFAULT_VIBRATE默认的震动 builder.setDefaults(Notification.DEFAULT_ALL); // Uri uri=Uri.parse("file:///sdcard/notification/ringer.mp3"); // builder.setSound(uri);//提示用户可以自定义设置铃声 Notification notification = builder.build();// 仅仅限于在高版本4.1以上使用 // notification.defaults=Notification.DEFAULT_SOUND; //在低版本中使用 // 第一个参数表示通知的唯一标示符 manager.notify(1000, notification); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub RemoteViews contentViews = new RemoteViews(getPackageName(), R.layout.custom_notification); contentViews.setImageViewResource(R.id.image, R.drawable.ic_launcher); contentViews.setTextViewText(R.id.title, "自定义通知的标题"); contentViews.setTextViewText(R.id.text, "自定义通知的内容"); Intent intent = new Intent(MainActivity.this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity( MainActivity.this, 0, intent, 0); builder.setContentIntent(pendingIntent); builder.setContent(contentViews);//指定自定义布局 Notification notification=builder.build(); manager.notify(10001,notification); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
相关文章推荐
- R语言学习:数据结构7-数据框
- 我想当老师
- SQL Server中smalldatetime的日期范围为何是[1900-01-01,2079-06-06]
- cocos2d-x之事件传递
- 【quickSort】
- 2015年终总结-青春如歌
- PHP之有关类和对象的系统函数与运算符
- CSDN资源上传记录(持续更新)
- MODEL
- 160229-02、Sublime Text 3 快捷键总结
- 6.0字符串String
- 堆排序(C++版)
- Compare Version Number
- 关于我和Github不得不说的一些小事
- 数据切分——Atlas读写分离Mysql集群的搭建
- kaerBdroW.139
- 编写用户侧定时器
- 如何查找openCV函数源代码
- linux学习之c注释
- 字符数组和字符指针