首页 > 学院 > 开发设计 > 正文

私有进程和全局进程的区别

2019-11-09 15:19:45
字体:
来源:转载
供稿:网友

私有进程

android:PRocess=":remote",以冒号开头,冒号后面的字符串原则上是可以随意指定的。如果我们的包名为“com.example.processtest”,则实际的进程名

为“com.example.processtest:remote”。这种设置形式表示该进程为当前应用的私有进程,其他应用的组件不可以和它跑在同一个进程中。

全局进程

进程名称不以“:”开头的进程都可以叫全局进程,如android:process="com.example.processtest.remote",以小写字母开头,表示运行在一个以这个名字命名的全局进程中,其他

应用通过设置相同的ShareUID可以和它跑在同一个进程。

Demo

com.aji.processdemo包名

project.properties文件

target=android-19

===========================================================

AndroidManifest.xml

<?xmlversion="1.0"encoding="utf-8"?>

<manifestxmlns:android="http://schemas.android.com/apk/res/android"

    package="com.aji.processdemo"

    android:versionCode="1"

    android:versionName="1.0">

 

   <uses-sdk

        android:minSdkVersion="14"

        android:targetSdkVersion="21"/>

 

   <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/APPTheme"

        android:name="com.aji.processdemo.MyApp">

        <activity

           android:name=".MainActivity"

           android:label="@string/app_name">

            <intent-filter>

                <actionandroid:name="android.intent.action.MAIN"/>

 

                <categoryandroid:name="android.intent.category.LAUNCHER"/>

            </intent-filter>

        </activity>

 

        <!-- 以:开头的名字,则表示这是一个应用程序的私有进程,私有进程的进程名称是会在冒号前自动加上包名 -->

        <service

           android:name="com.aji.processdemo.MyService01"

           android:process=":abc">

        </service>

       

        <!-- 进程名称不以“:”开头的进程都可以叫全局进程,其他应用通过设置相同的ShareUID可以和它跑在同一个进程 -->

        <service

           android:name="com.aji.processdemo.MyService02"

           android:process="com.aji.test">

        </service>

   </application>

 

</manifest>

===========================================================

activity_main.xml

<LinearLayoutxmlns: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"

    android:orientation="vertical"

    tools:context="com.aji.processdemo.MainActivity">

 

   <Button

        android:id="@+id/btn_01"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="启动私有进程"/>

 

   <Button

        android:id="@+id/btn_02"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="启动全局进程"/>

 

</LinearLayout>

===========================================================

MainActivity

package com.aji.processdemo;

 

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

 

public class MainActivity extends Activityimplements OnClickListener {

 

       @Override

       protectedvoid onCreate(Bundle savedInstanceState) {

              super.onCreate(savedInstanceState);

              setContentView(R.layout.activity_main);

              findViewById(R.id.btn_01).setOnClickListener(this);

              findViewById(R.id.btn_02).setOnClickListener(this);

       }

 

       @Override

       publicvoid onClick(View v) {

              switch(v.getId()) {

              caseR.id.btn_01:

                     Intentit1 = new Intent(MainActivity.this, MyService01.class);

                     startService(it1);

                     break;

              caseR.id.btn_02:

                     Intentit2 = new Intent(MainActivity.this, MyService02.class);

                     startService(it2);

                     break;

 

              default:

                     break;

              }

       }

}

===========================================================

MyApp.java

package com.aji.processdemo;

 

import java.util.List;

 

import android.app.ActivityManager;

importandroid.app.ActivityManager.RunningAppProcessInfo;

import android.app.Application;

import android.content.Context;

import android.util.Log;

 

public class MyApp extends Application {

 

       @Override

       publicvoid onCreate() {

              super.onCreate();

              //获取进程Id

              intpid = android.os.Process.myPid();

              //根据进程id获取进程名称

              //主进程的名称就是应用包名,私有进程的名称会在冒号前面加上包名,全局进程名称就是android:process设置的内容

              StringpName = getProcessName(this, pid);

              Log.e("m_tag","MyApp onCreate pid:" + pid + " pName:" + pName);

       }

 

       /**

        * 根据进程id获取进程名称

        *

        * @param cxt

        * @param pid

        * @return

        */

       publicString getProcessName(Context cxt, int pid) {

              ActivityManageram = (ActivityManager) cxt

                            .getSystemService(Context.ACTIVITY_SERVICE);

              List<RunningAppProcessInfo>runningApps = am.getRunningAppProcesses();

              if(runningApps == null) {

                     returnnull;

              }

              for(RunningAppProcessInfo procInfo : runningApps) {

                     if(procInfo.pid == pid) {

                            returnprocInfo.processName;

                     }

              }

              returnnull;

       }

}

===========================================================

MyService01 私有进程

package com.aji.processdemo;

 

import android.app.Service;

import android.content.Intent;

import android.os.IBinder;

import android.util.Log;

 

public class MyService01 extends Service {

       @Override

       publicIBinder onBind(Intent intent) {

              returnnull;

       }

      

       @Override

       publicint onStartCommand(Intent intent, int flags, int startId) {

              //TODO Auto-generated method stub

              Log.e("m_tag","MyService01==onStartCommand===>"+intent.toString());

              returnSTART_STICKY;

       }

}

===========================================================

MyService02 全局进程

package com.aji.processdemo;

 

import android.app.Service;

import android.content.Intent;

import android.os.IBinder;

import android.util.Log;

 

public class MyService02 extends Service {

       @Override

       publicIBinder onBind(Intent intent) {

              returnnull;

       }

 

       @Override

       publicint onStartCommand(Intent intent, int flags, int startId) {

              Log.e("m_tag","MyService02====onStartCommand===>"+intent.toString());

              returnSTART_STICKY;

       }

}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表