Android利用Intent启动和关闭Activity

Android利⽤Intent启动和关闭Activity
⼀、简介
Android应⽤程序中⼀般都有多个Activity,在Activity中,通过调⽤StartActivity⽅法,并在该⽅法的参数中传递Intent对象,就可以实现不同Activity之间的切换和数据传递。
通过StartActivity⽅法传递intent对象来启动另⼀个Activity时,可分为两类:
l 显式启动:在创建的Intent对象中明确指定启动的是哪个Activity;
l 隐式启动:安卓系统根据Intent的动作和数据决定应该启动哪个Activity。
1、显式启动Activity
通过Intent显式启动⼀个Activity时,必须先创建⼀个Intent对象,并在该对象的构造函数中指明要启动的⽬标组件名。例如:
var intent = new Intent(this, typeof(Activity1));
StartActivity(intent);
在这种情况下,除了intent对象以外,没有其他⽅式可以匹配组件名。
2、隐式启动Activity
隐式启动Activity是指Android系统根据过滤规则⾃动去匹配对应的Intent,即不需要在Intent对象中明确指明启动的是哪个Activity,⽽是让Android系统来决定应该启动谁。在这种情况下,Android系统会⾃动匹配最适合处理intent的⼀个或多个Activity。匹配的Activity可能是应⽤程序⾃⾝的,也可能是Android系统内置的,还可能是第3⽅应⽤程序提供的。因此,隐式启动这种⽅式更强调了Android应⽤程序中组件的复⽤性。
注意,如果希望让你的程序通过Activity展⽰某些动作,例如打电话、发邮件、发短信,或者使⽤activity中的数据等。这时候,⾸先考虑调⽤系统提供的功能去实现(即:隐式启动Activity的⽅式)。在这种情况下,只需要通过Intent指定相应的动作即可,这是intent真正体现其价值的地⽅。例如:
数据加密网关var call = new Intent(Intent.ActionCall); //初始化⼀个电话呼叫
call.SetData(Android.Net.Uri.Parse("tel:138********"));
StartActivity(call);
下⾯是常⽤的⼀些动作常量:
注意:⽤C#表⽰这些动作常量时,键⼊“Intent.”然后选择⼀个常量即可。这些可选择的常量去掉了分隔单词的下划线,并将每个单词的⾸字母改为⼤写,其他字母改为⼩写,例如:Intent.ActionCall。
除了指定相应的动作以外,还可以同时指定Uri,这种情况下,Android会调⽤内置的浏览器来隐式启动⼀个Activity。例如:
Intent intent=new Intent(Intent.ActionView, Uri.Parse("le");
手机linux操作系统StartActivity(intent);
3、关闭Activity
调⽤Finish()可终⽌activity。也可以调⽤FinishActivity() 来终⽌你之前启动了的⼀个独⽴的activity。
记住:显式关闭Activity仅⽤于你绝对不想让⽤户再返回这个activity的实例的情况。多数情况下,都不应该显式调⽤Finish()或者FinishActivity()来关闭acitivity,⽽是应该让系统为你去管理它。⼆、⽰例—ch1201IntentDemo
该例⼦演⽰如何通过Intent,以及如何启动另⼀个Activity。
本例⼦需要应⽤程序具有“CALL_PHONE”权限。
1、运⾏截图
运⾏前先在模拟器中添加⼀些联系⼈及其电话号码(模拟器本⾝有这个功能,直接在模拟器中操作即可),然后再测试要拨出的号码。
下⾯左图是该例⼦的主界⾯,右图是单击【打电话】按钮后,在第2个界⾯中拨出的电话号码截图。
下图是单击【转到活动1】按钮后的运⾏截图。
2、主要设计步骤
(1)添加“CALL_PHONE”权限
在【解决⽅案资源管理器】中,⿏标双击项⽬的【Properties】进⼊下⾯的界⾯,然后勾选“CALL_PHONE”选项:
设置后,系统就会⾃动在l中添加下⾯的代码:
<uses-permission android:name="android.permission.CALL_PHONE" />
(2)添加ch1201_Main.axml
在Resources\layout⽂件夹下添加该⽂件,模板选择【Layout】:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"账本网
android:text="打电话" />
<Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="转到活动1" />
</LinearLayout>
(3)添加ch1201_Layout1.axml
在Resources\layout⽂件夹下添加该⽂件,模板选择【Layout】:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="这是活动1的界⾯\n\n提⽰:按【Back】按钮返回"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"sb4
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:layout_gravity="center"
android:layout_margin="30dp" />
</LinearLayout>
(4)添加ch1201IntentDemoMain.cs
在SrcDemos⽂件夹下添加该⽂件,模板选择【Activity】:牙模
using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;
namespace MyDemos.SrcDemos
{
[Activity(Label = "【例12-1】Intent基本⽤法1")]
public class ch1201IntentDemoMain : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.ch1201_Main);
//启动电话拨号的Activity
FindViewById<Button>(Resource.Id.btn1).Click += delegate
{
var call = new Intent(Intent.ActionCall);
call.SetData(Android.Net.Uri.Parse("tel:138********"));
StartActivity(call);
};
//启动⾃定义的Activity
FindViewById<Button>(Resource.Id.btn2).Click += delegate
{
var intent = new Intent(this, typeof(ch1201IntentDemoActivity1));
StartActivity(intent);
};
}
}
}
(5)添加ch1201IntentDemoActivity1.cs
在SrcDemos⽂件夹下添加该⽂件,模板选择【Activity】:
using Android.App;
using Android.OS;
namespace MyDemos.SrcDemos
{
[Activity(Label = "【例12-1】Intent基本⽤法1")]
public class ch1201IntentDemoActivity1 : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.ch1201_Layout1);u盾客户端
}
}
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

本文发布于:2024-09-22 18:25:38,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/2/277042.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:启动   系统   对象   动作   匹配   添加   拨出
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议