Android's Intent

Source: Internet
Author: User

Start component public void start (view view) {//Create an Intent//intent intent=new Intent (This,mainactivity2.class) by developing setcomponent Ntent intent=new Intent ();//create component, respond by component, direct Intentcomponentname component=new componentname (This, mainactivity2.class ); Intent.setcomponent (component); startactivity (intent);}
public void Start1 (view view) {Intent intent=new Intent (this,mainactivity9.class); startactivity (Intent);}

To find the startup activity through the Action property, you must configure it in the manifest file

Use the Action property to find the start activitypublic void Start2 (view view) {Intent intent=new Intent ();//Set action, The ACTION is actually a string tag intent.setaction (mainactivity3.my_action); startactivity (intent);}

MainActivity3

package com.example.intent;import android.app.activity;import android.os.bundle;/** *   When you use the action component when you customize an action, you must add a default category  * <activity android:name= "MainActivity3" >               <intent-filter >                    <action android:name= "Com.example.action.MY_ACTION" >                   </action>                   <category android: Name= "Android.intent.category.DEFAULT"/>   </intent-filter>    *   If more than one component is successfully matched, the dialog box list will be made available to the user  * */public class MainActivity3 extends  activity{//Custom Action Tag Public static&nbSp;final string my_action= "Com.example.action.MY_ACTION"; @Overrideprotected  void oncreate ( Bundle savedinstancestate)  {// todo auto-generated method stubsuper.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main3);}}

The configuration in the manifest file, if there is no category, must be written on the default category

<category android:name= "Android.intent.category.LAUNCHER"/>

  <application        android:allowbackup= "true"          android:icon= "@drawable/ic_launcher"          android:label= "@string/app_name"         android: Theme= "@style/apptheme"  >        <activity             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 >

The more categories added, the more accurate, if there is not found in the category will be an error (if there are two categories, a category is not found, then an error)

public void Start3 (view view) {Intent intent=new Intent (); intent.setaction (mainactivity4.my_action); Intent.addcategory (mainactivity4.my_category);//If the MAINACTIVITY4 is not configured in the manifest file, and the following category is written, an error//intent.addcategory ( MAINACTIVITY4.MY_CATEGORY2);//The more categories added, the more accurate, if there is no category can be found error startactivity (intent);}

MainActivity4

Package Com.example.intent;import android.app.activity;import android.os.bundle;/** * Custom category * Multiple categories can be added in intent *  That requires that the matched component must meet these multiple categories at the same time to match successfully, otherwise the match fails * * */public class MainActivity4 extends activity{//Custom action category public static final String my_category= "Com.example.category.myCategory";p ublic static final String my_category2= " Com.example.category.myCategory2 ";//Custom Action public static final String my_action=" Com.example.action.myAction2 "; overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); Setcontentview (R.LAYOUT.ACTIVITY_MAIN4);}}

MainActivity4 in the manifest file

 <activity android:name= "MainActivity4" >               <intent-filter >                 <!--   <action android:name= " Com.example.action.MY_ACTION "></action> -->                   <action android:name= " Com.example.action.myAction2 "></action>                   <category android:name= " Com.example.category.myCategory "/>                   <category android:name= "Com.example.category.myCategory2"/>              &nbSp;    <category android:name= "Android.intent.category.DEFAULT"/>                   <!--  Add default category when no category  -->  </intent-filter> </activity>

The Date property in intent

The Date property represents the data for the action (to be manipulated)

In fact, a date is represented by a URI object

Typically, a combination of action+date attributes is used to describe an intent (intent)

When the intent matching component has more than one, the priority component is displayed first, and if the priority level is the same, the display list lets the user choose

Priority from 1000 to 1000, and one must be negative to be valid



MainActivity5: Specify access to a URL


<intent-filter android:priority= "> "

<action android:name= "Android.intent.action.VIEW"/>

<data android:scheme= "http" android:host= "Java.lampbrother.net"/>

<category android:name= "Android.intent.category.DEFAULT"/>

</intent-filter>



Intent intent=new Intent ();

Intent.setaction (Intent.action_view);

Uri uri=uri.parse ("http://java.lampbrother.net");

Intent.setdata (URI);

StartActivity (Intent);

Package com.example.intent;import android.app.activity;import android.content.intent;import  android.net.Uri;import android.os.Bundle;import android.view.View;/** *  The Date Property  * date property in intent represents the data for the action (to be manipulated)  *  actually a date is represented by a URI object  *  typically, using action+ The combination of the date property describes an intent (intent)  *  *  *  *  displays the component with the highest priority when the intent matching component has multiple If the priority level is the same, the display list lets the user choose  *  Priority from 1000 to 1000, and one of them must be negative to be valid  * */public class  mainactivity5 extends activity{@Overrideprotected  void oncreate (bundle  savedinstancestate)  {super.oncreate (savedinstancestate); Setcontentview (R.LAYOUT.ACTIVITY_MAIN5);} Public void testactiondateclick (view ivew) {intent intent=new intent (); Intent.setaction (Intent.action_view); Uri uri=uri.parse ("Http://java.lampbrother.net"); Intent.setdata (URI); startactivity (intent);} Public void testactivity7 (View view) {INTENT&NBsp;intent=new intent (This,mainactivity7.class); startactivity (intent);}} 

Configuration of MainActivity5 in the manifest file

         <activity android:name= "MainActivity5" ></ Activity>            <activity android: Name= "MainActivity6" >                 <intent-filter android:priority= ">        "             <action android:name= " Android.intent.action.VIEW "/>                     <data android:scheme= "http"  android:host= " Java.lampbrother.net "/>                 <category android:name= "Android.intent.category.DEFAULT"/>                 </intent-filter>          </activity>

Intent

Date+type Property

The type represents the types of data or MIME types (such as: Text/xml,text/html,audio/mp3,image/png ...)

The date and type properties generally require only one

The Setdate method sets the type implementation to NULL, instead, setting Settype sets the date property to null

If you want two properties to be set at the same time, use the Setdateandtype method to set



MianActivity7: Call System Selection (various) files, take mp3 file as an example,

Package com.example.intent;import android.app.activity;import android.content.intent;import  android.net.Uri;import android.os.Bundle;import android.view.View;/** * Intent  The * date+type property  * type represents the type of data or MIME type (such as: Text/xml,text/html,audio/mp3,image/png ...) The  * date and type properties generally require only one  *  the type implementation is set to NULL by using the Setdate method, whereas setting Settype also sets the date property to null *   If you want two properties to be set at the same time, use the Setdateandtype method to set the  *  * */public class MainActivity7  extends activity{@Overrideprotected  void oncreate (bundle savedinstancestate)  {//  todo auto-generated method stubsuper.oncreate (savedinstancestate); SetContentView ( r.layout.activity_main7);} Public void play7 (View view) {intent intent=new intent (); Intent.setAction ( Intent.action_view); Uri date=uri.parse ("File:///sdcard/xx.mp3");//intent.setdata (date);//intent.setdataandtype (data,  type); Intent.sEttype ("Audio/mp3"); StartActivity (intent);}} 

MainActivity8

Package com.example.intent;import android.app.activity;import android.content.intent;import  android.net.Uri;import android.os.Bundle;import android.view.View;/** *  Activity start Mode: 4 kinds of  * 1.standard  Standard mode, each start will create a new activity, join the target task of the stack top  * 2.singletop, Determine if the current task stack top has the same activity, if there is a direct use  *  if it does not exist, then create a new activity object into the stack top  * 3. Singletask: In the task stack is to determine whether there is the same activity, if it exists, it will clear the activity on the other activity object to display  *  if not present, create a new activity object to put on top of the stack  * 4.singleinstance: Activity is created in a new task stack, and only one activity instance is allowed in the task stack, *  Other components that invoke the activity directly use the  *  * intent flags property of the activity object in the task stack, or you can specify a startup mode  *  *   *  * */public class mainactivity8 extends activity{@ Overrideprotected void oncreate (bundle savedinstancestate)  {// TODO  Auto-generated method stubsuper.oncreate (savedinstancestate); Setcontentview (R.layout.activity_MAIN8);} Public void start1 (View view) {intent intent=new intent (this,MainActivity9.class); StartActivity (intent);} Public void start2 (View view) {intent intent=new intent (This,MainActivity8.class);// Equivalent to Singletask//intent.setflags (intent.flag_activity_new_task);//equivalent to Singletop//intent.setflags (Intent.FLAG_ Activity_single_top); startactivity (intent);}}


This article is from the "matengbing" blog, make sure to keep this source http://matengbing.blog.51cto.com/11395502/1883732

Android's Intent

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.