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

4 篇文章 0 订阅
1 篇文章 0 订阅

私有进程

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

<?xml version="1.0"encoding="utf-8"?>

<manifest xmlns: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>

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

 

                <category android: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

<LinearLayout 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"

    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;

       }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值