• android Intent 全面点的介绍


    第一种方式,用action来跳转。

    1、使用Action跳转,如果有一个程序的AndroidManifest.xml中的某一个Activity的IntentFilter段中 定义了包含了相同的Action那么这个Intent就与这个目标Action匹配。如果这个IntentFilter段中没有定义 Type,Category,那么这个 Activity就匹配了。但是如果手机中有两个以上的程序匹配,那么就会弹出一个对话可框来提示说明。
    Action的值在Android中有很多预定义,如果你想直接转到你自己定义的Intent接收者,你可以在接收者的IntentFilter 中加入一个自定义的Action值(同时要设定 Category值为"android.intent.category.DEFAULT"),在你的Intent中设定该值为Intent的 Action,就直接能跳转到你自己的Intent接收者中。因为这个Action在系统中是唯一的。
    2,data/type,你可以用Uri来做为data,比如Uri uri = Uri.parse(http://www.google.com);
    Intent i = new Intent(Intent.ACTION_VIEW,uri);手机的Intent分发过程中,会根据http://www.google.com 的scheme判断出数据类型type
    手机的Brower则能匹配它,在Brower的Manifest.xml中的IntenFilter中首先有ACTION_VIEW Action,也能处理http:的type,

    3, 至于分类Category,一般不要去在Intent中设置它,如果你写Intent的接收者,就在Manifest.xml的Activity的 IntentFilter中包含android.category.DEFAULT,这样所有不设置 Category(Intent.addCategory(String c);)的Intent都会与这个Category匹配。

    4,extras(附加信息),是其它所有附加信息的集合。使用extras可以为组件提供扩展信息,比如,如果要执行“发送电子邮件”这个动 作,可以将电子邮件的标题、正文等保存在extras里,传给电子邮件发送组件。

    Java 代码
    1. package com.android.edit_text;  
    2.   
    3. import android.app.Activity;  
    4. import android.content.Intent;  
    5. import android.os.Bundle;  
    6. import android.view.KeyEvent;  
    7. import android.view.View;  
    8. import android.widget.EditText;  
    9.   
    10. public class MyEditText extends Activity {  
    11.   
    12.     private TextView m_TextView;  
    13.     private EditText m_EditText;  
    14.   
    15.     /** Called when the activity is first created. */  
    16.     @Override  
    17.     public void onCreate(Bundle savedInstanceState) {  
    18.         super.onCreate(savedInstanceState);  
    19.         setContentView(R.layout.main);  
    20.   
    21.         m_EditText = (EditText) this.findViewById(R.id.EditText01);  
    22.         m_EditText.setOnKeyListener(editTextKeyListener);  
    23.     }  
    24.   
    25.     private EditText.OnKeyListener editTextKeyListener = new EditText.OnKeyListener() {  
    26.   
    27.         @Override  
    28.         public boolean onKey(View arg0, int arg1, KeyEvent arg2) {  
    29.           
    30.                         // action跳转,需要在AndroidManifest.xml中配置action  
    31.             Intent i = new Intent("android.intent.action.mydialog");  
    32.             MyEditText.this.startActivity(i);  
    33.               
    34.             return false;  
    35.         }  
    36.     };  
    37. }  
    package com.android.edit_text; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.widget.EditText; public class MyEditText extends Activity { private TextView m_TextView; private EditText m_EditText; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); m_EditText = (EditText) this.findViewById(R.id.EditText01); m_EditText.setOnKeyListener(editTextKeyListener); } private EditText.OnKeyListener editTextKeyListener = new EditText.OnKeyListener() { @Override public boolean onKey(View arg0, int arg1, KeyEvent arg2) { // action跳转,需要在AndroidManifest.xml中配置action Intent i = new Intent("android.intent.action.mydialog"); MyEditText.this.startActivity(i); return false; } }; } 


    Xml代 码
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     package="com.android.edit_text" android:versionCode="1"  
    4.     android:versionName="1.0">  
    5.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
    6.         <activity android:name=".MyEditText" android:label="@string/app_name">  
    7.             <intent-filter>  
    8.                 <action android:name="android.intent.action.MAIN" />  
    9.                 <category android:name="android.intent.category.LAUNCHER" />  
    10.             </intent-filter>  
    11.         </activity>  
    12.                 <!--配置跳转activity-->  
    13.         <activity android:name="com.android.dialog.MyDialog">  
    14.             <intent-filter>  
    15.     <!--配置action路径-->  
    16.                 <action android:name="android.intent.action.mydialog" />  
    17.                 <category android:name="android.intent.category.DEFAULT" />  
    18.             </intent-filter>  
    19.         </activity>  
    20.     </application>  
    21.     <uses-sdk android:minSdkVersion="7" />  
    22.   
    23. </manifest>   
    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.edit_text" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MyEditText" 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--> <activity android:name="com.android.dialog.MyDialog"> <intent-filter> <!--配置action路径--> <action android:name="android.intent.action.mydialog" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="7" /> </manifest> 


    第二种方式,用类名跳转。

    Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述, 负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。Intent在这里起着实现调用者与被调用者之间的解耦作用。
    Intent传递过程中,要找到目标消费者(另一个Activity,IntentReceiver或Service),也就是Intent的响 应者。

    Java 代码
    1. package com.Android;  
    2.   
    3. import android.app.Activity;  
    4. import android.content.Intent;  
    5. import android.os.Bundle;  
    6. import android.view.View;  
    7. import android.view.View.OnClickListener;  
    8.   
    9. public class FormStuff extends Activity {  
    10.     @Override  
    11.     public void onCreate(Bundle savedInstanceState) {  
    12.         super.onCreate(savedInstanceState);  
    13.         setContentView(R.layout.formstuff);  
    14.   
    15.         final ImageButton button = (ImageButton) findViewById(R.id.android_button);  
    16.         button.setOnClickListener(new OnClickListener() {  
    17.             public void onClick(View v) {  
    18. // 用类名跳转,需要在AndroidManifest.xml中申明activity  
    19.                 Intent intent = new Intent(FormStuff.this, HelloTabWidget.class);  
    20.                 startActivity(intent);  
    21.             }  
    22.         });  
    23.   
    24. }  
    package com.Android; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; public class FormStuff extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.formstuff); final ImageButton button = (ImageButton) findViewById(R.id.android_button); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { // 用类名跳转,需要在AndroidManifest.xml中申明activity Intent intent = new Intent(FormStuff.this, HelloTabWidget.class); startActivity(intent); } }); } 


    Xml代 码
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     package="com.Android" android:versionCode="1" android:versionName="1.0">  
    4.       
    5.     <application android:icon="@drawable/icon" android:theme="@android:style/Theme.NoTitleBar">  
    6.         <activity android:name=".FormStuff" android:label="@string/app_name">  
    7.             <intent-filter>  
    8.                 <action android:name="android.intent.action.MAIN" />  
    9.                 <category android:name="android.intent.category.LAUNCHER" />  
    10.             </intent-filter>  
    11.         </activity>  
    12.         <!--申明activity-->  
    13.     <activity android:name="HelloTabWidget"></activity>  
    14. </application>  
    15.     <uses-sdk android:minSdkVersion="4" />  
    16. </manifest>   
    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.Android" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:theme="@android:style/Theme.NoTitleBar"> <activity android:name=".FormStuff" 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--> <activity android:name="HelloTabWidget"></activity> </application> <uses-sdk android:minSdkVersion="4" /> </manifest> 


    一些Intent的常用发:
    Java 代码
    1. 显示网 页  
    2.   
    3.    1. Uri uri = Uri.parse("http://google.com");     
    4.    2. Intent it = new Intent(Intent.ACTION_VIEW, uri);     
    5.    3. startActivity(it);     
    6.   
    7. 显 示地图  
    8.   
    9.    1. Uri uri = Uri.parse("geo:38.899533,-77.036476");     
    10.    2. Intent it = new Intent(Intent.ACTION_VIEW, uri);      
    11.    3. startActivity(it);      
    12.    4//其 他 geo URI 範例     
    13.    5//geo:latitude,longitude     
    14.    6//geo:latitude,longitude?z=zoom     
    15.    7//geo:0,0?q=my+street+address     
    16.    8//geo:0,0?q=business+near+city     
    17.    9//google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom    
    18.    
    19.   
    20. 路 径规划  
    21.   
    22.    1. Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");     
    23.    2. Intent it = new Intent(Intent.ACTION_VIEW, uri);     
    24.    3. startActivity(it);     
    25.    4//where startLat, startLng, endLat, endLng are a long with 6 decimals like: 50.123456     
    26.   
    27. 打 电话  
    28.    1//叫出拨号程序    
    29.    2. Uri uri = Uri.parse("tel:0800000123");     
    30.    3. Intent it = new Intent(Intent.ACTION_DIAL, uri);     
    31.    4. startActivity(it);     
    32.   
    33.    1//直接打电话出 去     
    34.    2. Uri uri = Uri.parse("tel:0800000123");     
    35.    3. Intent it = new Intent(Intent.ACTION_CALL, uri);     
    36.    4. startActivity(it);     
    37.    5//用這个,要 在 AndroidManifest.xml 中,加上     
    38.    6//<uses-permission id="android.permission.CALL_PHONE" />     
    39.   
    40. 传 送SMS/MMS  
    41.    1//调用短信程序    
    42.    2. Intent it = new Intent(Intent.ACTION_VIEW, uri);     
    43.    3. it.putExtra("sms_body""The SMS text");      
    44.    4. it.setType("vnd.android-dir/mms-sms");     
    45.    5. startActivity(it);    
    46.   
    47.    1//传送消息    
    48.    2. Uri uri = Uri.parse("smsto://0800000123");     
    49.    3. Intent it = new Intent(Intent.ACTION_SENDTO, uri);     
    50.    4. it.putExtra("sms_body""The SMS text");     
    51.    5. startActivity(it);    
    52.   
    53.    1//传送 MMS     
    54.    2. Uri uri = Uri.parse("content://media/external/images/media/23");     
    55.    3. Intent it = new Intent(Intent.ACTION_SEND);      
    56.    4. it.putExtra("sms_body""some text");      
    57.    5. it.putExtra(Intent.EXTRA_STREAM, uri);     
    58.    6. it.setType("image/png");      
    59.    7. startActivity(it);     
    60.   
    61. 传 送 Email  
    62.    1. Uri uri = Uri.parse("mailto:xxx@abc.com");     
    63.    2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);     
    64.    3. startActivity(it);     
    65.   
    66.    1. Intent it = new Intent(Intent.ACTION_SEND);     
    67.    2. it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");     
    68.    3. it.putExtra(Intent.EXTRA_TEXT, "The email body text");     
    69.    4. it.setType("text/plain");     
    70.    5. startActivity(Intent.createChooser(it, "Choose Email Client"));     
    71.   
    72.    1. Intent it=new Intent(Intent.ACTION_SEND);       
    73.    2. String[] tos={"me@abc.com"};       
    74.    3. String[] ccs={"you@abc.com"};       
    75.    4. it.putExtra(Intent.EXTRA_EMAIL, tos);       
    76.    5. it.putExtra(Intent.EXTRA_CC, ccs);       
    77.    6. it.putExtra(Intent.EXTRA_TEXT, "The email body text");       
    78.    7. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");       
    79.    8. it.setType("message/rfc822");       
    80.    9. startActivity(Intent.createChooser(it, "Choose Email Client"));    
    81.   
    82.    1//传送附件  
    83.    2. Intent it = new Intent(Intent.ACTION_SEND);     
    84.    3. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");     
    85.    4. it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");     
    86.    5. sendIntent.setType("audio/mp3");     
    87.    6. startActivity(Intent.createChooser(it, "Choose Email Client"));    
    88.   
    89. 播 放多媒体  
    90.        Uri uri = Uri.parse("file:///sdcard/song.mp3");     
    91.        Intent it = new Intent(Intent.ACTION_VIEW, uri);     
    92.        it.setType("audio/mp3");     
    93.        startActivity(it);    
    94.   
    95.    
    96.   
    97.        Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");     
    98.        Intent it = new Intent(Intent.ACTION_VIEW, uri);     
    99.   
    100.        startActivity(it);    
    101.   
    102.    
    103.   
    104. Market 相 关  
    105. 1.        //寻找某个应用    
    106. 2.        Uri uri = Uri.parse("market://search?q=pname:pkg_name");    
    107. 3.        Intent it = new Intent(Intent.ACTION_VIEW, uri);     
    108. 4.        startActivity(it);     
    109. 5.        //where pkg_name is the full package path for an application    
    110.   
    111. 1.        //显示 某个应用的相关信息    
    112. 2.        Uri uri = Uri.parse("market://details?id=app_id");     
    113. 3.        Intent it = new Intent(Intent.ACTION_VIEW, uri);    
    114. 4.        startActivity(it);     
    115. 5.        //where app_id is the application ID, find the ID      
    116. 6.        //by clicking on your application on Market home      
    117. 7.        //page, and notice the ID from the address bar    
    118.   
    119.    
    120. Uninstall 应 用程序  
    121. 1.        Uri uri = Uri.fromParts("package", strPackageName, null);    
    122. 2.        Intent it = new Intent(Intent.ACTION_DELETE, uri);      
    123. 3.        startActivity(it);     
    显示网页 
    1. Uri uri = Uri.parse(http://google.com); 
    2. Intent it = new Intent(Intent.ACTION_VIEW, uri); 
    3. startActivity(it); 
    显示地图 
    1. Uri uri = Uri.parse("geo:38.899533,-77.036476"); 
    2. Intent it = new Intent(Intent.ACTION_VIEW, uri); 
    3. startActivity(it); 
    4. //其他 geo URI 範例 
    5. //geo:latitude,longitude 
    6. //geo:latitude,longitude?z=zoom 
    7. //geo:0,0?q=my+street+address 
    8. //geo:0,0?q=business+near+city 
    9. //google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom 
    路径规划 
    1. Uri uri = Uri.parse(http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en); 
    2. Intent it = new Intent(Intent.ACTION_VIEW, uri); 
    3. startActivity(it); 
    4. //where startLat, startLng, endLat, endLng are a long with 6 decimals like: 50.123456 
    打电话 
    1. //叫出拨号程序 
    2. Uri uri = Uri.parse("tel:0800000123"); 
    3. Intent it = new Intent(Intent.ACTION_DIAL, uri); 
    4. startActivity(it); 
    1. //直接打电话出去 
    2. Uri uri = Uri.parse("tel:0800000123"); 
    3. Intent it = new Intent(Intent.ACTION_CALL, uri); 
    4. startActivity(it); 
    5. //用這个,要在 AndroidManifest.xml 中,加上 
    6. //<uses-permission id="android.permission.CALL_PHONE" /> 
    传送SMS/MMS 
    1. //调用短信程序 
    2. Intent it = new Intent(Intent.ACTION_VIEW, uri); 
    3. it.putExtra("sms_body", "The SMS text"); 
    4. it.setType("vnd.android-dir/mms-sms"); 
    5. startActivity(it); 
    1. //传送消息 
    2. Uri uri = Uri.parse("smsto://0800000123"); 
    3. Intent it = new Intent(Intent.ACTION_SENDTO, uri); 
    4. it.putExtra("sms_body", "The SMS text"); 
    5. startActivity(it); 
    1. //传送 MMS 
    2. Uri uri = Uri.parse("content://media/external/images/media/23"); 
    3. Intent it = new Intent(Intent.ACTION_SEND); 
    4. it.putExtra("sms_body", "some text"); 
    5. it.putExtra(Intent.EXTRA_STREAM, uri); 
    6. it.setType("image/png"); 
    7. startActivity(it); 
    传送 Email 
    1. Uri uri = Uri.parse(mailto:xxx@abc.com); 
    2. Intent it = new Intent(Intent.ACTION_SENDTO, uri); 
    3. startActivity(it); 
    1. Intent it = new Intent(Intent.ACTION_SEND); 
    2. it.putExtra(Intent.EXTRA_EMAIL, me@abc.com); 
    3. it.putExtra(Intent.EXTRA_TEXT, "The email body text"); 
    4. it.setType("text/plain"); 
    5. startActivity(Intent.createChooser(it, "Choose Email Client")); 
    1. Intent it=new Intent(Intent.ACTION_SEND); 
    2. String[] tos={me@abc.com}; 
    3. String[] ccs={you@abc.com}; 
    4. it.putExtra(Intent.EXTRA_EMAIL, tos); 
    5. it.putExtra(Intent.EXTRA_CC, ccs); 
    6. it.putExtra(Intent.EXTRA_TEXT, "The email body text"); 
    7. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text"); 
    8. it.setType("message/rfc822"); 
    9. startActivity(Intent.createChooser(it, "Choose Email Client")); 
    1. //传送附件 
    2. Intent it = new Intent(Intent.ACTION_SEND); 
    3. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text"); 
    4. it.putExtra(Intent.EXTRA_STREAM, http://www.cnblogs.com/hdjjun/admin/file:///sdcard/mysong.mp3); 
    5. sendIntent.setType("audio/mp3"); 
    6. startActivity(Intent.createChooser(it, "Choose Email Client")); 
    播放多媒体 
    Uri uri = Uri.parse(http://www.cnblogs.com/hdjjun/admin/file:///sdcard/song.mp3); 
    Intent it = new Intent(Intent.ACTION_VIEW, uri); 
    it.setType("audio/mp3"); 
    startActivity(it); 
    Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1"); 
    Intent it = new Intent(Intent.ACTION_VIEW, uri); 
    startActivity(it); 
    Market 相关 
    1. //寻找某个应用 
    2. Uri uri = Uri.parse("market://search?q=pname:pkg_name"); 
    3. Intent it = new Intent(Intent.ACTION_VIEW, uri); 
    4. startActivity(it); 
    5. //where pkg_name is the full package path for an application 
    1. //显示某个应用的相关信息 
    2. Uri uri = Uri.parse("market://details?id=app_id"); 
    3. Intent it = new Intent(Intent.ACTION_VIEW, uri); 
    4. startActivity(it); 
    5. //where app_id is the application ID, find the ID 
    6. //by clicking on your application on Market home 
    7. //page, and notice the ID from the address bar 
    Uninstall 应用程序 
    1. Uri uri = Uri.fromParts("package", strPackageName, null); 
    2. Intent it = new Intent(Intent.ACTION_DELETE, uri); 
    3. startActivity(it); 
     
    TrackBack:http://wallage.blog.163.com/blog/static/1738962420104264340803/
  • 相关阅读:
    多线程在javaweb中的应用
    Class类是什么? Class.forName()是干什么的?
    JDBC学习笔记
    jsp
    VMware虚拟机中red hat linux ping不通宿主物理主机原因
    数据库设计原则(装载)
    PHP实现正态分布的累积概率函数
    如何正确配置 Nginx + PHP ???
    PHP针对二维数组无限遍历变形研究
    easyui常用控件及参数说明
  • 原文地址:https://www.cnblogs.com/hdjjun/p/1749808.html
Copyright © 2020-2023  润新知