Android——自定義通知欄使用

轉載請註明轉自:[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);
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章