Android中单APK应用多进程

本人博客原文

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

但是需要注意进程间内存的 不可见性
实例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也位于不同的内存地址空间,自然也不受影响。

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net

猜你喜欢

转载自www.cnblogs.com/siwnchh/p/10464318.html
今日推荐