notification(通知)的使用

来源:互联网 发布:linux找工作 编辑:程序博客网 时间:2024/06/02 11:18

http://blog.csdn.net/vipzjyno1/article/details/25248021

http://blog.csdn.net/xxbs2003/article/details/19167331

private Notification.Builder mTetheredNotificationBuilder;    private void showTetheredNotification(int icon,String type){    synchronized(Tethering.this.mNotificationSync){    NotificationManager notificationManager=    (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);    if(notificationManager==null){    return;    }        WifiManager mgr=(WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);    if(mgr==null){    return;    }        if(mLastNotificationId!=0){    if(mLastNotificationId==icon){    return;    }    notificationManager.cancelAsUser(null,mLastNotificationId,    UserHandle.ALL);//就是cancel掉notification    mLastNotificationId=0;    }        Intent intent=new Intent();    if(isBspPackage){    intent.setClassName("com.android.settings",com.android.settings.TetherSettings);    }else{    if("wifi".eqauls(type)){    intent.setClassName("com.android.settings",    "com.android.settings.wifi.hotspot.TetherWifiSettings");    }else{    intent.setClassName("com.android.settings",     "com.android.settings.TetherSettings");    }    }        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);    //FLAG_ACTIVITY_NO_HISTORY参数的意义:跳转到的Activity不压入栈中        PendingIntent pi=PendingIntent.getActivityAsUser(mContext,0,intent,0,    null,UserHandle.CURRENT);        Resources r=Resources.getSystem();    CharSequence title=r.getText(com.android.internal.R.string.tethered_notification_title);    String message=null;    String message_1=null;    String message_2=null;        if(isBspPackage()){    message=r.getString(com.android.internal.R.string.tethered_notification_message);    }else{    if("wifi".equals(type)||"comb".equals(type)){    List<HotspotClient> clients=mgr.getHotspotClients();    int connected=0;    int blocked=0;    if(clients!=null){    for(HotspotClient client:clients){    if(client.isBlocked){    blocked++;    }    }    connected=clients.size()-blocked;    }    message=r.getString(com.mediatek.internal.R.string.tethered_notification_message_for_hotspot,connected,blocked);    }else{    message=r.getString(com.android.internal.R.string.tethered_notification_message);    }    }        message_1=message;    boolean enable=Settings.Secure.getInt(mContent.getContentResolver(),    Settings.Secure.INTERFACE_THROTTLE,0)==1;    if(enable&&("wifi".equals(type)||"comb".equals(type))){    message_2=r.getString(com.mediatek.internal.R.string.tethered_notification_message_bandwidth_set);    message=message_1+", "+message_2;        if(mTetheredNotificationBuilder==null){    mTetheredNotificationBuilder=new Notification.Builder(mContext);    mTetheredNotificationBuilder.setWhen(0)    .setOngoing(true)    .setColor(mContext.getColor(    com.android.internal.R.color.sytem_notification_accent_color))    .setVisibility(Notification.VISIBILITY_PUBLIC)    .setCategory(Notification.CATEGORY_STATUS);    }    mTetheredNotificationBuilder.setSmallIcon(icon)    .setContentTitle(title)    .setContentText(message)    .setContentIntent(pi);    mLastNotificationId=icon;        Notification.InboxStyle style=new Notification.InboxStyle(mTetheredNotificationBuilder).addLine(message_1);        if(message_2!=null){    style.addLine(message_2);    }        notificationManager.notifyAsUser(null,mLastNotificationId,    style.build(),UserHandle.ALL);    }    }        private void updateTetheredNotification(){    synchronized(mPublicSync){    boolean wifiTethered=false;    Set ifaces=mIfaces.keySet();    for(Object iface:ifaces){    TetherInterfaceSM sm=mIfaces.get(iface);    if(sm!=null && sm.isTethered() && isWifi((String)iface)){    wifiTethered=true;    }    }        if(!wifiTethered)    {    return;    }    }    synchronized(Tethering.this.mNotificationSync){    NotificationManager notificationManager=(NotificationManager)    mContext.getSystemService(Context.NOTIFICATION_SERVICE);    if(notificationManager==null || mgr==null || mTetheredNotificationBuilder==null){    return;    }    List<HotspotClient> clients=mgr.getHotspotClients();    int connected=0;    int blocked=0;    if(client!=null){    for(HotspotClient client:clients){    if(client.isBlocked){    blocked++;    }    }    connected=clients.size()-blocked;    }    Resources r=Resources.getSystem();    CharSequence title=r.getText(com.android.internal.R.string.tethered_notification_title);    String message=null;    if(isBspPackage()){    message=r.getString(com.android.internal.R.string.tethered_notification_message);        }else{    message=r.getString(com.android.internal.R.string.tethered_notification_message_for_hotsport);    }        String message_1=message;    String message_2=null;    boolean enable=Settings.Secure.getInt(mContext.getContentResolver(),    Settings.Secure.INTERFACE_THROTTLE,0)==1;    if(enable){    message_2=r.getString(com.android.internal.R.string.tethered_notification_message_bandwidth_set);    message=message_1+", "+message_2;    }    mTetheredNotificationBuilder.setContentTitle(title)    .setContentText(message);        Notification.InboxStyle style=new Notification.InboxStyle(mTetheredNotificationBuilder).addLine(message_1);        if(message_2!=null){    style.addLine(message_2);    }        notificationManager.notify(null,mLastNotificationId,    style.build(),UserHandle.ALL);    }    }        private void clearTetheredNotification(){    synchronized(Tethering.this.mNotificationSync){    NotificationManager notificationManager=(NotificationManager)    mContext.getSystemService(Context.NOTIFICATION_SERVICE);    if(notificationManager!=null && mLastNotificationId!=0){    notificationManager.cancelAsUser(null,mLastNotificationId,    UserHandle.ALL);    }    }    }    }



0 0
原创粉丝点击