Android中单APK应用多进程

本人博客原文

Android中,默认一个APK包就对应一个进程,其进程名就为AndroidManifest.xml文件中 指定的package名。我们可以通过Activity, Service, BroadCastReceiver, ContentProvider的android:process属性来实现单APK多进程<wbr><br></wbr>

但是需要注意进程间内存的 不可见性
实例1
文件1 MainActivity.java
 
 

package com . lenovo . robin . test ;
import android . os . Bundle ;
import android . app . Activity ;
import android . content . Intent ;
import android . util . Log ;
import android . view . Menu ;
import android . view . MenuItem ;
import android . support . v4 . app . NavUtils ;
public class MainActivity extends Activity {
static boolean iscreated = false ;
final static String TAG = "robin" ;
@Override
public void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
setContentView ( R . layout . activity_main );
iscreated = true ;
Log . i ( TAG , "on create() in MainActivity" );
this . startService ( new Intent ( this , MyService . class ));
}
}

文件2 MyService.java
 
 

package com . lenovo . robin . test ;
import android . app . Service ;
import android . content . Intent ;
import android . os . IBinder ;
import android . util . Log ;
public class MyService extends Service {
String tag = "robin" ;
@Override
public void onCreate () {
Log . i ( tag , "MyService is oncreate" );
}
@Override
public int onStartCommand ( Intent intent , int flags , int startId ) {
Log . i ( tag , "MainActivity is created: " + MainActivity . iscreated );
return START_STICKY ;
}
@Override
public void onDestroy () {
Log . i ( tag , "OnDestory" );
}
@Override
public IBinder onBind ( Intent arg0 ) {
return null ;
}
}

文件 3 layout\activity_main.xml
 
 

<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent" >
<TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_centerHorizontal = "true"
android:layout_centerVertical = "true"
android:text = "@string/hello_world"
tools:context = ".MainActivity" />
</RelativeLayout>

文件4 AndroidManifest.xml
 
 

<manifest xmlns:android = "http://schemas.android.com/apk/res/android"
package = "com.lenovo.robin.test"
android:versionCode = "1"
android:versionName = "1.0" >
<uses-sdk
android:minSdkVersion = "8"
android:targetSdkVersion = "15" />
<application
android:icon = "@drawable/ic_launcher"
android:label = "@string/app_name"
android:theme = "@style/AppTheme" >
<activity
android:name = ".MainActivity"
android:label = "@string/title_activity_main" >
<intent-filter>
<action android:name = "android.intent.action.MAIN" />
<category android:name = "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name = ".MyService"
android:label = "@string/title_activity_main" >
</service>
</application>
</manifest>

文件5 values\strings.xml
 
 

<resources>
<string name = "app_name" > Test </string>
<string name = "hello_world" > Hello world! </string>
<string name = "menu_settings" > Settings </string>
<string name = "title_activity_main" > MainActivity </string>
</resources>

运行该应用程序
DDMS截图显示的进程如下:
Android中如何实现单APK多进程 - hubingforever - 民主与科学
运行打印出的日志如下:
08-12 21:55:32.365: I/robin(25964): on create() in MainActivity
08-12 21:55:32.435: I/robin(25964): MyService is oncreate
08-12 21:55:32.435: I/robin(25964): MainActivity is created: true
我们对 AndroidManifest.xml文件进行修改,以实现当APK应用多进程。
修改后的 AndroidManifest.xml文件如下
 
 

<manifest xmlns:android = "http://schemas.android.com/apk/res/android"
package = "com.lenovo.robin.test"
android:versionCode = "1"
android:versionName = "1.0" >
<uses-sdk
android:minSdkVersion = "8"
android:targetSdkVersion = "15" />
<application
android:icon = "@drawable/ic_launcher"
android:label = "@string/app_name"
android:theme = "@style/AppTheme" >
<activity
android:name = ".MainActivity"
android:label = "@string/title_activity_main" >
<intent-filter>
<action android:name = "android.intent.action.MAIN" />
<category android:name = "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name = ".MyService"
android:label = "@string/title_activity_main"
android:process = "com.lenove.robin.test.service" >
</service>
</application>
</manifest>

DDMS截图显示的进程如下:
Android中如何实现单APK多进程 - hubingforever - 民主与科学
运行打印出的日志如下:
08-11 06:58:56.917: I/robin(10561): on create() in MainActivity
08-11 06:58:57.037: I/robin(10572): MyService is oncreate
08-11 06:58:57.037: I/robin(10572): MainActivity is created: false
虽然我们在MainActivity的onCreate中把iscreated变量设置为了true,因为进程间内存的不可见性,,所以才会打印日志“ MainActivity is created: false”。
简单点说就是 每个进程都是运行在不同的虚拟机上,对于不同的进程,他们载入的Class文件虽然名字一样(比如都是 com.lenovo.robin.test.MainActivity ),但是他们其实是加载到了不同的内存地址空间。
com.lenovo.robin.test.MainActivity 中把 iscreated 变量设置为true,它其实只是把当前进程( com.lenovo.robin.test )的MainActivity类的iscreated变量的设置为了true,
com.lenove.robin.test.service 进程中的 com.lenovo.robin.test.MainActivity 类和它位于不同的内存地址空间,当然其变量iscreated也位于不同的内存地址空间,自然也不受影响。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值