android in practice_Using Intents(GoodShares project)

You need to send a request with data to another application, or you need to send a response 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 other application 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(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//ACTION_GET_CONTENT:a standard intent
				Intent 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 flow
				startActivityForResult(request,0);
			}
        	
        });
       
       Button button1=(Button)findViewById(R.id.btn1);
       button1.setOnClickListener(new OnClickListener(){
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent request=new Intent("example.mash.Action");
			request.addCategory(Intent.CATEGORY_DEFAULT);
			request.putExtra("example.mash.EXTRA_PHOTO", photoUri0);
			//launch separate app,give up control flow
			startActivityForResult(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.ACTION action 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(){
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					try{
						//load image into memory
						Bitmap 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 back
						MashActivity.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 the Activity 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 of parameters that are expected for inputs and uses for outputs.
your app surrendered control flow to another application and waited until the user finished interacting with that application.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值