Android 跨应用调用Activity及Service

如何调用另外一个app应用的activity或者service,本文提供一个验证可行的方法。

调用方法:

 

    Intent intent=new Intent("youActionName");  
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
    intent.addCategory(Intent.CATEGORY_DEFAULT);  
    intent.putExtra("type",inType);  //if needed   
    ComponentName cn=new ComponentName("applicationPackageName","packagename+classname");  
    intent.setComponent(cn);  
    startActivity(intent);  


 

在被调用的App里面需要定义 class (activity 或 service)属性和filter。需要明确的几点

如果不是action.Main,则需要主动申明Android:exported="true",允许外部访问

action name 要一致

category name要一致,如果调用的地方没有明确声明,被调用的地方要声明DEFAULT

 

<activity android:name=".pbap.BluetoothPbapLuancherActivity"   
            android:label="Bluetooth"  
            android:exported="true"  
            android:process="@string/process">  
    <intent-filter>                                 
        <action android:name="android.intent.action.MAIN" />  
        <category android:name="android.intent.category.DEFAULT" />   
    </intent-filter>  
</activity>  


 

 

posted @   jlins  阅读(528)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示