1. Get the current app package name
public static String Getrunningactivityname (context context) {
Activitymanager activitymanager= (Activitymanager) Context.getsystemservice (Context.activity_service);
Full class name
String runningactivity=activitymanager.getrunningtasks (1). Get (0). Topactivity.getclassname ();
String contextactivity = runningactivity.substring (Runningactivity.lastindexof (".") +1);
return contextactivity;
}
2. Get the name of the current activity
public static String Getapppackagename (context context) {
Activitymanager Activitymanager = (activitymanager) context.getsystemservice (Context.activity_service);
List<activitymanager.runningtaskinfo> TaskInfo = activitymanager.getrunningtasks (1);
ComponentName componentinfo = taskinfo.get (0). topactivity;
LOG.D ("Lixx", "Current application:" + componentinfo.getpackagename ());
return Componentinfo.getpackagename ();
}
3, determine whether the system desktop
/**
* Determine if the current interface is desktop
*/
Private Boolean ishome () {
Activitymanager Mactivitymanager = (activitymanager) getsystemservice (Context.activity_service);
list<runningtaskinfo> RTI = mactivitymanager.getrunningtasks (1);
Return Gethomes (). Contains (Rti.get (0). Topactivity.getpackagename ());
}
/**
* Get app package names for apps that belong to the desktop
*
* @return Returns a list of strings that contain all the package names
*/
Private List<string> Gethomes () {
list<string> names = new arraylist<string> ();
Packagemanager Packagemanager = This.getpackagemanager ();
Intent Intent = new Intent (intent.action_main);
Intent.addcategory (Intent.category_home);
list<resolveinfo> ResolveInfo = packagemanager.queryintentactivities (Intent,
PACKAGEMANAGER.MATCH_DEFAULT_ONLY);
for (ResolveInfo Ri:resolveinfo) {
Names.add (Ri.activityInfo.packageName);
}
return names;
}
4, the typical service class
public class Floatwindowservice extends Service {
/**
* Used to create or remove suspended windows in the thread.
*/
Private Handler Handler = new Handler ();
/**
* Timer, timed to detect whether the suspended window should be created or removed at the moment.
*/
private timer timer;
@Override
Public IBinder Onbind (Intent Intent) {
return null;
}
@Override
public int Onstartcommand (Intent Intent, int flags, int startid) {
Turn on the timer and refresh every 0.5 seconds
if (timer = = null) {
Timer = new timer ();
Timer.scheduleatfixedrate (New Refreshtask (), 0, 500);
LOG.D ("Lixx", "===== timed refresh, monitor the status of the suspended window =====");
}
Return Super.onstartcommand (Intent, flags, Startid);
}
@Override
public void OnDestroy () {
Super.ondestroy ();
Service is terminated while the timer continues to run
Timer.cancel ();
timer = null;
}
Class Refreshtask extends TimerTask {
@Override
public void Run () {
Get the name of the current activity
String activity_name = Mywindowmanager.getrunningactivityname (Getapplicationcontext ());
LOG.D ("Lixx", "activity_name---->" + activity_name);
Get the application package name
String pck_name = Mywindowmanager.getapppackagename (Getapplicationcontext ());
LOG.D ("Lixx", "pck_name---->" + pck_name);
The current interface is the desktop, and no hover window is displayed, creating a floating window.
if (Ishome ()) {
Handler.post (New Runnable () {
@Override
public void Run () {
LOG.D ("Lixx", "desktop, remove the hover window!") ");
Mywindowmanager.removesmallwindow (Getapplicationcontext ());
}
});
}else if (! Textutils.isempty (Activity_name)) {
LOG.D ("Lixx", "Activity_name is not null");
if (MYWINDOWMANAGER.ISSHOWINGYKQ (activity_name) && pck_name.equals ("com.ysten.istouch.client.screenmoving ")){
Handler.post (New Runnable () {
@Override
public void Run () {
Mywindowmanager.createsmallwindow (Getapplicationcontext ());
}
});
}else{
Handler.post (New Runnable () {
@Override
public void Run () {
LOG.D ("Lixx", "Remote Control interface, remove the hover window!") ");
Mywindowmanager.removesmallwindow (Getapplicationcontext ());
}
});
}
}else{
Handler.post (New Runnable () {
@Override
public void Run () {
Mywindowmanager.removesmallwindow (Getapplicationcontext ());
}
});
}
}
}
Android----Get the current app package name, the name of the current activity, and whether the system desktop, service class