Learn Android notes by yourself -- use intent in activity
1. Intent introduction:
Intent stands for "intention, intention" in Chinese. In Android, the Intent mechanism is provided to assist in interaction and communication between applications, intent describes the actions, actions involving data, and additional data of an application. Android identifies the corresponding components based on the description of the Intent, pass Intent to the called component and complete the call of the component. Intent can be used not only between applications, but also between activities/services within the application. Therefore, Intent can be understood as a "medium" for communication between different components to provide information about the mutual call of components.
How to start the Inten component
Intent can start an Activity, a Service, and broadcast Broadcasts. The specific method is as follows:
Component name |
Method Name |
Activity |
StartActvity () StartActivity () |
Service |
StartService () BindService () |
Broadcasts |
SendBroadcasts () SendOrderedBroadcasts () SendStickyBroadcasts () |
2. explicit intent and implicit intent:
Intent mainly includes implicit intent and explicit intent. Explicit intent is usually used to start data between activities in the current application, while implicit intent is common in some specific actions in the startup system, such as calling or cross-application Activity startup.
Explicit Intent: Call Intent. setComponent () or Intent. the setClass () method explicitly specifies the Intent of the component name as the explicit Intent, and the explicit Intent specifies the component to which the Intent should be passed.
Implicit Intent: Intent without explicitly specifying the component name is implicit Intent. The Android system will find the most suitable component to process the intent based on the action, category, and data (URI and data type) set in the implicit intent.
Iii. Case -- enable the system camera:
Activity_main.xml:
Main:
package com.example.wanglaoda.opencamera;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;public class opencamera extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_opencamera); Button button = (Button) findViewById(R.id.opencamera); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(); intent.setAction(android.media.action.IMAGE_CAPTURE); intent.addCategory(android.intent.category.DEFAULT); startActivity(intent); } }); } @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_opencamera, 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); }}