11月 292019
测试环境:自己编译的Android8.1系统(userDebug模式)
首先目标应用的AndroidManifest.xml中定义Activity的时候,必须设置一个intent-filter以及action属性,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zht.car.aidlclient"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".Test2Activity"> <intent-filter> <action android:name="action_abc" /> <category android:name="abc" /> </intent-filter> </activity> <activity android:name=".Test1Activity"> <intent-filter> <action android:name="action_abc" /> <category android:name="abc" /> </intent-filter> </activity> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
上面的代码Test1Activity和Test2Activity都有一个intent-filter属性,以及他下面的action子属性
然后使用下面的代码就可以获取这个应用中的有action=”action_abc”的activity了。
1 2 3 4 5 6 7 8 9 10 |
Intent intent = new Intent("action_abc"); intent.setPackage("com.zht.xxx.xxxxxxxx"); //此处为目标应用的包名 PackageManager pm = getPackageManager(); List<ResolveInfo> alist = pm.queryIntentActivities(intent, PackageManager.MATCH_ALL); for(int i = 0; i < alist.size(); i++){ Log.d(LOG_TAG, "queryIntentActivities: " + alist.get(i).activityInfo.name); Log.d(LOG_TAG, "LaunchMode: " + alist.get(i).activityInfo.launchMode); Log.d(LOG_TAG, "processName: " + alist.get(i).activityInfo.processName); } |
Sorry, the comment form is closed at this time.