私有進程
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"/>
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;
}
}
新聞熱點
疑難解答