android 25 跨进程启动activity

时间:2024-08-04 19:34:38

跨进程启动activity,启动系统预定义的activity就是跨进程的。

client项目启动server项目的activity。

android 25   跨进程启动activity

clientActivity.java

package com.sxt.day04_10_client;

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 Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListener();
} private void setListener() {
findViewById(R.id.btnStartACtivity).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent("com.sxt.day04_10_service.MainActivity");//要启动的actvity的java类,查找系统中所有的activity,要遍历一遍。前提是server软件安装了。设置Intent的时候只设置Action不设置category则category是用的DEFAULT,所以server处的category要加一个DEFAULT
startActivity(intent);
}
});
} }

clientActivity项目的描述文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sxt.day04_10_client"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.sxt.day04_10_client.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>
</application> </manifest>

serverActivity.java

package com.sxt.day04_10_service;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//加载xml文件
} }

serverActivity项目的描述文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sxt.day04_10_service"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.sxt.day04_10_service.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="com.sxt.day04_10_service.MainActivity"/>client项目中将会启动这个activity
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application> </manifest>