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


    }

  • 相关阅读:
    linux-Redhat7 windows物理机与虚拟机设置共享目录
    解决Vue-cli3.0下scss文件编译过慢、卡顿问题
    CCS进阶——div的宽度和高度是由什么决定的?
    在线图片资源转换成Base64格式
    浅析libuv源码-node事件轮询解析(4)
    MaxCompute Studio使用心得系列7——作业对比
    from _sqlite3 import * ImportError: DLL load failed: 找不到指定的模块。
    Java高并发程序设计学习笔记(九):锁的优化和注意事项
    模块:摘要算法,hashlib
    面向对象:类的内置方法
  • 原文地址:https://www.cnblogs.com/yfceshi/p/6917595.html
Copyright © 2020-2023  润新知