Android的Notification实例介绍

结合代码解释:

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;

...

private void showNotification(Message msg, int id) {
        NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.notiicon, msg
                .getTitle(), System.currentTimeMillis());
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        Intent intent = new Intent(this, MainActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("info", msg.getInfo());
        intent.putExtras(bundle);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent contentIntent = PendingIntent.getActivity(this, id,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);

        notification.setLatestEventInfo(this, msg.getTitle(), msg.getInfo(),
                contentIntent);
        notiManager.notify(id, notification);

    }


还可以通过设置资源文件来定义显示内容等:

     String strText = getResources().getString(R.string.network_switch_notify_title);
     String strNotice = getResources().getString(R.string.for_user_guide);


 
1. 从系统中获得Notification服务,getSystemService()

NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);


2. 构造一个Notification,包括图标,图标的文字,以及Notification时间部分,通常使用当前时间。
FLAG_AUTO_CANCEL说明Notification点击一次就消失。

Notification notification = new Notification(R.drawable.notiicon, msg
                .getTitle(), System.currentTimeMillis());
        notification.flags = Notification.FLAG_AUTO_CANCEL;

3.构造一个Intent, FLAG_ACTIVITY_CLEAR_TOP, FLAG_ACTIVITY_NEW_TASK 两个FLAG表示优先寻找已经打开的应用,如果应用没有打开那么启动它。

Intent intent = new Intent(this, MainActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("info", msg.getInfo());
        intent.putExtras(bundle);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_NEW_TASK);

 
4. PendingIntent 是Intent的包装和描述。 把Intent包装在PendingIntent里,this是Context,id是PendingIntent的标志,如果id相同会被认为是一个。FLAG_UPDATE_CURRENT是指后来的PendingIntent会更新前面的。

PendingIntent contentIntent = PendingIntent.getActivity(this, id,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);

 
5. 发送Notification。
notification.setLatestEventInfo(this, msg.getTitle(), msg.getInfo(),
                contentIntent);

        notiManager.notify(id, notification);



Android tethering 实例


        Intent broadcast = new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
        broadcast.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_AVAILABLE_TETHER,
                availableList);
        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ACTIVE_TETHER, activeList);
        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ERRORED_TETHER,
                erroredList);
        mContext.sendStickyBroadcast(broadcast);
        if (DBG) Log.d(TAG, "sendTetherStateChangedBroadcast " + availableList.size() + ", " +
                activeList.size() + ", " + erroredList.size());

        if (usbTethered) {
            if (wifiTethered) {
                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general);
            } else {
                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_usb);
            }
        } else if (wifiTethered) {
            showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_wifi);
        } else {
            clearTetheredNotification();
        }
    }

    private void showTetheredNotification(int icon) {
        NotificationManager notificationManager =
                (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager == null) {
            return;
        }

        if (mTetheredNotification != null) {
            if (mTetheredNotification.icon == icon) {
                return;
            }
            notificationManager.cancel(mTetheredNotification.icon);
        }

        Intent intent = new Intent();
        intent.setClassName("com.android.settings", "com.android.settings.TetherSettings");
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

        PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);

        Resources r = Resources.getSystem();
        CharSequence title = r.getText(com.android.internal.R.string.tethered_notification_title);
        CharSequence message = r.getText(com.android.internal.R.string.
                tethered_notification_message);

        if(mTetheredNotification == null) {
            mTetheredNotification = new Notification();
            mTetheredNotification.when = 0;
        }
        mTetheredNotification.icon = icon;
        mTetheredNotification.defaults &= ~Notification.DEFAULT_SOUND;
        mTetheredNotification.flags = Notification.FLAG_ONGOING_EVENT;
        mTetheredNotification.tickerText = title;
        mTetheredNotification.setLatestEventInfo(mContext, title, message, pi);

        notificationManager.notify(mTetheredNotification.icon, mTetheredNotification);
    }





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值