Notification的几种用法(API不同)


        我们也知道Android系统也是在不断升级的,有关Notification的用法也就有很多种,有的方法已经被android抛弃了,现在我实现了三种不同的方法,并适应不同的android版本。现在我就把代码公布出来,我喜欢把解释写在代码中,在这里我就不多说了,先看效果图:


再看代码,主要的代码如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package net.loonggg.notification;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Notification;  
  5. import android.app.NotificationManager;  
  6. import android.app.PendingIntent;  
  7. import android.content.Context;  
  8. import android.content.Intent;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.widget.RemoteViews;  
  12.   
  13. public class MainActivity extends Activity {  
  14.     private static final int NOTIFICATION_FLAG = 1;  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.     }  
  21.   
  22.     public void notificationMethod(View view) {  
  23.         // 在Android进行通知处理,首先需要重系统哪里获得通知管理器NotificationManager,它是一个系统Service。  
  24.         NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
  25.         switch (view.getId()) {  
  26.         // 默认通知  
  27.         case R.id.btn1:  
  28.             // 创建一个PendingIntent,和Intent类似,不同的是由于不是马上调用,需要在下拉状态条出发的activity,所以采用的是PendingIntent,即点击Notification跳转启动到哪个Activity  
  29.             PendingIntent pendingIntent = PendingIntent.getActivity(this0,  
  30.                     new Intent(this, MainActivity.class), 0);  
  31.             // 下面需兼容Android 2.x版本是的处理方式  
  32.             // Notification notify1 = new Notification(R.drawable.message,  
  33.             // "TickerText:" + "您有新短消息,请注意查收!", System.currentTimeMillis());  
  34.             Notification notify1 = new Notification();  
  35.             notify1.icon = R.drawable.message;  
  36.             notify1.tickerText = "TickerText:您有新短消息,请注意查收!";  
  37.             notify1.when = System.currentTimeMillis();  
  38.             notify1.setLatestEventInfo(this"Notification Title",  
  39.                     "This is the notification message", pendingIntent);  
  40.             notify1.number = 1;  
  41.             notify1.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。  
  42.             // 通过通知管理器来发起通知。如果id不同,则每click,在statu那里增加一个提示  
  43.             manager.notify(NOTIFICATION_FLAG, notify1);  
  44.             break;  
  45.         // 默认通知 API11及之后可用  
  46.         case R.id.btn2:  
  47.             PendingIntent pendingIntent2 = PendingIntent.getActivity(this0,  
  48.                     new Intent(this, MainActivity.class), 0);  
  49.             // 通过Notification.Builder来创建通知,注意API Level  
  50.             // API11之后才支持  
  51.             Notification notify2 = new Notification.Builder(this)  
  52.                     .setSmallIcon(R.drawable.message) // 设置状态栏中的小图片,尺寸一般建议在24×24,这个图片同样也是在下拉状态栏中所显示,如果在那里需要更换更大的图片,可以使用setLargeIcon(Bitmap  
  53.                                                         // icon)  
  54.                     .setTicker("TickerText:" + "您有新短消息,请注意查收!")// 设置在status  
  55.                                                                 // bar上显示的提示文字  
  56.                     .setContentTitle("Notification Title")// 设置在下拉status  
  57.                                                             // bar后Activity,本例子中的NotififyMessage的TextView中显示的标题  
  58.                     .setContentText("This is the notification message")// TextView中显示的详细内容  
  59.                     .setContentIntent(pendingIntent2) // 关联PendingIntent  
  60.                     .setNumber(1// 在TextView的右方显示的数字,可放大图片看,在最右侧。这个number同时也起到一个序列号的左右,如果多个触发多个通知(同一ID),可以指定显示哪一个。  
  61.                     .getNotification(); // 需要注意build()是在API level  
  62.             // 16及之后增加的,在API11中可以使用getNotificatin()来代替  
  63.             notify2.flags |= Notification.FLAG_AUTO_CANCEL;  
  64.             manager.notify(NOTIFICATION_FLAG, notify2);  
  65.             break;  
  66.         // 默认通知 API16及之后可用  
  67.         case R.id.btn3:  
  68.             PendingIntent pendingIntent3 = PendingIntent.getActivity(this0,  
  69.                     new Intent(this, MainActivity.class), 0);  
  70.             // 通过Notification.Builder来创建通知,注意API Level  
  71.             // API16之后才支持  
  72.             Notification notify3 = new Notification.Builder(this)  
  73.                     .setSmallIcon(R.drawable.message)  
  74.                     .setTicker("TickerText:" + "您有新短消息,请注意查收!")  
  75.                     .setContentTitle("Notification Title")  
  76.                     .setContentText("This is the notification message")  
  77.                     .setContentIntent(pendingIntent3).setNumber(1).build(); // 需要注意build()是在API  
  78.                                                                             // level16及之后增加的,API11可以使用getNotificatin()来替代  
  79.             notify3.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。  
  80.             manager.notify(NOTIFICATION_FLAG, notify3);// 步骤4:通过通知管理器来发起通知。如果id不同,则每click,在status哪里增加一个提示  
  81.             break;  
  82.         // 自定义通知  
  83.         case R.id.btn4:  
  84.             // Notification myNotify = new Notification(R.drawable.message,  
  85.             // "自定义通知:您有新短信息了,请注意查收!", System.currentTimeMillis());  
  86.             Notification myNotify = new Notification();  
  87.             myNotify.icon = R.drawable.message;  
  88.             myNotify.tickerText = "TickerText:您有新短消息,请注意查收!";  
  89.             myNotify.when = System.currentTimeMillis();  
  90.             myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能够自动清除  
  91.             RemoteViews rv = new RemoteViews(getPackageName(),  
  92.                     R.layout.my_notification);  
  93.             rv.setTextViewText(R.id.text_content, "hello wrold!");  
  94.             myNotify.contentView = rv;  
  95.             Intent intent = new Intent(Intent.ACTION_MAIN);  
  96.             PendingIntent contentIntent = PendingIntent.getActivity(this1,  
  97.                     intent, 1);  
  98.             myNotify.contentIntent = contentIntent;  
  99.             manager.notify(NOTIFICATION_FLAG, myNotify);  
  100.             break;  
  101.         case R.id.btn5:  
  102.             // 清除id为NOTIFICATION_FLAG的通知  
  103.             manager.cancel(NOTIFICATION_FLAG);  
  104.             // 清除所有的通知  
  105.             // manager.cancelAll();  
  106.             break;  
  107.         default:  
  108.             break;  
  109.         }  
  110.     }  
  111. }  
再看主布局文件:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <Button  
  9.         android:id="@+id/btn1"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:onClick="notificationMethod"  
  13.         android:text="默认通知(已被抛弃,但是通用)" />  
  14.   
  15.     <Button  
  16.         android:id="@+id/btn2"  
  17.         android:layout_width="fill_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:onClick="notificationMethod"  
  20.         android:text="默认通知(API11之后可用)" />  
  21.   
  22.     <Button  
  23.         android:id="@+id/btn3"  
  24.         android:layout_width="fill_parent"  
  25.         android:layout_height="wrap_content"  
  26.         android:onClick="notificationMethod"  
  27.         android:text="默认通知(API16之后可用)" />  
  28.   
  29.     <Button  
  30.         android:id="@+id/btn4"  
  31.         android:layout_width="fill_parent"  
  32.         android:layout_height="wrap_content"  
  33.         android:onClick="notificationMethod"  
  34.         android:text="自定义通知" />  
  35.   
  36.     <Button  
  37.         android:id="@+id/btn5"  
  38.         android:layout_width="fill_parent"  
  39.         android:layout_height="wrap_content"  
  40.         android:onClick="notificationMethod"  
  41.         android:text="清除通知" />  
  42.   
  43. </LinearLayout>  

还有一个是:自定义通知的布局文件my_notification.xml,代码如下:


[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="#ffffff"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:id="@+id/text_content"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:textSize="20sp" />  
  13.   
  14. </LinearLayout>  

来自:http://blog.csdn.net/loongggdroid/article/details/17616509

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NotificationAndroid 操作系统中的一个重要组件,可用于向用户发送各种提示信息。在 Android 应用程序中使用 Notification 的步骤如下: 1. 创建 Notification 对象 你可以通过 Notification.Builder 或者 NotificationCompat.Builder 对象来创建 Notification 对象,设置通知的标题、内容、图标等参数。 2. 发送 Notification 使用 NotificationManager 对象的 notify() 方法来发送 Notification。 3. 处理 Notification 的点击事件 如果用户点击了 Notification,你可以通过 PendingIntent 对象来启动一个 Activity 或者执行一个操作。 以下是一个简单的示例代码,用于创建和发送一个 Notification: ```java // 创建 Notification.Builder 对象 Notification.Builder builder = new Notification.Builder(this); builder.setContentTitle("标题"); builder.setContentText("内容"); builder.setSmallIcon(R.drawable.icon); // 发送 Notification NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, builder.build()); // 处理 Notification 的点击事件 Intent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); ``` 这样就可以发送一个简单的 Notification,并且处理用户的点击事件。需要注意的是,为了让应用程序发送 Notification,你需要在 AndroidManifest.xml 文件中添加相应的权限:`<uses-permission android:name="android.permission.VIBRATE" />` 和 `<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值