Android——自定义通知栏使用

来源:互联网 发布:丹朱围棋软件 编辑:程序博客网 时间:2024/06/05 04:38
转载请注明转自:[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);        }    }
0 0
原创粉丝点击