两个apk之间传递数据

来源:互联网 发布:那个软件看欧美剧 编辑:程序博客网 时间:2024/05/21 00:18

两个apk自检传递数据的方法有两种:

第一种方法:
从AAapk传到BBapk

AA的activity代码:

package com.example.aa;import android.os.Bundle;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.view.MotionEvent;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    @Override    public boolean onTouchEvent(MotionEvent event) {     //放入BBApk项目的包名, 需要启动的Activity的完整类名     ComponentName componentName = new ComponentName("com.example.bb", "com.example.bb.BBMainActivity");     Intent intent = new Intent();     intent.putExtra("one", "来自AA");     intent.setComponent(componentName);     startActivity(intent);     return super.onTouchEvent(event);    }}

AA项目的清单文件:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.aa"    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.aa.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>

BB项目的activity:

package com.example.bb;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.text.TextUtils;import android.widget.TextView;public class BBMainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        TextView tv_show = (TextView) findViewById(R.id.tv_show);        Intent intent = getIntent();        if(intent != null){         String value = intent.getStringExtra("one");         if(!TextUtils.isEmpty(value)){          tv_show.setText(value);         }        }    }}

BB项目的清单文件:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.bb"    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.bb.BBMainActivity"            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>

BB项目的布局文件:

<RelativeLayout xmlns: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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".BBMainActivity" >    <TextView        android:id="@+id/tv_show"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" /></RelativeLayout>

到这里就可以实现两个apk之间的数据传递,从AA到BB传递数据过去后,按返回键的时候,会出现几次BB,原因是因为在AA触摸的时候,一不小心多点触摸了,可以把AA中的触摸事件改为一个button点击事件,就不会出现这种情况了。

下载demo点击这里

好了,接下来我们来实现第二种方法,是利用隐式意图来实现两个apk之间的数据传递的。 CC把数据传递到DD;

CC的activity的代码:

package com.example.cc;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.app.Activity;import android.content.Intent;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button button = (Button) findViewById(R.id.button);        button.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                 Intent intent = new Intent("com.xin.two");                 intent.putExtra("one", "来自CC");                 startActivity(intent);                 finish();            }        });    }//  @Override//    public boolean onTouchEvent(MotionEvent event) {//       //隐式意图//       Intent intent = new Intent("com.xin.two");//       intent.putExtra("one", "来自CC");//       startActivity(intent);//       finish();//       return super.onTouchEvent(event);//      }}

CC的布局文件:

<RelativeLayout xmlns: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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" /></RelativeLayout>

CC的清单文件:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.cc"    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.cc.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>

DD的activity的代码:

package com.example.dd;import android.os.Bundle;import android.text.TextUtils;import android.view.KeyEvent;import android.widget.TextView;import android.app.Activity;import android.content.Intent;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        TextView tv_show = (TextView) findViewById(R.id.tv_show);        Intent intent = getIntent();        if(intent != null){         String value = intent.getStringExtra("one");         if(!TextUtils.isEmpty(value)){          tv_show.setText(value);         }        }    }    /**从CC触摸屏到DD,按返回键DD要消失3次,是在CC上多点触摸了,切换成点击就不存在这个问题了*/    @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        if (keyCode == KeyEvent.KEYCODE_BACK) {            finish();        }        return super.onKeyDown(keyCode, event);    }}

DD的布局文件:

<RelativeLayout xmlns: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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <TextView        android:id="@+id/tv_show"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" /></RelativeLayout>

DD的清单文件:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.dd"    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.dd.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>           <!--  隐式意图添加下面的过滤 -->            <intent-filter >                <action android:name="com.xin.two"/>                <category android:name="android.intent.category.DEFAULT"/>            </intent-filter>        </activity>    </application></manifest>

到这里利用隐式意图已经实现两个apk之间的数据传递了。

下载demo点击这里

0 0