转载请注明转自:[noyet12的博客](http://blog.csdn.net/u012975705) 博客原址:http://blog.csdn.net/u012975705/article/details/50073197

Android中通知栏的使用,还是刚学Android时玩过,后面一直没机会用到,今天做项目的时候用到了,这里mark下。

通知栏写法:

package com.plusub.renthostapp.service;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.os.IBinder;import android.support.annotation.Nullable;import android.widget.RemoteViews;import com.plusub.lib.service.BaseService;import com.plusub.lib.task.TaskMessage;import com.plusub.renthostapp.R;import com.plusub.renthostapp.activity.MainActivity;/** * package: com.plusub.renthostapp.service * Created by noyet on 2015/11/27. */public class NotifyService extends BaseService {    private static NotificationManager mManager;    private Notification mNotification;    private RemoteViews mRemoteViews;    private final static String RENT_NEWS = "有新的租地订单";    private final static String SHOP_NEWS = "有新的商品订单";    private final static String LOG_NEWS = "有新的日志";    public static void cancelNotify() {        mManager.cancelAll();    }    public static void startService(Context context) {        Intent intent = new Intent(context, NotifyService.class);        context.startService(intent);    }    public static void startService(Context context, Intent intent) {        context.startService(intent);    }    @Override    public void onCreate() {        super.onCreate();        mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);        mRemoteViews = new RemoteViews(getPackageName(), R.layout.notify_item1);        mNotification = new Notification(R.drawable.ic_launcher, "有新消息", System.currentTimeMillis());        mNotification.defaults |= Notification.DEFAULT_SOUND;        mNotification.flags |= Notification.FLAG_AUTO_CANCEL;    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        int logsNum = intent.getIntExtra("logsNum", 0);        int orderNum = intent.getIntExtra("orderNum", 0);        int rentNum = intent.getIntExtra("rentNum", 0);        Intent notifyIntent = new Intent(this, MainActivity.class);        if (rentNum > 0) {            mRemoteViews.setTextViewText(R.id.notify_item_tv, RENT_NEWS);            mNotification.contentView = mRemoteViews;            notifyIntent.putExtra("fragment", 0);// notifyIntent.putExtra("notify_text", RENT_NEWS);            mNotification.contentIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);            mManager.notify(0, mNotification);        }        if (orderNum > 0) {            mRemoteViews.setTextViewText(R.id.notify_item_tv, SHOP_NEWS);            mNotification.contentView = mRemoteViews;            notifyIntent.putExtra("fragment", 1);// notifyIntent.putExtra("notify_text", SHOP_NEWS);            mNotification.contentIntent = PendingIntent.getActivity(this, 1, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);            mManager.notify(1, mNotification);        }        if (logsNum > 0) {            mRemoteViews.setTextViewText(R.id.notify_item_tv, LOG_NEWS);            mNotification.contentView = mRemoteViews;            notifyIntent.putExtra("fragment", 0);// notifyIntent.putExtra("notify_text", LOG_NEWS);            mNotification.contentIntent = PendingIntent.getActivity(this, 2, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);            mManager.notify(2, mNotification);        }        return super.onStartCommand(intent, flags, startId);    }    @Override    public void exitApp() {        mManager.cancelAll();        stopSelf();    }    @Override    public boolean isAutoFinish() {        return false;    }    @Override    public void refresh(TaskMessage taskMessage, Object... objects) {    }    @Nullable    @Override    public IBinder onBind(Intent intent) {        return null;    }}

App不可见时启动服务

@Override    protected void onPause() {        super.onPause();        if (mTimer != null) {            mTimer.cancel();        }        if (mTask != null) {            mTask.cancel();        }        if (MainApplication.getInstance().getIsLogin()) {            mTask = new TimerTask() {                @Override                public void run() {                    getNews();                }            };            mTimer = new Timer();            mTimer.schedule(mTask, 3 * 1000, 3 * 60 * 1000);        }    }

更多相关文章

  1. Android通知
  2. Android监听系统通知
  3. Android消息通知
  4. 推荐阅读:Android开发者的博客
  5. Android使用TextView实现无下划线超链接 | 萝卜白菜的博客

随机推荐

  1. 入门数据分析
  2. 如何在业余时自学数据分析?
  3. 【从零到一学懂区块链】——区块
  4. 数据分析方法论
  5. SQL基础快速入门
  6. 从零到一学懂区块链(2):矿工&挖矿
  7. 数据处理
  8. Excel可视化(1):5个基本图使用指南
  9. 精读白皮书系列1:遇见比特币
  10. 我的2018年终总结分析