The Android app icon is displayed on the status bar and different icons are displayed.

Source: Internet
Author: User

Ii. Principles

In fact, after the study, we found that it is very simple:

2.1 display icon on the status bar

[Java]
Icationicationmanager nm = (NotificationManager) getSystemService (Context. NOTIFICATION_SERVICE );
Notification n = new Notification (
ResId, "Floats Start! ", System. currentTimeMillis ());
 
// Place the notification to the "Ongoing" group in the notification bar, that is, the "running" group.
N. flags | = Notification. FLAG_ONGOING_EVENT;
// Indicates that the notification is not cleared after you click "clear 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. icationy (icationication_id_icon, n );

Icationicationmanager nm = (NotificationManager) getSystemService (Context. NOTIFICATION_SERVICE );
Notification n = new Notification (
ResId, "Floats Start! ", System. currentTimeMillis ());

// Place the notification to the "Ongoing" group in the notification bar, that is, the "running" group.
N. flags | = Notification. FLAG_ONGOING_EVENT;
// Indicates that the notification is not cleared after you click "clear 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. Y (icationication_id_icon, n); 2.2 modify the icon display

You don't need the cancel notification. You only need to pass in different resId and then send the notification.

[Java]
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. icationicationmanager;
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 ();
}
 
/*
* When the main application interface is displayed, the floating layer is removed.
* 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;
Mwindowsmgrparams. 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 floating on all interfaces
* (In the 4.0 + system, under the drop-down menu, and in the 2.3 system, above the top menu)
*/
Mwindows mgrparams. type = 2003;
MWindowMgrParams. format = 1;

/*
* The actual code is wmParams. flags | = FLAG_NOT_FOCUSABLE;
* 40 is the default property of wmParams (32) + 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 icationication_id_icon = 0x10000;
/*
* If the ICON is not deleted from the status bar and addIconToStatusbar is called,
* There will be no changes. Except:
* If you set different icons for resId in notification, different icons are displayed.
* Icon
*/
Private void addIconToStatusbar (int resId ){
Icationicationmanager nm = (NotificationManager) getSystemService (Context. NOTIFICATION_SERVICE );
Notification n = new Notification (
ResId, "Floats Start! ", System. currentTimeMillis ());

// Place the notification to the "Ongoing" group in the notification bar, that is, the "running" group.
N. flags | = Notification. FLAG_ONGOING_EVENT;
// Indicates that the notification is not cleared after you click "clear 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. icationy (icationication_id_icon, n );
}

Private void deleteIconToStatusbar (){
Icationicationmanager nm = (NotificationManager) getSystemService (Context. NOTIFICATION_SERVICE );
Nm. cancel (icationication_id_icon );
}
}

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. icationicationmanager;
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 ();
}

/*
* When the main application interface is displayed, the floating layer is removed.
* 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;
Mwindowsmgrparams. 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 floating on all interfaces
* (In the 4.0 + system, under the drop-down menu, and in the 2.3 system, above the top menu)
*/
Mwindows mgrparams. type = 2003;
MWindowMgrParams. format = 1;

/*
* The actual code is wmParams. flags | = FLAG_NOT_FOCUSABLE;
* 40 is the default property of wmParams (32) + 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 icationication_id_icon = 0x10000;
/*
* If the ICON is not deleted from the status bar and addIconToStatusbar is called,
* There will be no changes. Except:
* If you set different icons for resId in notification, different icons are displayed.
* Icon
*/
Private void addIconToStatusbar (int resId ){
Icationicationmanager nm = (NotificationManager) getSystemService (Context. NOTIFICATION_SERVICE );
Notification n = new Notification (
ResId, "Floats Start! ", System. currentTimeMillis ());

// Place the notification to the "Ongoing" group in the notification bar, that is, the "running" group.
N. flags | = Notification. FLAG_ONGOING_EVENT;
// Indicates that the notification is not cleared after you click "clear 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. icationy (icationication_id_icon, n );
}
 
Private void deleteIconToStatusbar (){
Icationicationmanager nm = (NotificationManager) getSystemService (Context. NOTIFICATION_SERVICE );
Nm. cancel (icationication_id_icon );
}
}
The above is the source code. When the focus of the application changes, the image on the status bar also changes.


 

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.