你好,游客 登录 注册 搜索
背景:
阅读新闻

Android 跨应用调用Activity

[日期:2012-09-13] 来源:Linux社区  作者:ouyangliping [字体: ]

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

调用方法:

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

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

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

action name 要一致

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

  1. <activity android:name=".pbap.BluetoothPbapLuancherActivity"   
  2.             android:label="Bluetooth"  
  3.             android:exported="true"  
  4.             android:process="@string/process">  
  5.     <intent-filter>                                 
  6.         <action android:name="android.intent.action.MAIN" />  
  7.         <category android:name="android.intent.category.DEFAULT" />   
  8.     </intent-filter>  
  9. </activity>  
linux
相关资讯       Android开发入门 
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 匿名 发表于 2012/10/22 8:31:00
写的很好,终于知道之前的程序为什么出错啦。