HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Android intent is an abstract description of the action to be executed. It can be started by a startActivity activities, broadcastIntent to send broadcast it to any interested radio receiver components, startService (Intent) or bindService (Intent, ServiceConnection, int) to communicate with back-end services.
Intent itself (an Intent object) is a passive data structure that holds the abstract description of the operation to be performed.
For example, you have an active, open the mail client and send mail to your Android device. For this purpose, you need to send ACTION_SEND activity with a suitable selector to Android intention processor. Specified selector given the appropriate interface to let the user decide how to send his message data.
Intent email = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:")); email.putExtra(Intent.EXTRA_EMAIL, recipients); email.putExtra(Intent.EXTRA_SUBJECT, subject.getText().toString()); email.putExtra(Intent.EXTRA_TEXT, body.getText().toString()); startActivity(Intent.createChooser(email, "Choose an email client from/en."));
StartActivity syntax for calling the above methods to open the e-mail activity, the results of running the code looks like this:
For example, you have an active, you need to open a URL on Android devices through a browser. For this purpose, send your event ACTION_WEB_SEARCH intention to Android processors intention to open a given URL in your browser. Processor intent by analyzing a series of activities, and to choose the most suitable one activity you intend, in this case, the Web browser activity. Processor intent to pass on your web page address to the Web browser, and open the Web browser activity.
String q = "abc"; Intent intent = new Intent(Intent.ACTION_WEB_SEARCH ); intent.putExtra(SearchManager.QUERY, q); startActivity(intent);
The above example will find "https://w3resource.net/" search engine on Android, and gives results on a keyword activity.
For each component - activities, services, broadcast receivers have independent mechanism to convey intent.
No. | Method and Description |
---|
1 | Context.startActivity (): intent passed to the method used to start a new activity or have an existing activity to do something new. |
2 | Context.startService (): intent passed to the method, initialize a service, or new information to a persistent service. |
3 | Context.sendBroadcast (): intent passed to the method, the information will be delivered to all interested broadcast receivers. |
Intent object is a package of information for component receives intentions as Android system receives information.
Intent object includes the following components, depending on what you want to communicate or perform.
It is to be part of the intent of the object, it is represented as a string. Intent broadcast, the action in the event, will be reported. Action will largely determine how the rest of intent is organized. Intent class defines a set of actions constants corresponding to different intentions. Here is the Android intent standard action list.
Intent object in motion can be set by setAction () method, by getAction () method to read.
Add data to the intention of the filter specifications. This specification can be just one data type (such as cell type attribute), a URI, or include both data types and URI. By the URI to specify attributes of different portions.
These attributes specify the format of the URL is optional, but also independent of each other -
setData () method can only be specified URI data, setType () can only specify the type of meta data, setDataAndType () can be specified URI and element types simultaneously. URI by getData () to read, type by getType () to read.
Here are some examples of actions / data set -
No. | Action / data and a description |
---|
1 | ACTION_VIEW content: // contacts / people / 1: 1 is displayed for the user ID information. |
2 | ACTION_DIAL content: // contacts / people / 1: Show phone dialer, and populate the user data 1. |
3 | ACTION_VIEW tel: 123: Show phone dialer, and fill the given number. |
4 | ACTION_DIAL tel: 123: Show phone dialer, and fill the given number. |
5 | ACTION_EDIT content: // contacts / people / 1: Edit the user ID information 1. |
6 | ACTION_VIEW content: // contacts / people /: display a list of users to view. |
7 | ACTION_SET_WALLPAPER: Select the wallpaper display setting. |
8 | ACTION_SYNC: synchronize data, default value: android.intent.action.SYNC |
9 | ACTION_SYSTEM_TUTORIAL: Open platform definition tutorial (or default start tutorial Tutorial) |
10 | ACTION_TIMEZONE_CHANGED: When the time zone is changed notification |
11 | ACTION_UNINSTALL_PACKAGE: you run the default uninstaller |
Category is an optional part of the intent, is a string that contains additional information about the types of components to be processed intentions. addCategory () method with the intention of adding the object category, removeCategory () method to remove before adding categories, getCategories () Gets all set to the intent object categories. Here is the Android intent standard category list.
You can view the following sections intent filters to understand how we use categories by a corresponding intention to select the appropriate activities.
This is passed to the assembly to deal with intent to key additional information described herein. Sets by putExtras (), getExtras () method to read. Here is the Android intent standard additional data list.
These tags are an optional part of the intent, indicating the Android system how to initiate the activity, how after starting treatment.
No. | Tags and descriptions |
---|
1 | FLAG_ACTIVITY_CLEAR_TASK: If the intent, and passing through Context.startActivity, this tag will cause all existing tasks associated with the activity is cleared before the event starts. Activities will be the root of an empty task, all the old activity is terminated. Use of the mark may be combined with FLAG_ACTIVITY_NEW_TASK. |
2 | FLAG_ACTIVITY_CLEAR_TOP: If this flag is set, the event will be started in the task that is currently running. This disease does not start a new activity instance, all activity on it is turned off, this intention as a new intent is delivered to the existing (currently at the top) activities. |
3 | FLAG_ACTIVITY_NEW_TASK: This tag is generally used for such activities "starter" style of behavior: to provide users with a run of data can be completed independently, and start a separate children complete activities. |
Component Object name is an optional field representative activities, services or broadcast receiver class. If set, the Intent object is passed to implement an instance of the class designed Otherwise, Android other intentions other information to locate a suitable target. Component name by setComponent (), setClass () or setClassName () to set up, () get through getComponent.
Android supports two types of intentions.
The inner world of an explicit intention to connect the application, if you need to connect an activity to another activity, we can show intent, the following figure shows the connection by clicking the button first activity to the second activity.
The intent of targeting component by name, generally used for the application of internal information - such as a subsidiary activity or activity to start a start a sibling activities. for example:
// 通过指定类名的显式意图 Intent i = new Intent(FirstActivity.this, SecondAcitivity.class); // 启动目标活动 startActivity(i);
The intent of these goals do not name, the domain component name is blank. Implicit intents are often used to activate components of other applications. for example:
Intent read1=new Intent(); read1.setAction(android.content.Intent.ACTION_VIEW); read1.setData(ContactsContract.Contacts.CONTENT_URI); startActivity(read1);
The code above will be given the following results:
Target component receives intent, you can use getExtras () method to obtain additional data transmitted by the source component. E.g:
// 在代码中的合适位置获取包对象 Bundle extras = getIntent().getExtras(); // 通过键解压数据 String value1 = extras.getString("Key1"); String value2 = extras.getString("Key2");
The following example demonstrates the use of Android intention to launch a variety of Android features built-in applications.
step | description |
---|
1 | Using Android Studio IDE to create an Android application, named Intent filter, package named cn.w3resource.intentfilter. When you create a project, ensure that the target SDK and compile with the latest version of the Android SDK use advanced API. |
2 | Modify src / cn.w3resource.intentfilter / MainActivity.java file and add the code defines two listeners to the corresponding two buttons "Start Browser" and "Start Call" |
3 | Modify res / layout / activity_main.xml layout files, online layout add three buttons. |
4 | Launch Android emulator to run the application and verify the results of changes made to the application. |
The following is the content of src / cn.w3resource.intentfilter / MainActivity.java file:
package cn.w3resource.intentfilter; import android.content.Intent; import android.net.Uri; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class MainActivity extends ActionBarActivity { Button b1,b2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1=(Button)findViewById(R.id.button); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("https://w3resource.net/")); startActivity(i); } }); b2=(Button)findViewById(R.id.button2); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("tel:9510300000")); startActivity(i); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Here is the content of res / layout / activity_main.xml file:
<RelativeLayout xmlns:android="https://www.android.com/" xmlns:tools="https://www.android.com/" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="意图实例" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="30dp" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="https://w3resource.net/" android:textColor="#ff87ff09" android:textSize="30dp" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton" android:src="@drawable/ic_launcher" android:layout_below="@+id/textView2" android:layout_centerHorizontal="true" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_below="@+id/imageButton" android:layout_alignRight="@+id/imageButton" android:layout_alignEnd="@+id/imageButton" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动浏览器" android:id="@+id/button" android:layout_alignTop="@+id/editText" android:layout_alignRight="@+id/textView1" android:layout_alignEnd="@+id/textView1" android:layout_alignLeft="@+id/imageButton" android:layout_alignStart="@+id/imageButton" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动电话" android:id="@+id/button2" android:layout_below="@+id/button" android:layout_alignLeft="@+id/button" android:layout_alignStart="@+id/button" android:layout_alignRight="@+id/textView2" android:layout_alignEnd="@+id/textView2" /> </RelativeLayout>
Here is the content of res / values / strings / xml, defining two new constants.
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Intent filter</string> <string name="action_settings">Settings</string> </resources>
The following is the default AndroidManifest.xml content:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.w3resource.intentfilter" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="22" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/Base.Theme.AppCompat" > <activity android:name="cn.w3resource.intentfilter.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>
Let's just run a modified Intent filter applications. I assume you've created AVD during the installation environment. Open your project in the active file, click on the toolbar Icon to run the application In Android Studio. Android Studio install the application on the AVD and starts it. If all goes well, it will be displayed on the emulator window as follows:
Now click on the "Start Browser" button, which will launch a browser, depending on configuration and display https://w3resource.net/ as follows:
In a similar manner, you can click on the "Start Call" button to open the phone interface, which will allow you to dial phone numbers have been given.
You have seen how to use the intention to call another activity. Android operating system uses filters to specify a range of activities, services, and broadcast receivers handle intent, need the help of intent specified action category, the data pattern. Use <intent-filter> element in the manifest file listed in the corresponding action, categories, and data types in activities, services, and broadcast receivers.
The following example shows part of AndroidManifest.xml file specified by the action of an active cn.w3resource.intentfilter.CustomActivity can set categories and data calls:
<activity android:name=".CustomActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <action android:name="com.example.MyApplication.LAUNCH" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="http" /> </intent-filter> </activity>
When the activity is defined above the filter, other activities you can use the following way to invoke this event. Use android.intent.action.VIEW, use cn.w3resource.intentfilter.LAUNCH action and provide android.intent.category.DEFAULT category.
Element specifies the activities to be called the expected data type. Examples of the above, the custom activity desired data from the "http: //" at the beginning.
This is the case, through the filter, it is intended to be delivered to multiple activities or services to which the user will be asked to start assembly. If you do not find the target component, an exception occurs.
Before calling the event, a series of Android Check test:
The following examples are some modifications of the above examples. Here we will see if a defined intention to call two events, Android how to resolve the conflict; how to use filters to invoke a custom activity; if there is no intention to define the appropriate activities will be abnormal.
step | Explanation |
---|
1 | Using Android Studio IDE to create an Android application, named Intent filter, package named cn.w3resource.intentfilter. When you create a project, ensure that the target SDK and compile with the latest version of the Android SDK use advanced API. |
2 | Modify src / cn.w3resource.intentfilter / MainActivity.java file, add the code to define three listeners to three buttons corresponding to the layout defined in the document. |
3 | Add src / cn.w3resource.intentfilter / CustomActivity.java file to include an activity that can be a different intention calls. |
4 | Modify res / layout / activity_main.xml linear layout file add three buttons. |
5 | Add res / lauout / custom_view.xml layout file, simply add |
6 | Modify AndroidManifest.xml file, add the <intent-filter> definition of rules intended to invoke custom activities. |
7 | Launch Android emulator to run the application and verify the results of changes made to the application. |
The following is the content of src / cn.w3resource.intentfilter / MainActivity.java of:
package cn.w3resource.intentfilter; import android.content.Intent; import android.net.Uri; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class MainActivity extends ActionBarActivity { Button b1,b2,b3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1=(Button)findViewById(R.id.button); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("https://w3resource.net/")); startActivity(i); } }); b2=(Button)findViewById(R.id.button2); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent("cn.w3resource.intentfilter.LAUNCH",Uri.parse("https://w3resource.net/")); startActivity(i); } }); b3=(Button)findViewById(R.id.button3); b3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent("cn.w3resource.intentfilter.LAUNCH",Uri.parse("https://w3resource.net/")); startActivity(i); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Here's what src / cn.w3resource.intentfilter / CustomActivity.java of:
package cn.w3resource.intentfilter; import android.app.Activity; import android.net.Uri; import android.os.Bundle; import android.widget.TextView; public class CustomActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.custom_view); TextView label = (TextView) findViewById(R.id.show_data); Uri url = getIntent().getData(); label.setText(url.toString()); } }
Here is the content of res / layout / activity_main.xml of:
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="意图实例" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="30dp" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="https://w3resource.net/" android:textColor="#ff87ff09" android:textSize="30dp" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton" android:src="@drawable/ic_launcher" android:layout_below="@+id/textView2" android:layout_centerHorizontal="true" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_below="@+id/imageButton" android:layout_alignRight="@+id/imageButton" android:layout_alignEnd="@+id/imageButton" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="通过View动作启动浏览器" android:id="@+id/button" android:layout_alignTop="@+id/editText" android:layout_alignRight="@+id/textView1" android:layout_alignEnd="@+id/textView1" android:layout_alignLeft="@+id/imageButton" android:layout_alignStart="@+id/imageButton" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="通过Launch动作启动浏览器" android:id="@+id/button2" android:layout_below="@+id/button" android:layout_alignLeft="@+id/button" android:layout_alignStart="@+id/button" android:layout_alignRight="@+id/textView2" android:layout_alignEnd="@+id/textView2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="异常情况" android:id="@+id/button3" android:layout_below="@+id/button2" android:layout_alignLeft="@+id/button2" android:layout_alignStart="@+id/button2" android:layout_alignRight="@+id/textView2" android:layout_alignEnd="@+id/textView2" /> </RelativeLayout>
Here is the content of res / layout / custom_view.xml file:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/show_data" android:layout_width="fill_parent" android:layout_height="400dp"/> </LinearLayout>
Here is the content of res / values / strings.xml file:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">My Application</string> <string name="action_settings">Settings</string> </resources>
Here is the content of AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.w3resource.intentfilter" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="22" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/Base.Theme.AppCompat" > <activity android:name="cn.w3resource.intentfilter.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 android:name="cn.w3resource.intentfilter.CustomActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <action android:name="cn.w3resource.intentfilter.LAUNCH" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="http" /> </intent-filter> </activity> </application> </manifest>
Let's just run a modified Intent filter applications. I assume you've created AVD during the installation environment. Open your project in the active file, click on the toolbar Icon to run the application In Android Studio. Android Studio install the application on the AVD and starts it. If all goes well, it will be displayed on the emulator window as follows:
Click on the first button, "Using View action to start the browser." Here we define our custom activity contains "android.intent.action.VIEW", and the Android system has been defined to correspond to the default active VIEW action to start the Web browser, Android displays the following options to choose what you want to start activity:
If you choose a browser, Android will launch a Web browser and open website. If you choose IntentDemo option, Android will start CustomActivity, the event did not do anything, just capture and display the data passed in the TextView.
Now, through the Back button and click "Launch action by starting Browser" button, where Android apply filters to select the defined activities, and simply start the custom activities.
Again use the back button to go back and click on the "exceptional conditions" button, try to find one where Android intent given by the effective filters, but could not find a definition of effective activity. Because we use https instead of http data, and given the correct action, once Android generate an exception. as follows: