如何从framework层面跳过app开屏广告(简单模拟)

如何从framework层面跳过app开屏广告(这里的app是自己写的demo,比较简单,这里仅记录刚刚学到的思路)。

App的组成

这个app demo仅有两个activity,一个是开屏广告页SplashActivity,另一个是内容显示页MainActivitySplashActivity这个广告页模拟现在的app的开屏广告,右上角有一个倒计时10s按钮,必须等到倒计时完成或者点击按钮才能进入MainActivity

以下是AndroidManifest.xml组成:

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="开屏广告"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication">

        <!-- 开屏页 -->
        <activity
            android:name=".SplashActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- 主界面 -->
        <activity
            android:name=".MainActivity"
            android:exported="true"> <!-- 这里exported="true"很重要 -->
        </activity>
    </application>

</manifest>

跳开屏广告思路

就是修改framework/baseActivity.javastartActivity方法,因为在桌面点击app启动的时候,就会调用这个方法。这个思路也有个坏处,如果app的广告activity中,进行了一些资源的设置或者后续逻辑的铺垫,就会影响后面打开的activity。

如何确认当前页面的Activity

目的是确定广告页和实际内容页的activity,方便我们进行跳过广告进入实际的页面,命令是adb shell dumpsys activity activities | grep -i "ResumedActivity"

bash 复制代码
$ dumpsys activity activities | grep -i "ResumedActivity"
    topResumedActivity=ActivityRecord{2cd1b30 u0 com.example.myapplication/.MainActivity t45}
  ResumedActivity: ActivityRecord{2cd1b30 u0 com.example.myapplication/.MainActivity t45}

Activity.java的startActivity修改

java 复制代码
framework/base/core/java/android/app/Activity.java
    
    
@Override
public void startActivity(Intent intent, @Nullable Bundle options) {
    android.util.Log.d("test1", "startActivity");
    ComponentName srcCom =
        new ComponentName("com.example.myapplication",
        "com.example.myapplication.SplashActivity"); // 广告页

    ComponentName destCom =
        new ComponentName("com.example.myapplication",
        "com.example.myapplication.MainActivity"); // 内容页
    if(intent.getComponent() != null ) {
        android.util.Log.d("test1", "[startActivity] intent.getComponent() != null == "+intent.getComponent());
        if(intent.getComponent().equals(srcCom)) { // 如果当前要跳转的是广告页,重定向到内容页
            android.util.Log.d("test1", "[startActivity] intent.getComponent().equals(srcCom)");
            intent.setComponent(destCom);
        }
    }else {
        android.util.Log.d("test1", "[startActivity] intent.getComponent() == null");
    }


    getAutofillClientController().onStartActivity(intent, mIntent);
    if (options != null) {
        startActivityForResult(intent, -1, options);
    } else {
        // Note we want to go through this call for compatibility with
        // applications that may have overridden the method.
        startActivityForResult(intent, -1);
    }
}

关于AndroidManifest.xml中exported="true"简单说明

在一开始的AndroidManifest.xml中,MainActivity的exported="false",就导致死活跳转不了,而且连app都打不开了,直接弹窗说"未安装该应用"。log报错如下:

log 复制代码
java.lang.SecurityException: Permission Denial: starting Intent ...
cmp=com.example.myapplication/.MainActivity
... not exported from uid 10163

原因是Launcher 想启动 MainActivity,但这个 Activity exported=false,系统禁止跨进程启动。

系统规则是:

exported 含义
true 允许其他应用启动
false 只能本应用启动

因为这里是自己的app demo,所以exported属性咱们自己可以改,真实的app肯定改不了,所以这里提供的仅仅是一个思路。

相关推荐
恋猫de小郭11 分钟前
Android Studio 放着没怎么用,怎么也会越来越卡?
android·前端·flutter
Kapaseker14 分钟前
Compose 动画 — 显隐的艺术
android·kotlin
黄林晴18 分钟前
Android官方发布 AppFunctions,让系统AI直接调用你的APP
android·agent
2501_915909061 小时前
完整指南:如何将iOS应用上架到App Store
android·ios·小程序·https·uni-app·iphone·webview
赏金术士3 小时前
Retrofit + Kotlin 协程(Android 实战教程)
android·kotlin·retrofit
大炮筒10 小时前
COCOS2DX4.0CPPWIN移植安卓踩坑总结
android
qq_4228286212 小时前
android图形学之SurfaceControl和Surface的关系 五
android·开发语言·python
tongyiixiaohuang14 小时前
轻易云平台助力快麦数据入库MySQL
android·数据库·mysql
JohnnyDeng9417 小时前
Android 包体积优化:R8/ProGuard 深度配置
android
qq_4523962317 小时前
第六篇:《JMeter逻辑控制器:循环、条件和交替执行》
android·java·jmeter