android如何调用其他应用的方法或属性

来源:互联网 发布:eclipse oxygen是java 编辑:程序博客网 时间:2024/05/22 02:23

Android中有Context的概念,有了Context就可以做很多事情,如打开activity、发送广播、打开文件夹和数据库、获取

 

classLoader、获取资源等等。

 

那么能获取到手机上其他应用的Context吗?

能!有了其他应用的Context,几乎就可以做其他应用能做的任何事。


示例:
下面这个类是手机上的某个应用
某个应用中的类代码  收藏代码
  1. package com.example;  
  2.   
  3. import android.app.Activity;  
  4. import android.util.Log;  
  5.   
  6. public class InvokedActivity extends Activity {  
  7.     public void print(String msg) {  
  8.         Log.i("IA", msg);  
  9.     }  
  10. }  
 

下面代码是我的应用,去调用上面那个类的print方法
Res/layout/main.xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <Button android:id="@+id/invoke"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="调用其它应用方法" />  
  11. </LinearLayout>  
 
Java代码  收藏代码
  1. package com.invoke;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Toast;  
  9.   
  10. public class TestActivity extends Activity {  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.main);  
  15.           
  16.         findViewById(R.id.invoke).setOnClickListener(new OnClickListener() {  
  17.             @Override  
  18.             public void onClick(View v) {  
  19.                 invokeOtherPackage();  
  20.             }  
  21.         });  
  22.     }  
  23.       
  24.     private void invokeOtherPackage() {  
  25.         try {  
  26.             /* 
  27.              CONTEXT_INCLUDE_CODE: 包含代码,可以执行此包中的代码。 
  28.              CONTEXT_IGNORE_SECURITY: 忽略安全警告,不加有此功能不能用,会得到安全警告。 
  29.              */  
  30.             Context otherPackageContext = this.createPackageContext("com.example", Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);  
  31.             if (otherPackageContext != null) {  
  32.                 //载入类  
  33.                 Class clazz = otherPackageContext.getClassLoader().loadClass("com.example.InvokedActivity");  
  34.                 if (clazz != null) {  
  35.                     //创建实例  
  36.                     Object obj = clazz.newInstance();  
  37.                     //获取并执行print方法  
  38.                     clazz.getMethod("print", String.class).invoke(obj, "Ask in TestActivity");  
  39.                 } else {  
  40.                     showMessage("没有获取到类");  
  41.                 }  
  42.             } else {  
  43.                 showMessage("没有获取到其它包上下文");  
  44.             }  
  45.         } catch (Exception e) {  
  46.             e.printStackTrace();  
  47.         }  
  48.     }  
  49.       
  50.     private void showMessage(String message) {  
  51.         Toast.makeText(this, message, Toast.LENGTH_SHORT).show();  
  52.     }  
  53. }  
 

当点击按钮时,会调用其他应用的方法(InvokedActivity的print方法),该方法输入如下


可能你在想,是不是只有当两个应用是同一个keystore签名才可以相互调用?当不同keystore签名就不能相互调用?
错,不同keystore签名一样可以调用,这个我测试过了。

不要使用此功能来破坏其他应用,厚道点为好。

摘自:http://chenfeng0104.iteye.com/blog/1164810