The Android app icon displays the principle of implementation on the status bar _android

Source: Internet
Author: User
first, ahead
In the study of "Android similar to the 360,qq Butler Suspension window" whim, want to apply the icon also displayed to the status bar, similar to mobile phone QQ, and when the message to change the status bar icon display.

Second, the principle
In fact, after studying, we found that it is very simple:
2.1 Display icons on the status bar
Copy Code code as follows:

Notificationmanager nm = (Notificationmanager) getsystemservice (Context.notification_service);
Notification n = new Notification (
Resid, "floats start!", System.currenttimemillis ());

Place this notice in the ongoing group of the notification bar
N.flags |= notification.flag_ongoing_event;
Indicates that this notification is not cleared after clicking "Purge Notification" in the notification bar.
Often used with flag_ongoing_event
N.flags |= notification.flag_no_clear;

pendingintent pi = pendingintent.getactivity (this, 0, getintent (), 0);
n.contentintent = PI;
N.setlatesteventinfo (This, "Floatswindow", "start!", pi);
Nm.notify (Notification_id_icon, N);

2.2 Change the display of icons
Do not cancel this notice, simply passing in a different resid, and then notice.
Copy Code code as follows:

Package Com.chris.floats.window;
Import Android.os.Bundle;
Import Android.util.DisplayMetrics;
Import android.view.Gravity;
Import Android.view.WindowManager;
Import android.app.Activity;
Import android.app.Notification;
Import Android.app.NotificationManager;
Import android.app.PendingIntent;
Import Android.content.Context;
public class Mainactivity extends activity {
private static WindowManager mwindowmgr = null;
Private Windowmanager.layoutparams mwindowmgrparams = null;
private static Floatswindowview Mfloatswindowview = null;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
}

@Override
protected void OnStop () {
Super.onstop ();
Deleteicontostatusbar ();
}
/*
* Display the main interface, remove the suspension layer
* Modify the icon on the status bar
*/
@Override
public void Onwindowfocuschanged (Boolean hasfocus) {
if (Hasfocus) {
if (Mfloatswindowview!= null) {
Mwindowmgr.removeview (Mfloatswindowview);
Mfloatswindowview = null;
}
Addicontostatusbar (R.DRAWABLE.A0);
}else{
Getwindowlayout ();
Addicontostatusbar (R.drawable.ic_launcher);
}
}
private void InitParams () {
Displaymetrics DM = getresources (). Getdisplaymetrics ();
Mwindowmgrparams.x = dm.widthpixels-136;
MWINDOWMGRPARAMS.Y = 300;
Mwindowmgrparams.width = 136;
Mwindowmgrparams.height = 136;
}
private void Getwindowlayout () {
if (Mfloatswindowview = = null) {
Mwindowmgr = (WindowManager) getbasecontext (). Getsystemservice (Context.window_service);
Mwindowmgrparams = new Windowmanager.layoutparams ();

/*
* 2003 in the point of suspension above all interface
* (in the 4.0+ system, under the Pull-down menu, and in 2.3, above the pull menu)
*/
Mwindowmgrparams.type = 2003;
Mwindowmgrparams.format = 1;

/*
* Code is actually wmparams.flags |= flag_not_focusable;
* The origin of the 40 is the default property of Wmparams (+) + flag_not_focusable (8)
*/
Mwindowmgrparams.flags = 40;
mwindowmgrparams.gravity = Gravity.left | Gravity.top;
InitParams ();

Mfloatswindowview = new Floatswindowview (this);
Mwindowmgr.addview (Mfloatswindowview, mwindowmgrparams);
}
}

Private final static int notification_id_icon = 0x10000;
/*
* If you do not remove icon from the status bar and continue calling Addicontostatusbar,
* There will be no change. Apart from:
* If you set a different icon for the RESID in notification, you will see a different
* The icon
*/
private void Addicontostatusbar (int resid) {
Notificationmanager nm = (Notificationmanager) getsystemservice (Context.notification_service);
Notification n = new Notification (
Resid, "floats start!", System.currenttimemillis ());

Place this notice in the ongoing group of the notification bar
N.flags |= notification.flag_ongoing_event;
Indicates that this notification is not cleared after clicking "Purge Notification" in the notification bar.
Often used with flag_ongoing_event
N.flags |= notification.flag_no_clear;

pendingintent pi = pendingintent.getactivity (this, 0, getintent (), 0);
n.contentintent = PI;
N.setlatesteventinfo (This, "Floatswindow", "start!", pi);
Nm.notify (Notification_id_icon, N);
}

private void Deleteicontostatusbar () {
Notificationmanager nm = (Notificationmanager) getsystemservice (Context.notification_service);
Nm.cancel (Notification_id_icon);
}
}

The above is the source code, when the application of focus changes, the status bar on the picture will also follow the changes.
SOURCE Download Address: http://download.csdn.net/detail/qingye_love/5506825

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.