You need to send a request with data to another application, or you need to send aresponse to a request from another application. The application making the request

will cede control to another application in order to accomplish this task.

In this case,we need another application to do something for us and we’re willing to allow the otherapplication to take control of the user interface to do this.



create GoodShares app

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="example.goodshares"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="15" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".ShareActivity"            android:label="@string/title_activity_share" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

create layout share.xml

<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <LinearLayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:orientation="vertical">    <Button         android:layout_width="match_parent"         android:layout_height="wrap_content"        android:text="@string/button0"         android:id="@+id/btn0" />    <ImageView         android:layout_width="match_parent"         android:layout_height="wrap_content"        android:id="@+id/pic0" />          <Button         android:layout_width="match_parent"         android:layout_height="wrap_content"        android:text="@string/button1"         android:id="@+id/btn1" />      <ImageView         android:layout_width="match_parent"         android:layout_height="wrap_content"        android:id="@+id/pic1" />          <TextView         android:layout_width="match_parent"         android:layout_height="wrap_content"        android:id="@+id/text0" />    <Button         android:id="@+id/next"         android:layout_width="match_parent"         android:layout_height="wrap_content"        android:text="@string/next" />      </LinearLayout></ScrollView>

create ShareActivity class:

public class ShareActivity extends Activity {Uri  photoUri0;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.share);                Button button=(Button)findViewById(R.id.btn0);        button.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stub//ACTION_GET_CONTENT:a standard intentIntent request=new Intent(Intent.ACTION_GET_CONTENT);request.setType("image/*");//cause the intent to be routed to the Gallery//launch separate app,give up control flowstartActivityForResult(request,0);}                });              Button button1=(Button)findViewById(R.id.btn1);       button1.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent request=new Intent("example.mash.Action");request.addCategory(Intent.CATEGORY_DEFAULT);request.putExtra("example.mash.EXTRA_PHOTO", photoUri0);//launch separate app,give up control flowstartActivityForResult(request,1);}         });    }        protected void onActivityResult(int requestCode, int resultCode, Intent data){    if(requestCode==0){    //retrieve data sent by Gallery    photoUri0=(Uri) data.getParcelableExtra(Intent.EXTRA_STREAM);    if(photoUri0==null&&data.getData()!=null){    photoUri0=data.getData();    }    ImageView imgView0=(ImageView)findViewById(R.id.pic0);    imgView0.setImageURI(photoUri0);     }else if(requestCode==1){    //retrieve data sent by Mash app    Uri photoUri1=(Uri)data.getParcelableExtra("example.mash.EXTRA_RESULT");    if(photoUri1==null&&data.getData()!=null){    photoUri1=data.getData();    }    ImageView imgView1=(ImageView)findViewById(R.id.pic1);    imgView1.setImageURI(photoUri1);    }    }    }

create ImageMash app:

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="example.mash"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="15" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MashActivity"            android:label="@string/title_activity_image_mash" >            <intent-filter>                <action android:name="example.mash.ACTION" />                <category android:name="android.intent.category.DEFAULT" />            </intent-filter>        </activity>    </application></manifest>
must use the example.mash.ACTIONaction in an Intent
create layout file main.xml:

<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" ><ImageView     android:id="@+id/image"/>  <Button       android:id="@+id/button"      android:text="mash"/></RelativeLayout>
create MashActivity class

public class MashActivity extends Activity {public static final String EXTRA_PHOTO ="example.mash.EXTRA_PHOTO";private static final int RESULT_ERROR = 99;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);          Intent request=getIntent();          if(request!=null &&request.hasExtra(EXTRA_PHOTO)){          //get uri of inbound image          final Uri uri=(Uri)request.getParcelableExtra(EXTRA_PHOTO);          ImageView image=(ImageView)findViewById(R.id.image);          image.setImageURI(uri);          Button button=(Button)findViewById(R.id.button);          button.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubtry{//load image into memoryBitmap bmp=BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));Bitmap mashed=mash(bmp);Uri resultUri=saveImage(mashed);Intent response=new Intent();response.putExtra("example.mash.EXTRA_RESULT", resultUri);//set result,pass intent backMashActivity.this.setResult(Activity.RESULT_OK,response);}catch(FileNotFoundException e){Log.e("MashActivity", "Exception mashing pic", e);MashActivity.this.setResult(RESULT_ERROR);}finish();//release control}                    });          }    }    protected Bitmap mash(Bitmap bmap){    //处理操作省略...    return bmap;    }        protected Uri saveImage(Bitmap bmap){    //处理操作省略    return null;    }}

You don’t need to know the name or class of theActivity that you want to integrate with; you need to know the name of the action(and optionally its category.) You may also need to know the names and types ofparameters that are expected for inputs and uses for outputs.
your app surrendered controlflow to another application and waited until the user finished interacting withthat application.


更多相关文章

  1. Android——基于ConstraintLayout实现的可拖拽位置控件
  2. Android集成ShareSDK第三方分享和登录
  3. Android(安卓)Configuration change引发的问题及解决方法
  4. 关于在内部类中启动一个Android(安卓)Intent的疑惑
  5. Android(安卓)GPRS的自动打开与关闭
  6. Android(安卓)使用ORMLite操作数据库
  7. Android(安卓)NDK学习笔记6-JNI对引用数据类型的操作
  8. Android(安卓)RxJava操作符详解系列: 创建操作符
  9. Android(安卓)app项目开发步骤总结

随机推荐

  1. Android(安卓)将TabHost放在最下方显示
  2. Announcing the 2016 Android(安卓)Exper
  3. android studio 生成 release aar
  4. 关于Android/java的复杂对象的深拷贝和浅
  5. android 休眠
  6. Android电源管理,低电量报警
  7. android 图片的压缩
  8. Android(安卓)简单数据库(增删改查)
  9. android 注册、登录实现
  10. android进度条对话框