通过SharedUser id,拥有同一个User id的多个APK可以配置成运行在同一个进程中.所以默认就是可以互相访问任意数据.也可以配置成运行成不同的进程,同时可以访问其他APK的数据目录下的数据库和文件.就像访问本程序的数据一样.
Test4的AndroidManifest.xml。 android:sharedUserId="com.example"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test4.w00425655.test4"
android:sharedUserId="com.example">
<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=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Test5的AndroidManifest.xml。android:sharedUserId="com.example"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test5.w00425655.test5"
android:sharedUserId="com.example">
<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=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Test5的MainActivity.java
package com.test5.w00425655.test5;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
SharedPreferences sharedPreferences = getSharedPreferences("shared",0);
switch (item.getItemId()) {
case R.id.navigation_home:
try {
//获取UID,验证是否相同
PackageManager pm = getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo("com.test5.w00425655.test5", 0);
Toast.makeText(MainActivity.this, Integer.toString(ai.uid,10), Toast.LENGTH_SHORT).show();
mTextMessage.setText(Integer.toString(ai.uid,10));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
//供Test4调用的静态方法
public static String getStr(String str){
return str + "Test5getStr";
}
//供Test4调用的非静态方法
public String getString(String str){
return str + "Test5getString";
}
}
Test4的MainActivity.java
package com.test4.w00425655.test4;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final TextView mTextview = (TextView) findViewById(R.id.textview);
try {
//获取UID,验证是否相同
PackageManager pm = getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo("com.test4.w00425655.test4", 0);
Toast.makeText(MainActivity.this, Integer.toString(ai.uid,10), Toast.LENGTH_SHORT).show();
mTextview.setText(Integer.toString(ai.uid,10));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
Context context = null;
try {
context = this.createPackageContext("com.test5.w00425655.test5",CONTEXT_INCLUDE_CODE|Context.CONTEXT_IGNORE_SECURITY);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
final Context finalContext = context;
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Class clazz = null;
Object object = null;
try {
//加载Test4的类和函数
clazz = finalContext.getClassLoader().loadClass("com.test5.w00425655.test5.MainActivity");
object = clazz.newInstance();
Method method = clazz.getDeclaredMethod("getString",String.class);
mTextview.setText((String)method.invoke(object,"cccccccccc"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
注:
Context.CONTEXT_INCLUDE_CODE:
把参数包名对应的应用中的代码包含到Context中,这样当前应用才能访问context应用中的类,如果context对应的应用不能安全的加载到当前应用中
,会报出SecurityException,如果想要加载另一个应用,访问它的数据,则这个标记必须设置
CONTEXT_IGNORE_SECURITY:忽略加载context对应的应用这个过程中的
安全限制,总是允许加载,和CONTEXT_INCLUDE_CODE配合使用,代表不管是否安全,都要加载,一般使用要小心。
最终验证OK,Test4可以成功调用Test5的静态和费静态方法。