To explain how the Android activity switches between data transfer _android

Source: Internet
Author: User
Tags switches

I've written a similar tool for the Android system's cropped image. The feature was largely implemented, but left a call question: How do I invoke the cropping tool from my program and get the cropped picture?

In fact, this is also very simple, is the basic use of intent.

First image (the interface is still not optimized, unsightly ugly bar):

Start activity, open Picture Selection window, choose a picture randomly

Here is the jump to the cropping interface

Press the Crop button, exit the activity, return to the original interface, and show the cropped figure

The process is like this, also calculates the system to simulate the whole process of cutting function. Here is the key code and description for implementing the functionality.

This first calls the main program a, and the subroutine of the call is B.

B is the program that you write, calling him needs to customize his activity Action. The system action corresponds to an action string, such as some action constants defined in the intent class:

public static final String Action_main = "Android.intent.action.MAIN";
public static final String Action_view = "Android.intent.action.VIEW";
public static final String Action_edit = "Android.intent.action.EDIT";
public static final String Action_call = "Android.intent.action.CALL";

In the intent method, there are some methods such as: Intent.setclass (Activity.this, X.class); But I don't like this way very much.
In addition to the common construction method is

Public Intent (String action);
Public Intent (String action, Uri URI);

The intent object that uses String,uri This parameter type is called an implicit intent object, that is, the method of constructing the intent class does not specify which activity the target of intent is. These goals depend on the configuration information in the Androidmanifest.xml file to determine. That is, the action may mean more than one target, or a Androidmanifest.xml file can be configured with more than one activity action that receives the same action.

In Androidmanifest.xml, the general file format is as follows:

<application
android:allowbackup= "true"
android:icon= "@drawable/ic_launcher"
android:label= "@ String/app_name "
android:theme=" @style/apptheme ">
<activity
android:name=" Com.example.crop_ Image_my. 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>

Where the <action> tag specifies a system-defined activity action. The action indicates that the first activity that starts when the application starts needs to receive this action. That means the action is the Android app that launches the main window.

Because I need my own action to start the clipping program, I added the following paragraph in the XML above:

<intent-filter>
<action android:name= "Com.example.crop_image_my.copper"/>
<data Android : scheme= "crop"/>
<category android:name= "Android.intent.category.DEFAULT"/>

Keep. Main is I need to run the program alone also can (do not know how to delete the words will be, no test)

The above <data> tag defines a scheme, so the following URI can be written crop://something

Oh yes, the above is B's configuration file, a to start B.

The intent of B can be invoked below in a:

private void Startmycropintent (String path) throws FileNotFoundException {
Intent Intent = new Intent (" Com.example.crop_image_my.copper ", Uri.parse (" crop://"+ Path));
Startactivityforresult (Intent,);

The above constructs the intent not to say, the parameter is in the previous configuration file. To start a new activity, you can use StartActivity (), but in order to get the return value, the Startactivityforresult () method is used to process the Onactivityresult ().

The second parameter, 12, is the request code, which corresponds to the first parameter in onactivityresult (int requestcode,int resultcode, Intent data). This number can be casually written, but the proposed use of resources to write, such as a button trigger Startactivityforresult (), you can put this button r.id.button1 when the request code (in fact, with anything, as long as easy to identify).

OK, after starting the activity, note that the parameter has a URI, which is the path to the picture I chose, and in the cropping method B, get the URI:

if (Getintent (). GetData ()!= null) {Imgpath = Getintent (). GetData ()
. GetPath ();//See the composition of the URI data
log.v ("<dbw > ", Imgpath);
Mycropview.setbmppath (Imgpath);

It is written in the OnCreate and is used in the construction of this value.

The following is the shutdown activity. Write in B. Just put the code.

@Override public
void OnClick (View v) {
//TODO auto-generated a stub
switch (V.getid ()) {case
r.id . Btn_crop:
Bitmap croppedimage = Mycropview.getcroppedimage ();
Croppedimageview.setimagebitmap (croppedimage);
Savecroppedimage (croppedimage);
Return to the last activity
log.v ("<DBW>", Newfilepath);
Getintent (). Putextra ("NewPath", Newfilepath);
Setresult (, Getintent ());
break;
Case R.id.btn_cancel:
setresult ();
break;
Default: Break
;
}
Finish ();

After cropping, save the picture and then use the Getintent () Putextra () method to place the picture path in the intent. "NewPath" is a random name, as the identification of the data. Finish () is to close this activity. Parameter 20 is the second parameter of onactivityresult (int requestcode,int resultcode, Intent data).

Finally gets the truncated picture, which is written in a:

@Override
protected void onactivityresult (int requestcode, int resultcode, Intent data) {
Super.onactivityresult (Requestcode, ResultCode, data);
Switch (requestcode) {case
:
if (ResultCode = =) {
String path = Data.getextras (). getString ("NewPath");
LOG.V ("<DBW>", "get------" + path);
Bitmap bmp = Bitmapfactory.decodefile (path);
Iv.setimagebitmap (BMP);
else if (ResultCode = =) {
Toast Toast = Toast.maketext (This, "You canceled the operation", Toast.length_long);
Toast.show ();
}
break;
Default: Break
;
}

The switch is for different activity (currently only starts one, the identification code is 12). Then the different resultcode to do different processing.

Before using the Putextra to set up the data, the Data.getextra method is used to get the bundle object, and to obtain different data according to the GetXXX method.

This is the process.

4.28.2015

Another simple method:

2 Activity,eclipse projects built on the same project right-click->new->others->android->android activity

Manifest.xml:

<application
android:allowbackup= "true"
android:icon= "@drawable/ic_launcher"
android:label= "@ String/app_name "
android:theme=" @style/apptheme ">
<activity boot display
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 switching
android:name= ". ShowMessage "
android:label=" @string/title_activity_display_message "
android:parentactivityname=". Mainactivity ">
<meta-data
android:name=" Android.support.PARENT_ACTIVITY "
android:value=" Com.example.android_switchactivity. Mainactivity "/>
</activity>

Add in Mainactivity:

Public final static String extra_message = "com.example.android_switchactivity." Message "; This string can be assigned any value, but to ensure that the unique intent intent = new Intent (this, showmessage.class);
EditText mEt = (edittext) Findviewbyid (r.id.edit_message);
String message = Met.gettext (). toString ();
Intent.putextra (extra_message, message);

In the 2nd activity add:

Intent Intent = Getintent ();
String message = Intent.getstringextra (mainactivity.extra_message);
TextView mTv = new TextView (this);
Mtv.settextsize (40);


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.