安卓Activity跳转的几种方式

安卓Activity跳转的几种方式本文转载于http://blog.sina.com.cn/s/blog_5140274d0100q4j7.html,本人仅作为学习交流之用,请大家尊重原创。第一种方式,用action来跳转。使用Action跳转,如果有一个程序的AndroidManifest.xml中的某一个Activity的IntentFilter段中定义了包含了相同的Action那么这个Intent就与这个目标Ac…

大家好,又见面了,我是你们的朋友全栈君。

本文转载于http://blog.sina.com.cn/s/blog_5140274d0100q4j7.html,本人仅作为学习交流之用,请大家尊重原创。

第一种方式,用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代码

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; 
 
 
@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代码

<?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代码

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代码

<?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> 

第三种方式:通过Uri进行页面跳转

一些Intent的常用法:

Java代码

显示网页 
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&amp;cbp=1,yaw,,pitch,zoom&amp;mz=mapZoom   
 
 
路径规划 
 
1. Uri uri = Uri.parse("http://maps.google.com/maps?f=d&amp;saddr=startLat startLng&amp;daddr=endLat endLng&amp;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. //&lt;uses-permission id="android.permission.CALL_PHONE" /&gt; 
 
 
传送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, "file:///sdcard/mysong.mp3"); 
5. sendIntent.setType("audio/mp3"); 
6. startActivity(Intent.createChooser(it, "Choose Email Client"));   
 
播放多媒体 
   Uri uri = Uri.parse("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   
 
 
 
1.        Uri uri = Uri.fromParts("package", strPackageName, null);   
2.        Intent it = new Intent(Intent.ACTION_DELETE, uri); 
3.        startActivity(it);  
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/143157.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 元素守恒计算方法_元素个数怎么算

    元素守恒计算方法_元素个数怎么算给定一个整数数组 nums,按要求返回一个新数组 counts。数组 counts 有该性质: counts[i] 的值是 nums[i] 右侧小于 nums[i] 的元素的数量。示例:输入:nums = [5,2,6,1]输出:[2,1,1,0] 解释:5 的右侧有 2 个更小的元素 (2 和 1)2 的右侧仅有 1 个更小的元素 (1)6 的右侧有 1 个更小的元素 (1)1 的右侧有 0 个更小的元素提示:0 <= nums.length <= 10^5-10^4

    2022年8月9日
  • linux安装lib包_linux生成静态库

    linux安装lib包_linux生成静态库前几天手里的智能锁项目,收到产品的建议(命令)说,就是人脸识别成功的时候,不要只显示摄像头捕捉到的图像,要弄个酷炫一点的背景,背景里图片中间有个圆圈,人脸就放到圆圈里也就是类似这样。。当然,这是我思考了好几个小时的结果,开始想不明白要怎么实现,其实想通了也很简单,三个步骤A把背景图像的RGB读出来out_bufB把摄像头采集到的图像读出来(分辨率和背…

    2022年10月31日
  • springmvc之@PathVariable 注解

    springmvc之@PathVariable 注解springmvc之@PathVariable 注解

    2022年4月24日
  • 压测TPS_测压管原理

    压测TPS_测压管原理1.TPS、并发量是什么关系?为什么有的地⽅要⽤TPS?有的地⽅要⽤并发?⾸先,TPS是⼀个吞吐速度的概念,就是每秒处理多少请求。是衡量系统处理能⼒的指标,⽽往往TPS的最⼤值,并⾮系统资源耗尽的时点,因为TPS和系统资源是⼀个抛物线的关系,就是当资源最优配置时往往是TPS最⾼的时间,当资源耗尽时,往往TPS也是⾮常低的。每个TPS指标都会对应当时的并发量。然后说说并发量,并发量往往是对⼀个系统同时操作的⼈数的,或者说同时产⽣的请求数的预估,来衡量系统的承载能⼒。⾔外之意,这个指标⽬的在于看能否同时承载

    2022年10月21日
  • IntentService的原理及使用

    在Android开发中,我们或许会碰到这么一种业务需求,一项任务分成几个子任务,子任务按顺序先后执行,子任务全部执行完后,这项任务才算成功。那么,利用几个子线程顺序执行是可以达到这个目的的,但是每个线程必须去手动控制,而且得在一个子线程执行完后,再开启另一个子线程。或者,全部放到一个线程中让其顺序执行。这样都可以做到,但是,如果这是一个后台任务,就得放到Service里面,由于Service和Ac

    2022年4月7日
  • Ubuntu “Failed to fetch”错误的解决方法

    Ubuntu “Failed to fetch”错误的解决方法设置妥当DNS服务器即可.$sudovim/etc/resolv.conf修改DNS:nameserver8.8.8.8nameserver8.8.4.4然后重启网络服务:sudo

    2022年7月1日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号