首页 > 代码库 > Android----获得当前应用包名、当前activity的名字、推断是否是系统桌面、service类

Android----获得当前应用包名、当前activity的名字、推断是否是系统桌面、service类

1、获得当前应用包名

public static String getRunningActivityName(Context context) {
ActivityManager activityManager=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 
//完整类名
String runningActivity=activityManager.getRunningTasks(1).get(0).topActivity.getClassName(); 
String contextActivity = runningActivity.substring(runningActivity.lastIndexOf(".")+1);
return contextActivity;
}

2、获得当前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", "当前应用:" + componentInfo.getPackageName());
return componentInfo.getPackageName();
}

3、推断是否是系统桌面

/**
* 推断当前界面是否是桌面
*/
private boolean isHome() {
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> rti = mActivityManager.getRunningTasks(1);
return getHomes().contains(rti.get(0).topActivity.getPackageName());
}


/**
* 获得属于桌面的应用的应用包名称

* @return 返回包括全部包名的字符串列表
*/
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、典型的service类

public class FloatWindowService extends Service {


/**
* 用于在线程中创建或移除悬浮窗。
*/
private Handler handler = new Handler();


/**
* 定时器。定时进行检測当前应该创建还是移除悬浮窗。
*/
private Timer timer;


@Override
public IBinder onBind(Intent intent) {
return null;
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 开启定时器,每隔0.5秒刷新一次
if (timer == null) {
timer = new Timer();
timer.scheduleAtFixedRate(new RefreshTask(), 0, 500);
Log.d("lixx", "=====定时刷新,监听悬浮窗的状态=====");
}
return super.onStartCommand(intent, flags, startId);
}


@Override
public void onDestroy() {
super.onDestroy();
// Service被终止的同一时候也停止定时器继续执行
timer.cancel();
timer = null;
}


class RefreshTask extends TimerTask {


@Override
public void run() {
//获得当前activity的名字
String activity_name = MyWindowManager.getRunningActivityName(getApplicationContext());
Log.d("lixx", "activity_name---->" + activity_name);
//获得应用程序包名
String pck_name = MyWindowManager.getAppPackageName(getApplicationContext());
Log.d("lixx", "pck_name---->" + pck_name);

// 当前界面是桌面,且没有悬浮窗显示。则创建悬浮窗。
if (isHome()) {
handler.post(new Runnable() {
@Override
public void run() {
Log.d("lixx", "桌面,移除悬浮窗!");
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", "遥控器界面。移除悬浮窗!

");
MyWindowManager.removeSmallWindow(getApplicationContext());
}
});
}
}else{
handler.post(new Runnable(){
@Override
public void run(){
MyWindowManager.removeSmallWindow(getApplicationContext());
}
});
}
}


}

Android----获得当前应用包名、当前activity的名字、推断是否是系统桌面、service类