使用Intent方式进行跨进程访问

直接上代码啦

(1)这里我用一个按钮触发事件

@Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
    }  
  
    @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_main, menu);  
          
        Button button1 = (Button) findViewById(R.id.button1);  
        button1.setOnClickListener(new OnClickListener() {  
  
            @Override  
            public void onClick(View v) {  
                method1();  
            }  
        });  
        return true;  
    }  
  
    /* 
     * 1.利用compnent方式访问其他apk的(主activity),无需设置androidmanifest.xml 
     */  
    public void method1() {  
  
        // 这些代码是启动另外的一个应用程序的主Activity  
        ComponentName componetName = new ComponentName(  
                // 这个是另外一个应用程序的包名 ,androidmanifest.xml中的package值!!!  
                "com.amaker.test",  
                // 这个参数是要启动的Activity (主activity)  
                "com.amaker.test.MainActivity");  
          
        try {  
            Intent intent = new Intent();  
            intent.setComponent(componetName);  
            startActivity(intent);  
        } catch (Exception e) {  
            Toast.makeText(getApplicationContext(),"没有找到应用程序!!!", 0).show();  
        }  
    }  
  
    /* 
     * 2.利用compnent方式访问其他apk的(非主activity),需要设置androidmanifest。xml中非主activity的exported属性,具体如下: 
     *   被访问的activity是"com.amaker.ch03.drawable.TestBitmapActivity" 则需要设置的是:<activity android:name="com.amaker.ch03.drawable.TestBitmapActivity"  android:exported="true"/> 
     */  
    public void method2() {  
        Intent intent = new Intent();  
        intent.setComponent(new ComponentName("com.amaker.test","com.amaker.ch03.drawable.TestBitmapActivity"));  
                //"com.amaker.ch03.string.TestStringActivity"));  
        //intent.setAction("android.intent.action.VIEW");  
        startActivity(intent);  
    }  
  
    /* 
     * 3.设置androidmanifest.xml访问其他apk的(非主activity) 
     * 注意:使用这种方式的时候需要在被调用的apk的androidmanifest.xml里做如下设置(访问非主activity) <activity 
     * android:name="com.amaker.ch03.xml.TestXmlActivity"> <intent-filter> 
     * <action android:name="android.intent.action.xml" /> <data 
     * android:scheme="info" /> <category 
     * android:name="android.intent.category.DEFAULT" /> </intent-filter> 
     * </activity> 
     */  
    public void method3() {  
        Intent intent = new Intent("android.intent.action.xml",  
                Uri.parse("info://调用其他应用程序的Activity"));  
        startActivity(intent);  
    }  

2)被调用的工程之所以可以被Demo调用,是因为AndroidManifest.xml被做了修改。具体代码如下:

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
      package="com.amaker.test"  
      android:versionCode="1"  
      android:versionName="1.0">  
    <application android:icon="@drawable/icon" android:label="@string/app_name">  
        <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>  
          
        <activity android:name="com.amaker.ch03.color.TestColorActivity">  
           <action android:name="android.intent.action.color" />  
           <category android:name="android.intent.category.DEFAULT" />  
        </activity>    
          
        <activity android:name="com.amaker.ch03.string.TestStringActivity" android:exported="true"/>  
          
        <activity android:name="com.amaker.ch03.dimen.TestDimensionActivity"/>  
          
          
        <activity android:name="com.amaker.ch03.xml.TestXmlActivity">  
            <intent-filter>          
                <action android:name="android.intent.action.xml" />  
                <data android:scheme="info" />  
                <category android:name="android.intent.category.DEFAULT" />  
            </intent-filter>  
        </activity>   
          
      
        <activity android:name="com.amaker.ch03.drawable.TestBitmapActivity"  android:exported="true"/>     
          
        <activity android:name="com.amaker.ch03.layout.TestLayoutActivity"/>    
        <activity android:name="com.amaker.ch03.menu.TestMenuActivity"/>  
    </application>  
    <uses-sdk android:minSdkVersion="3" />  
  
</manifest> </span>
做了一个小总结,希望能给程序宝宝们提供一个便利,哈哈!!!


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Android 中,进程通信(IPC,Interprocess Communication)是指不同进程之间的通信。Android 中主要有以下几种进程通信方式: 1. Intent:通过 Intent 可以在不同应用程序之间传递消息和数据。通过发送和接收 Intent,不同进程之间可以实现简单的通信。 2. AIDL:AIDL(Android Interface Definition Language)是一种用于定义 Android 进程间通信接口的语言。通过 AIDL,一个应用程序可以将一个接口暴露给其他应用程序使用,从而实现进程通信。 3. ContentProvider:ContentProvider 是 Android 提供的一种用于共享数据的组件。通过 ContentProvider,一个应用程序可以将数据存储在 SQLite 数据库、文件系统或网络上,并且其他应用程序可以通过 ContentResolver API 访问这些数据。 4. Messenger:Messenger 是一种基于 AIDL 的 IPC 机制,它允许不同进程之间传递 Message 对象。通过 Messenger,一个进程可以向另一个进程发送消息,并且可以通过 Handler 处理接收到的消息。 5. Socket:Socket 是一种传输层协议,可以在不同主机之间进行通信。在 Android 中,一个应用程序可以通过 Socket 进行进程通信,例如使用 HTTP 协议进行网络通信。 需要注意的是,进程通信可能会带来额外的开销和安全问题,因此应该谨慎使用。在使用进程通信时,需要确保数据的安全性和正确性,并避免出现死锁、死循环等问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值