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);
}
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;
return componentInfo.getPackageName();
}
3、推斷是否是系統桌面
/**
* 推斷當前界面是否是桌面
*/
private boolean isHome() {
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> rti = mActivityManager.getRunningTasks(1);
}
/**
* 獲得屬於桌面的應用的應用包名稱
*
* @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類