Android 组件复用和进程关系

一:跨进程调用Activity

案例设计, 设计两个Application, 第一个Application1将调用第二个Application中的Activity2, 同时第二个Application2会调用自身的Activity2. 以此判断进程ID来观察进程的存在情况, 详细说明和代码如下:

Application1: ClientApk 这个程序有一个ClientMainActivity会发起对应用二的一个Activity(名称为ServerActivity)的调用, ClientMainActivity启动时, 将进程的id打印出来

public class ClientMainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv = (TextView)findViewById(R.id.ClientTV);
        ClickAction l = new ClickAction();
        tv.setOnClickListener(l);
        int myProcessID = Process.myPid();
        //打印当前进程的ID
        Log.i("---ClientMainActivity----", myProcessID + "");
    }
   
    class ClickAction implements OnClickListener{
  public void onClick(View v) {
   Intent intent = new Intent("com.ostrichmyself.tiantian");
   //ComponetName不描述清楚的话, 就无法正确的找到ServerActivity, 但系统提供的Activity,如main都可以为什么?
    final ComponentName cn = new ComponentName("com.ostrichmyself","com.ostrichmyself.ServerActivity");
       intent.setComponent(cn);
       startActivity(intent);
   
  }
     
    }
}

Application2: ServerApk 这个程序有两个Activity, 其一为ServerMainActivity, 其二为ServerActivity, 点击ServerMainActivity, 会对ServerActivity进行调用.  ServerActivity 即被调用, 这两个Activity启动的时候, 进程号也会打印出来.

package com.ostrichmyself;
import Android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.Process;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class ServerMainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv = (TextView)findViewById(R.id.mainTV);
        ClickAction l = new ClickAction();
        tv.setOnClickListener(l);
        int myProcessID = Process.myPid();
        Log.i("---ServerMainActivity----", myProcessID + "");
    }
   
    class ClickAction implements OnClickListener{
  public void onClick(View v) {
   Intent intent = new Intent("com.ostrichmyself.tiantian");
    final ComponentName cn = new ComponentName("com.ostrichmyself","com.ostrichmyself.ServerActivity");
       intent.setComponent(cn);
       startActivity(intent);
   
  }
     
    }
}

-----------------------------------------------------

package com.ostrichmyself;
import android.app.Activity;
import android.os.Process;
import android.os.Bundle;
import android.util.Log;
/**
 * 这个将被自身和外部调用
 */
public class ServerActivity extends Activity {
 
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.server_gui);
        int myProcessID = Process.myPid();
        Log.i("---ServerActivity----", myProcessID + "");
    }
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwssxx.html