首页 > 其他分享 >Android学习笔记(三五):再谈Intent(下)-一些实践

Android学习笔记(三五):再谈Intent(下)-一些实践

时间:2023-08-04 23:33:46浏览次数:38  
标签:再谈 new intent activity Intent Android onCreate browser



Android的UI框架要求用户将他们的app分为activity,通过itent来进行调度,其中有一个main activity由Android的launcher在桌面中调用。例如一个日历的应用,需要查看日历的activity,查看单个事件的activity,编辑事件的activity等等。在查看日历的activity中,如果用户选择的某个事件,需要通过查看事件的activity来处理。这就是最近本的app UI框架,本次,我们将学习如何通过intent来完成。

Activity之间的关系

某种业务逻辑下,activity1需要知道被调起的activity2是否结束,activity2就是activity1的subactivity。

另一些业务逻辑下,activity1并不需要了解被它调起的activity2的运行情况,例如在邮件中打开一个附件,邮件activity1并不需要了解查看附件activity2是否结束。这种情况下,两个activity不是主从关系,而是peer关系,activity2是一个常规的activity。

步骤1:Make the Intent

intent会封装一个请求给Android,使得其他activity或者intent receiver知道如何处理。如果intent要launch的activity是我们自己编写的,可以很容易通过一个精确指定的intent来实现,例如:

new Intent(this, HelpActivity.class);

其中HelpActivity.class就是我们需要launch的component。这个activity只需要在AndroidManifest.xml文件中定义,但是不需要任何的inter filter,因为是通过直接请求的方式。如果我们需要打上一些data,即Uri,请求一个通用的activity,如下:

Uri uri=Uri.parse("geo:"+lat.toString()+","+lon.toString());
Intent i=new Intent(Intent.ACTION_VIEW, uri);

步骤2:Make the Call

根据activity之间的关系,通过intent的方法startActivity()或者startActivityForResult()来launch另一个activity。后者是需要知道activity2是否运行结束,以及运行的结果。

startActivityForResult()会向intent传递一个数字,这个数字对于calling activity来讲是唯一的。用于区分哪个activity。被调起的activity运行完,会在calling activity中触发onActivityResult(),我们可以通过这个唯一的数字来区分到底是哪个被调起的activity运行结束。我们还会得到下面的信息:

  1. result code,通过被调起的activity用setResult()来设置,一般是RESULT_OK和RESULT_CANCELLED,当然我们也可以定义我们自己的返回值,(从RESULT_FIRST_USER开始定义)
  2. 可选的String,用于传递某些返回信息,例如是一个URL来表述某个资源,有例如在ACTION_PICK中返回用户选取内容的数据。
  3. 可选的Bundle,用于返回所需的信息

例子一:一个简单的调用

Android学习笔记(三五):再谈Intent(下)-一些实践_string

例子通过输入经纬度,然后通过intent去launch地图应用。布局文件如下,顺带复习一下TableLayout的布局:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout ...... > 
   <TableLayout android:layout_width="fill_parent"   android:layout_height="wrap_content"
    android:stretchColumns="1,2"> 
       <TableRow> 
              <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                 android:paddingLeft="2dip" android:paddingRight="4dip" 
                 android:text="Location:" /> 
               <EditText android:id="@+id/c18_lat" 
                 android:layout_width="fill_parent"  android:layout_height="wrap_content"
                android:cursorVisible="true" 
                 android:editable="true" 
                 android:singleLine="true"  
                 android:layout_weight="1"/> 
               <EditText android:id="@+id/c18_lon" 
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                 android:cursorVisible="true" 
                 android:editable="true" 
                 android:singleLine="true" 
                 android:layout_weight="1" /> 
       </TableRow> 
   </TableLayout> 
         <Button android:id="@+id/c18_map"  android:layout_width="fill_parent" android:layout_height="wrap_content"
           android:text="Show Me!" /> 
 </LinearLayout>

源代码如下:

Android学习笔记(三五):再谈Intent(下)-一些实践_browser_02

public class Chapter18Test1 extends Activity{
    private EditText latitude = null; 
     private EditText longtitude =null; 
  
 
  
 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
         setContentView(R.layout.chapter_18_test1); 
  
 
        latitude = (EditText)findViewById(R.id.c18_lat);
         longtitude = (EditText)findViewById(R.id.c18_lon); 
         Button button = (Button)findViewById(R.id.c18_map); 
         button.setOnClickListener(new View.OnClickListener() { 
             public void onClick(View v) { 
                String _lat = latitude.getText().toString();
                 String _lon = longtitude.getText().toString(); 
                 //由于AVD缺乏地图应用,所以在模拟器中,会无法触发geo:作为URI的应用,会出现报错,如果仍然使用模拟器,可通过Web浏览的方式。例子采用模拟器。
                 //Uri uri=Uri.parse("geo:" + _lat + _lon);
                 Uri uri=Uri.parse("http://maps.google.com/?q=" +_lat + "," + _lon);
                 startActivity(new Intent(Intent.ACTION_VIEW,uri));             } 
         }); 
     } 
 }

例子二:Tab Browser

我们可以回顾一下Android学习笔记(二二): 多页显示-Tag的使用中最后一个例子,在Tab中显示Acitvity。如果我们需要将Activity作为content在tab中显示,我们提供一个Intent来launch所需的activity。在这个例子中,我们将在Tab的内容中通过intent调起网页浏览。

如果我们直接使用

Intent intent=new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(http://commonsware.com));

然后在tab中setContent(intent);我们会发现不能争取工作,因为在Android中,基于安全考虑,不能在tab中加载其他app的activity。因此我们需要自己来写。我们先写两个嵌入webkit browser的浏览器。

public class Chapter18Test2_1 extends Activity{
    private WebView browser = null;   
     protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState); 
         browser = new WebView(this); 
         setContentView(browser); 
         browser.loadUrl("http://commonsware.com"); 
     } 
 }
public class Chapter18Test2_2 extends Activity{
     private WebView browser = null;   
     protected void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState); 
         browser = new WebView(this); 
         setContentView(browser); 
         browser.loadUrl("http://www.android.com"); 
     } 
 }

calling activity的源代码如下,很简单

Android学习笔记(三五):再谈Intent(下)-一些实践_class_03

public class Chapter18Test2 extends TabActivity{ //使用TabActivity,已经有Tab的布局,因此我们不再需要布局xml文件。
    private TabHost host = null;  
  
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
         host=getTabHost(); 
         host.addTab(host.newTabSpec("one").setIndicator("CW").
                 setContent(new Intent(this,Chapter18Test2_1.class)));
         host.addTab(host.newTabSpec("Two").setIndicator("Android"). 
                 setContent(new Intent(this,Chapter18Test2_2.class)));
     } 
 }

我们回头在来看看这个例子的代码,同样的打开某个webkit browser的URL使用了两个activity,两个class,这是相当浪费的。我们可以将URL作为Intent的extra的信息传递。我们将上面的例子修改一下,通过Bundle来传递extra信息,并在calling activity中,可以很方便地增加tab。

被调用的activity:

public class Chapter18Test2_0 extends Activity{
    private WebView browser = null; 
     protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
         browser = new WebView(this); 
         setContentView(browser); 
         Bundle bundle = getIntent().getExtras();         browser.loadUrl(bundle.getString("http_uri"));
     } 
 }

calling的activity:

Android学习笔记(三五):再谈Intent(下)-一些实践_class_04

public class Chapter18Test2 extends TabActivity{
    private TabHost host = null; 
     protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
         host=getTabHost(); 
         addTab("one","CW","http://commonsware.com"); 
         addTab("Two","Android","http://www.android.com");
         addTab("Three","Blog","");
     } 
   //增加一个方法,用于添加tab 
    private void addTab(String tag, String indicator, String uri){ 
         Bundle bundle = new Bundle(); 
         bundle.putString("http_uri",uri); 
         Intent intent = new Intent(this,Chapter18Test2_0.class); 
         intent.putExtras(bundle);         host.addTab(host.newTabSpec(tag).setIndicator(indicator).setContent(intent));
     } 
 }

标签:再谈,new,intent,activity,Intent,Android,onCreate,browser
From: https://blog.51cto.com/u_9877302/6970100

相关文章

  • android mvvm实例解析
    MVVM架构,将整个应用分为三层,View层,VM层,Model层。其中View层单向引用VM层,VM层单向引用Model层。如上图。单向引用,而非双向引用,这是MVVM与MVP最大的区别。View层,只是单向引用VM层,VM层不需要引用View层,但是却可以更新View层。这是通过VM层的观察者模式实现的,在这里使用架构组件Liv......
  • Android面试必看手册,错过了金三银四可别再错过金九银十了
    眼看着时间一天一天地过去,距离金九银十也就二十多天的日子了,还有多少程序员是两眼摸黑不知道面试要做哪些准备的朋友?还不知道进大厂需要复习哪些资料的朋友可以看过来,知道有些朋友会没什么准备跟无头苍蝇一样所以博主早在一个月前就已经在各大网站和教育平台收集了大量面试相关的资......
  • 金九银十就要来了,为Android面试者准备的最全面试题+答案解析
    马上要到金九银十了,小编总结了一些面试题目包含百度/腾讯/小米/网易/搜狗/知乎/京东/360/瓜子,现在放上来,由于是自己整理,所以涵盖不全面的话诸位请谅解。抽象类与接口的区别?大体区别如下:抽象类可以提供成员方法的实现细节,而接口中只能存在public抽象方法;抽象类中的成员变量可以是......
  • android 休眠唤醒机制分析— wake_lock
    Android的休眠唤醒主要基于wake_lock机制,只要系统中存在任一有效的wake_lock,系统就不能进入深度休眠,但可以进行设备的浅度休眠操作。wake_lock一般在关闭lcd、tp但系统仍然需要正常运行的情况下使用,比如听歌、传输很大的文件等。Wakelock-wakelock在android的电源管理系统中扮演......
  • 自己开发的Android 软件发布贴(11月6日)
    Android开发其实去年就开始了,只是当时还只会用ActionScript因为原生Android开发需要学习java语言。从使用下来看,ActionScript运行效能不高做出来的软件基本没法用。经过几个月学习java现在已经能掌握Android开发给大家带来一些自己写的软件,希望能给大家带来帮助所有软件没有......
  • AndroidStudio2021.3logcat工具无法显示日志解决办法
    AndroidStudio2021.3logcat工具无法显示日志解决办法 https://blog.csdn.net/weixin_43623271/article/details/127876964  1.File->setting2.搜索logcat,->ExperimentalunChkEnablenewLogcattoolwindow以后提示时Dismissit ......
  • Android开发 Jetpack Compose Button
    前言  此篇博客讲解Button按钮一个简单的例子快速了解一下效果图代码@ComposablefunAPage(){Column(Modifier.fillMaxSize(),horizontalAlignment=Alignment.CenterHorizontally,verticalArrangement=Arrangement.Center){......
  • Android布局容器&视图元素
    1.界面布局简介在Android中,界面布局是指如何组织和排列用户界面中的视图(View)元素,以形成用户所看到的界面。Android提供了多种布局容器(LayoutContainer)和视图元素(ViewElement),用于实现各种不同类型的用户界面。常用的Android界面布局容器有以下几种:LinearLayout:线性布局容器,......
  • Android Dex文件详解
    前言==相信大家都熟悉dex文件,把一个apk给解压缩,就会得到一堆dex文件,但是这些dex文件是怎么来的,又有什么用,为什么这样设计,有进行思考过吗俗话说知其然,知其所以然,本篇文章开始探究一下这些底层实现细节。正文==不同的虚拟机JVMJVM是JavaVirtualMachine的简称,即Java虚拟机,它本质是......
  • Android 打印调用栈的方法
    转载1.Java层调用栈打印:(1)打印本地调用堆栈Log.i(TAG,Log.getStackTraceString(newThrowable()));//打印本地调用堆栈(2)打印远程调用堆栈importandroid.os.Binder;importandroid.app.IActivityManager;importandroid.util.Log;StringprocessName="";intpid=......