Android Study Notes (5th): Next to intent (5th)-some practices

Source: Internet
Author: User

The Android UI framework requires users to divide their apps into activities for scheduling through itent. One of the main activities is called by the Android launcher on the desktop. For example, for a calendar application, you need to view the calendar activity, the activity of a single event, and the activity of editing the event. In the activity for viewing the calendar, if you select an event, you need to view the event activity to process it. This is the latest app UI framework. This time, we will learn how to use intent.

Relationship between activities

In some business logic, activity1 needs to know whether the started activity2 is over, and activity2 is the subactivity of activity1.

In other business logic, activity1 does not need to know the running status of activity2 triggered by it. For example, to open an attachment in an email, activity1 does not need to know whether the attachment activity2 is finished. In this case, the two activities are not a master-slave relationship, but a peer relationship, and activity2 is a regular activity.

Step 1: Make the intent

Intent encapsulates a request to Android so that other activities or intent referers know how to handle it. If intent needs to launch the activity we write, it can be easily implemented by a precisely specified intent, for example:

New Intent (this, HelpActivity. class );

Among them, HelpActivity. class is the component we need to launch. This activity only needs to be defined in the AndroidManifest. xml file, but no inter filter is required, because it is directly requested. If we need to add some data, namely Uri, to request a common activity, as follows:

Uri uri = Uri. parse ("geo:" + lat. toString () + "," + lon. toString ());
Intent I = new Intent (Intent. ACTION_VIEW, uri );

Step 2: Make the call

IntentStartActivity ()OrStartActivityForResult ()To launch another activity. The latter needs to know whether activity2 is running and the running result.

StartActivityForResult () will pass a number to the intent, which is unique for the calling activity. Used to identify the activity. The onActivityResult () is triggered in the calling activity after the started activity is run. We can use this unique number to identify which activity has ended. We will also get the following information:

  1. Result code, which is set by setResult () for the initiated activity, generally RESULT_ OK and RESULT_CANCELLED. Of course, we can also define our own return values (defined from RESULT_FIRST_USER)
  2. Optional String, used to pass some returned information. For example, a URL is used to express a resource. For example, data of the user selected content is returned in ACTION_PICK.
  3. Optional Bundle to return the required information

Example 1: a simple call

In this example, enter the longitude and latitude, and use intent to apply the launch map. The layout file is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout...>
<TableLayout android: layout_width = "fill_parent" android: layout_height = "wrap_content"
Android: stretchColumns = "1, 2">
<TableRow>
<TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content"
Android: paddingLeft = "2dip" android: paddingRight = "4dip"
Android: text = "Location:"/>
<EditText android: id = "@ + id/c18_lat"
Android: layout_width = "fill_parent" android: layout_height = "wrap_content"
Android: cursorVisible = "true"
Android: editable = "true"
Android: singleLine = "true"
Android: layout_weight = "1"/>
<EditText android: id = "@ + id/c18_lon"
Android: layout_width = "fill_parent" android: layout_height = "wrap_content"
Android: cursorVisible = "true"
Android: editable = "true"
Android: singleLine = "true"
Android: layout_weight = "1"/>
</TableRow>
</TableLayout>
<Button android: id = "@ + id/c18_map"
Android: layout_width = "fill_parent" android: layout_height = "wrap_content"
Android: text = "Show Me! "/>
</LinearLayout>

The source code is as follows:

Public class Chapter18Test1 extends Activity {
Private EditText latitude = null;
Private EditText longtitude = null;

 

Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. chapter_18_test1 );

Latitude = (EditText) findViewById (R. id. c18_lat );
Longtitude = (EditText) findViewById (R. id. c18_lon );
Button button = (Button) findViewById (R. id. c18_map );
Button. setOnClickListener (new View. OnClickListener (){
Public void onClick (View v ){
String _ lat = latitude. getText (). toString ();
String _ lon = longtitude. getText (). toString ();
// Because AVD lacks map applications, in the simulator, geo: As the URI application cannot be triggered, and an error is reported. If the simulator is still used, you can browse through the Web. The example uses a simulator.
// Uri uri = Uri. parse ("geo:" + _ lat + _ lon );
Uri uri = Uri. parse ("http://maps.google.com /? Q = "+ _ lat +", "+ _ lon );
Startactivity (new intent (intent. action_view, Uri ));
}
});
}
}

Example 2: tab Browser

We can review the Android learning notes (ii): the last example of multi-page display-Tag usage is to display acit.pdf In the Tab. If we need to display the Activity as content in the tab, we provide an Intent to launch the required activity. In this example, we will use intent to invoke web browsing in the Tab content.

If we use

Intent intent = new Intent (Intent. ACTION_VIEW );
I. setData (Uri. parse (http://commonsware.com ));

Then, in the tab, setContent (intent); we will find that we cannot fight for work, because in Android, the activity of other apps cannot be loaded in the tab based on security considerations. Therefore, we need to write it by ourselves. We will first write two browsers embedded in webkit browser.

Public class Chapter18Test2_1 extends Activity {
Private WebView browser = null;
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
Browser = new WebView (this );
SetContentView (browser );
Browser. loadUrl ("http://commonsware.com ");
}
}
Public class Chapter18Test2_2 extends Activity {
Private WebView browser = null;
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Browser = new webview (this );
Setcontentview (browser );
Browser. loadurl ("http://www.android.com ");
}
}

The source code of calling activity is as follows:

Public class Chapter18Test2 extendsTabactivity{
// Tabactivity is used. The tab layout is already available, so we no longer need to layout XML files.
Private tabhost host = NULL;

 

Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Host =Gettabhost();
Host. addtab (host.Newtabspec("One ").Setindicator("CW ").
Setcontent(New intent(This,Chapter18test2_1.class)));
Host. addtab (host. newtabspec ("two"). setindicator ("android ").
Setcontent(New intent (This,Chapter18Test2_2.class)));
}
}

Let's look back at the code in this example. Two activities and two classes are used to open a webkit browser URL, which is a waste. We can pass the URL as the extra information of Intent. Modify the preceding example and pass extra information through Bundle. In calling activity, you can easily add tabs.

Called activity:

Public class Chapter18Test2_0 extends Activity {
Private WebView browser = null;
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
Browser = new WebView (this );
SetContentView (browser );
Bundle bundle =Getintent (). getextras ();
Browser. loadUrl (Bundle. getString("Http_uri"));
}
}

Calling activity:

Public class Chapter18Test2 extends TabActivity {
Private TabHost host = null;
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Host = gettabhost ();
Addtab ("one", "CW", "http://commonsware.com ");
Addtab ("two", "android", "http://www.android.com ");
Addtab ("three", "blog", "http://blog.csdn.net/flowingflying ");
}
// Add a method to add a tab
Private void addtab (string tag, string indicator, string URI ){
Bundle bundle = new bundle ();
Bundle. putstring ("http_uri", Uri );
Intent intent = new intent (this, chapter18test2_0.class );
Intent. putExtras (bundle );
Host. addtab (host. newtabspec (TAG). setindicator (indicator). setcontent (intent ));
}
}

Related links:
My Android development articles

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.