Android 使用Notification自定义通知栏显示。自定义系统状态栏通知类NotificationExtend,也可以手动更新状态栏。依据此项目可以做成线程接受,或者广播接受自动更新。先看实现效果。

项目地址下载:点击

1、效果图


2、自定义NotificationExtend类来统一管理通知栏

package com.notification.manage;import java.lang.ref.WeakReference;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;/** * 负责显示通知栏 * @author Administrator * */public class NotificationExtend {private WeakReference<Context> mContextReference;private final String TAG = "Show";private int mHeadId;private String mTitle = "";private String mContent = "";public void setActivity(Context context) {if (null != mContextReference) mContextReference.clear();mContextReference = new WeakReference<Context>(context);}public void setParam(int RHeadId, String title, String content) {mHeadId = RHeadId;mTitle = title;mContent = content;}public Context getContext() {if (null == mContextReference) return null;return mContextReference.get();}@SuppressWarnings("deprecation")public void showNotification() {if (null == mContextReference || null == mContextReference.get()) return;NotificationManager notificationManager = (                  NotificationManager)mContextReference.get().getSystemService(                          android.content.Context.NOTIFICATION_SERVICE);Notification notification = new Notification();int icon = mHeadId;CharSequence tickerText = mContent;long when = System.currentTimeMillis();notification.flags = Notification.FLAG_ONGOING_EVENT;notification.flags |= Notification.FLAG_AUTO_CANCEL;          notification.flags |= Notification.FLAG_SHOW_LIGHTS;//系统状态栏会出现闪一下提示--正在后台运行notification = new Notification(icon, tickerText, when);// 放置系统状态栏  在"正在运行"栏目中        Intent notificationIntent = new Intent(mContextReference.get(), mContextReference.get().getClass());          notificationIntent.setAction(Intent.ACTION_MAIN);          notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);          PendingIntent contentIntent = PendingIntent.getActivity(          mContextReference.get(), 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);                    //放置在屏幕整个下滑,出现栏目中         notification.setLatestEventInfo(mContextReference.get(), mTitle, mContent, contentIntent);          //刷新        notificationManager.notify(TAG, 529, notification); }public void cancelNotification(){ if (null == mContextReference || null == mContextReference.get()) return;        NotificationManager notificationManager = (                  NotificationManager) mContextReference.get().getSystemService(                          android.content.Context.NOTIFICATION_SERVICE);          notificationManager.cancel(TAG, 529);       } }

3、统一CustomActivity继承activity,供后面的activity继承。

package com.notification.manage;import com.notification.activity.R;import android.app.Activity;import android.os.Bundle;public class CustomActivity extends Activity{protected static NotificationExtend mNotificationExtend = new NotificationExtend();protected boolean mNotificationShow = true;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);if (mNotificationShow) {mNotificationExtend.setActivity(this);}}@Overrideprotected void onResume() {super.onResume();if (mNotificationShow == true)mNotificationExtend.cancelNotification();}@Overrideprotected void onPause() {super.onPause();if (mNotificationShow == true) {//设置通知栏的参数mNotificationExtend.setParam(R.drawable.head, getString(R.string.app_name), getString(R.string.app_is_run_background));mNotificationExtend.showNotification();}}@Overridepublic void finish() {super.finish();}}

4、看怎么调用,使用。主要使用是在上面那个类里面的onPause和onResume的activity的生命周期里。

package com.notification.activity;import com.notification.manage.CustomActivity;import android.os.Bundle;import android.view.KeyEvent;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.app.AlertDialog;import android.content.DialogInterface;public class NotificationMainActivity extends CustomActivity{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_notification_main);Button button = (Button)findViewById(R.id.button1);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//测试更新通知栏,这个东西可以做成线程接受,或者广播接受自动更新mNotificationExtend.setParam(R.drawable.notify_general, "有新的消息", "吃了饭没?");mNotificationExtend.showNotification();}});}/** * 连续按两次返回键就退出 *///private long firstTime;////@Override//public void onBackPressed() {//// TODO Auto-generated method stub//if (System.currentTimeMillis() - firstTime < 3000) {//Exit();//} else {//firstTime = System.currentTimeMillis();//}//}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {Exit();return true;}return super.onKeyDown(keyCode, event);}private void Exit() {//提示是否退出软件AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("提示");builder.setMessage("是否退出?");builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {System.exit(0);}});builder.setNegativeButton("隐藏", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {//暂时消失当前activity,移动到后台moveTaskToBack(true);}});builder.show();}}


项目地址下载:点击

更多相关文章

  1. Flutter学习笔记 按返回键直接回到桌面,不退出APP
  2. Android完全退出的4种方法
  3. android之通过SurfaceView以及SurfaceHolder进行视频播放
  4. android使用广播退出应用程序
  5. 状态栏提示Notification
  6. Android悬浮通知无效的问题
  7. android开发两种退出程序方式(killProcess,System.exit)
  8. Notification 通知栏的使用
  9. Android实现退出界面弹出提示对话框

随机推荐

  1. 利用Android画圆弧canvas.drawArc()实例
  2. android-adb调试与sqlite远程shell(andro
  3. Android系统应用层App启动过程分析
  4. (20120731)android面试总结(002)
  5. OS X环境下如何搭建编译Cocos2D-X v3.x的
  6. 3.0之后在LinearLayout里增加分割线
  7. android中几个EditText的内容相关联的设
  8. Android(安卓)View measure(0,0)的作用
  9. android View与ViewGroup研究
  10. Android(安卓)金额转大写