android四个组件的跨进程通信

Android四大组件(Activity,service,broadcast,Content Provider)跨进程通信相信在android项目中进程用到,此处将一一做以说明以及总结.

 

1.Activity:界面显示组件,最终extends Context,且实现了很多Ui交互接口.

  (1)不同app跨进程调用:OneMainActivity调用AnotherMainActivity.下面只做简单说明.需要运行验证的可以copy代码进行运行校验.

   

package com.example.oneactivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class OneMainActivity extends Activity {
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one_main);
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setAction("com.ysfl.otherActivity");//此处设置需要起动app的action.
                Log.d(this.getClass().getName(),"I'm OneMainActivity onclick to start anotherActivity");
                startActivity(intent);//跨进程调用其它应用
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_one_main, menu);
        return true;
    }
    @Override
    protected void onPause(){
        super.onPause();
        Log.d(this.getClass().getName(),"I'm OneMainActivity  onPause");
    }
    @Override
    protected void onStop(){
        super.onStop();
        Log.d(this.getClass().getName(),"I'm OneMainActivity  onStop");
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
        Log.d(this.getClass().getName(),"I'm OneMainActivity  onDestroy");
    }
}

 OneMainActivity的xml文件不做说明,它只是一个简单的配置.AnotherMainActivity的代码和配置文件如下.

package com.example.anotheractivity;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class AnotherMainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_another_main);
        Log.d(this.getClass().getName(),"I'm AnotherMainActivity  onCreate");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_another_main, menu);
        return true;
    }
    @Override
    protected void onPause(){
        super.onPause();
        Log.d(this.getClass().getName(),"I'm AnotherMainActivity  onPause");
    }
    @Override
    protected void onStop(){
        super.onStop();
        Log.d(this.getClass().getName(),"I'm AnotherMainActivity  onStop");
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
        Log.d(this.getClass().getName(),"I'm AnotherMainActivity  onDestroy");
    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.anotheractivity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.anotheractivity.AnotherMainActivity"
            android:label="@string/app_name" >
            <intent-filter>
<!-- 此处的action是其它应用需要起动的action -->
<action android:name="com.ysfl.otherActivity" /> <category android:name="android.intent.category.DEFAULT" /> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

运行结果如下图:

后续待续..........

 

猜你喜欢

转载自www.cnblogs.com/syyh2006/p/9187676.html