星空网 > 软件开发 > 操作系统

安卓中通知(Notification)的基本使用方法

1. 通知的使用场合

  当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现。发出一条通知后,手机最上方的状态栏中会显示一个通知的图标,下拉状态栏后可以看到通知的详细内容。
2. 通知的创建步骤
  (1)获取NotificationManager实例,可以通过调用Conten的getSystenService()方法得到,getSystemService()方法接收一个字符串参数用于确定获取系统的哪个服务, 这里我们传入Context.NOTIFICATION_SERVICE 即可。获取NotificationManager实例如下:

  NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

  (2)创建Notification对象,该对象用于存储通知的各种所需信息,我们可以使用它的有参构造函数来创建。构造函数有三个参数,第一个参数指定通知图标,第二个参数用于指定通知的ticker 内容,当通知刚被创建的时候,它会在系统的状态栏一闪而过,属于一种瞬时的提示信息。第三个参数用于指定通知被创建的时间,以毫秒为单位,当下拉系统状态栏时,这里指定的时间会显示在相应的通知上。创建一个Notification 对象可以写成:

  Notification notification = new Notification(R.drawable.ic_launcher,"This is a ticker text",System.currentTimeMillis());

  (3)调用Notification的setLatestEventIfo()方法对通知的布局进行设定,这个方法接收四个参数,第一个参数是Context。第二个参数用于指定通知的标题内容,下拉系统状态栏就可以看到这部分内容。第三个参数用于指定通知的正文内容,同样下拉系统状态栏就可以看到这部分内容。第四个参数用于指定实现通知点击事件的PendingIntent对象,如果暂时用不到可以先传入null。因此,对通知的布局进行设定就可以写成:

  notification.setLatestEventInfo(context, "This is content title", "This iscontent text", null);
  (4)调用NotificationManager的notify()方法显示通知。notify()方法接收两个参数,第一个参数是id,要保证为每个通知所指定的id 都是不同的。第二个参数则是Notification 对象,这里直接将我们刚刚创建好的Notification 对象传入即可。显示一个通知就可以写成:

  manager.notify(1, notification);
3.代码示例

安卓中通知(Notification)的基本使用方法images/loading.gif' data-original="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" />安卓中通知(Notification)的基本使用方法
<LinearLayout ="http://schemas.android.com/apk/res/android"  ="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical" >  <Button     android:id="@+id/send_notice"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="Send notice"    />  </LinearLayout>

布局
public class MainActivity extends Activity implements OnClickListener {  private Button sendNotice;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    sendNotice = (Button) findViewById(R.id.send_notice);    sendNotice.setOnClickListener(this);  }  @Override  public void onClick(View view) {    switch (view.getId()) {      case R.id.send_notice:        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);        Notification notification = new Notification(            R.drawable.ic_launcher, "This is a ticker text", System.currentTimeMillis());                notification.setLatestEventInfo(this, "This is content title",            "This is content text", null);        manager.notify(1, notification);      default:        break;    }  }}

 



 

 







 

 

 



 




原标题:安卓中通知(Notification)的基本使用方法

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

​9月如何规划亚马逊旺季?奉上3点建议:https://www.kjdsnews.com/a/541660.html
亚马逊卖家涌入东南亚,会带来什么变化?:https://www.kjdsnews.com/a/541661.html
Shopify红人营销怎么做,Shopify营销流程介绍:https://www.kjdsnews.com/a/541662.html
韩国——触手可及的跨境市场:https://www.kjdsnews.com/a/541663.html
你的物流成本为什么居高不下?试试这7个技巧!:https://www.kjdsnews.com/a/541664.html
亚马逊日本站积分中心上线,11/1起将自动更新账户积分比例!:https://www.kjdsnews.com/a/541665.html
2022世界杯门票如何买?:https://www.vstour.cn/a/363182.html
沈阳到本溪的距离有多远?(详细路线及交通方式):https://www.vstour.cn/a/363183.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流