Android 8.1.0 判断系统当前顶部 Activity 是不是 目标 Activity

本文提供了一个在Android8.1.0环境下检查特定应用程序及其Activity是否处于前台运行状态的方法。通过获取系统的运行任务信息,对比目标应用的包名和Activity名称,实现对目标Activity是否为顶部Activity的判断。

         当前是基于Android 8.1.0,在不同的版本,方法有可能表现不一样。

         没有足够测试机,没有进行适配。

    private boolean ifTargetActivityIsTop(Context context, String targetAppPackageName, String targetAppActivityName) {
        String TAG = "Check_Top_Activity";
        ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> mTasks = mActivityManager.getRunningTasks(1);//5.0

        if (mTasks.get(0) != null){
            ComponentName topActivity = mTasks.get(0).topActivity;
            if(topActivity != null && !TextUtils.isEmpty(topActivity.getPackageName()) &&
                    !TextUtils.isEmpty(topActivity.getClassName())){
                Log.w(TAG,"topActivity.getPackageName() -> " + topActivity.getPackageName());
                Log.w(TAG,"topActivity.getClassName() -> " + topActivity.getClassName());
                if(topActivity.getPackageName().equals(targetAppPackageName) &&
                        topActivity.getClassName().equals(targetAppActivityName)) {
                    Log.d(TAG,"target Activity is in top !");
                    return true;
                }else {
                    Log.w(TAG,"target Activity is not in top.");
                    return false;
                }
            }else {
                Log.w(TAG,"topActivity is null or PackageName is null or ClassName is null.");
                return false;
            }
        }else {
            Log.w(TAG,"mTasks.get(0) == null ");
            return false;
        }
    }

 以上。

最后,这是我写的简单 demo,参考一下。

//地址(等有空了传上去)

 

 

 

 

 

 

plugins { id 'com.android.application' } android { namespace 'com.example.kucun2' compileSdk 33 defaultConfig { applicationId "com.example.kucun2" minSdk 24 targetSdk 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_17 targetCompatibility JavaVersion.VERSION_17 } buildFeatures { viewBinding true } } dependencies { implementation "androidx.activity:activity:1.7.2" implementation "androidx.activity:activity-ktx:1.7.2" implementation 'com.google.code.gson:gson:2.8.9' implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.11.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.3' implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.1' implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1' implementation 'androidx.navigation:navigation-fragment:2.4.1' implementation 'androidx.navigation:navigation-ui:2.4.1' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' // 网络请求 implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' // 表格控件 implementation 'io.github.cymchad:BaseRecyclerViewAdapterHelper:4.0.3' // implementation 'io.github.cymchad:BaseRecyclerViewAdapterHelper:4.1.7' implementation 'androidx.recyclerview:recyclerview:1.3.2' // 刷新控件 implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0' // Material Design implementation 'com.google.android.material:material:1.11.0' // 对话框 implementation 'com.github.gabriel-TheCode:AestheticDialogs:1.3.6' implementation 'at.favre.lib:bcrypt:0.9.0' // Android专用BCrypt库 implementation 'androidx.room:room-runtime:2.4.3' annotationProcessor 'androidx.room:room-compiler:2.4.3' } 2 issues were found when checking AAR metadata: 1. Dependency 'androidx.activity:activity-ktx:1.8.0' requires libraries and applications that depend on it to compile against version 34 or later of the Android APIs. :app is currently compiled against android-33. Also, the maximum recommended compile SDK version for Android Gradle plugin 7.4.2 is 33. Recommended action: Update this project's version of the Android Gradle plugin to one that supports 34, then update this project to use compileSdkVerion of at least 34. Note that updating a library or application's compileSdkVersion (which allows newer APIs to be used) can be done separately from updating targetSdkVersion (which opts the app in to new runtime behavior) and minSdkVersion (which determines which devices the app can be installed on). 2. Dependency 'androidx.activity:activity:1.8.0' requires libraries and applications that depend on it to compile against version 34 or later of the Android APIs. :app is currently compiled against android-33. Also, the maximum recommended compile SDK version for Android Gradle plugin 7.4.2 is 33. Recommended action: Update this project's version of the Android Gradle plugin to one that supports 34, then update this project to use compileSdkVerion of at least 34. Note that updating a library or application's compileSdkVersion (which allows newer APIs to be used) can be done separately from updating targetSdkVersion (which opts the app in to new runtime behavior) and minSdkVersion (which determines which devices the app can be installed on).
06-05
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值