android notification怎么在状态栏显示图标
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android notification怎么在状态栏显示图标相关的知识,希望对你有一定的参考价值。
根据activity的生命周期,在activity不显示时,会执行onStop函数,所以你在onStop函数(按退出键除外)里面把notification放在通知栏里,再此显示时,把notification从通知栏里去掉。或者,只要程序在运行就一直显示通知栏图标,这样简单点。
privatevoid showNotification()
// 创建一个NotificationManager的引用
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
// 定义Notification的各种属性
Notification notification =new Notification(R.drawable.icon,
天籁之音播放器, System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的Ongoing即正在运行组中
notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在点击了通知栏中的清除通知后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults = Notification.DEFAULT_LIGHTS;
notification.ledARGB = Color.BLUE;
notification.ledOnMS =5000;
// 设置通知的事件消息
CharSequence contentTitle =天籁之音正在播放……; // 通知栏标题
CharSequence contentText =ameyume; // 通知栏内容
Intent notificationIntent =new Intent(context, MusicDemo.class); // 点击该通知后要跳转的Activity
PendingIntent contentItent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText,
contentItent);
// 把Notification传递给NotificationManager
notificationManager.notify(0, notification);
去掉通知栏通知的方法
// 启动后删除之前我们定义的通知
NotificationManager notificationManager = (NotificationManager) this 参考技术A setLatestEventInfoNotification.FLAG_***配合使用
Android Notification状态栏通知
没有添加额外的震动及声音效果,这里直接实现了通知的功能,看效果吧:
MainActivity.java
package com.example.notification; import android.os.Bundle; import android.annotation.SuppressLint; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { public NotificationManager mNotificationManager; @SuppressLint("NewApi") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 1-获得MotificationManager的引用。 String ns = Context.NOTIFICATION_SERVICE; mNotificationManager = (NotificationManager) getSystemService(ns); // 2-实例化Notification: int icon = R.drawable.new_mail; CharSequence tickerText = "Hello"; long when = System.currentTimeMillis(); Notification notification = new Notification(icon, tickerText, when); // 3-定义Notification,如显示icon、目标intent等信息 Context context = getApplicationContext(); CharSequence contentTitle = "My notification"; CharSequence contentText = "Hello World!"; Intent notificationIntent = new Intent(v.getContext(), MessageActivity.class); PendingIntent contentIntent = PendingIntent.getActivity( v.getContext(), 0, notificationIntent, 0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); // 4-传递给Manager. final int HELLO_ID = 1; mNotificationManager.notify(HELLO_ID, 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.activity_main, menu); return true; } }MessageActivity.java
package com.example.notification; import android.app.Activity; import android.app.NotificationManager; import android.content.Context; import android.os.Bundle; public class MessageActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_message); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.cancel(1);//这里实现了,点击消息后,自己主动清除消息的功能。 } }XML布局文件就不写了。非常easy的~ 这里没有实现通知到达时的提示效果,如震动、提示音之类。将在 Android Notification实现推送消息过程中接受到消息端有声音及震动及亮屏提示 文章实现这样的功能
以上是关于android notification怎么在状态栏显示图标的主要内容,如果未能解决你的问题,请参考以下文章
Android中使用Notification在状态栏上显示通知
Android自定义状态栏通知(Status Notification)的正确实现