android Intent

自学安卓开发可不是一个好玩的事儿,主要是网上的资料大多写的太深奥,有的只写理论和术语,有的只有代码没有解释,所以看了很多也不知道Intent的作用和如何实现,想必是自己的基础差过人品的原因。

其实想说明一个问题很简单,只需要三个步骤:1、举一个生活化的例子。2、说明在安卓环境下的应用方法、基本理论。3、再写一段代码演示给初学者即可。

Intent,翻译成“意图”,很有意思,很贴切,其实完全可以再大白话一点,就是“想干嘛”。想干嘛就是意图,它分为两种,显性意图和隐性意图。

显性意图

想像一下,如果中午带饭吃,带着一个包子,饿时就会想:我要吃包子。因为知道自己带了包子,还很好表达,随时都能吃,这个就叫“显性意图”。

通常显式意图用Intent.setComponent()或Intent.setClass()等方法,明确指定了要做什么事。比如:


 
 
  1. Intent intent = new Intent();
  2. intent.setClass(MainActivity.this, SecondActivity.class);
  3. startActivity(intent);

这三行大概是说:

1、定义一个新的意图;

2、意图:设置一个类(当前的活动页,目标活动页);

3、启动刚才定义的意图。

这样就调用了一个新的Activity活动页面出来(需要自己先建立好),当然这些基本都是固定的格式,显性意图相对容易理解,没什么可解释的。

隐性意图

还有一种情况,中午没带饭,这个时候饿了,就会想:我想吃点东西……这个时候,就相对麻烦了,因为要决定是出去吃,还是叫外卖,吃什么品牌的,吃具体的什么东西。找助理秘书来帮忙吧,就对助理秘书,“我想……”她就晕了,所以必须再表达的清楚一点“我想吃……”,她大概猜出你要吃午饭,而不是其它什么东西,然后给你一个清单,你从清单上选择好,她就去买了。在这个过程中,你的想法就叫“隐性意图”,因为说的不是很明确,也有好几份外卖店可选。

从刚才的举例来看,吃自己带的包子效率最高,同样使用自己编写的Activity效率也是最高,问题是不能天天吃包子,偶尔还要有点别的需求,这个时候就可借助外卖了,也就是调用其它APP。

在程序开发的过程中,如果是调用自己的Activity,就像从自带的包子一样,指名道姓就可以了。如果想引用其它的程序,比如看网页,可能你手机里有好几个网页浏览器,到底要用哪个?就要像刚才点外卖的过程一样,至少要说清楚筛选条件,系统就可以帮你找出符合条件的APP,包括你自己写的Activity,请你选择。

 Android系统会根据隐式意图中你提前设置好动作条件(action)、类别(category)、数据条件(URI和数据类型)找到最合适的APP来处理程序的需求。“动作”、“类别”、“数据”条件,好比“吃”、“包子”、“肉馅”这个意思。

在哪里设置呢?就在AndroidManifest.xml文件里,这个通常叫“功能清单文件”,这里可以设置意图过滤,即intent-filter里写的内容。

以下是建立了一个新Activity后,需要在功能清单文件中添加的内容,注册了新Activity的名称和筛选条件:


 
 
  1. <activity android:name=".SecondActivity">
  2. <intent-filter tools:ignore="AppLinkUrlError">
  3. <action android:name="android.intent.action.VIEW" />
  4. <category android:name="android.intent.category.DEFAULT" />
  5. <data android:scheme="http" android:host="www.baidu.com"/>
  6. <data android:scheme="http" android:host="www.youku.com"/>
  7. </intent-filter>
  8. </activity>
action的功能关键词是VIEW,是“浏览”。category是默认,data数据在这里指明了两条,如果有想用网络,指百度和优酷的,都符合我程序的筛选执行条件,其它的我不管。

在回到MainActivity里来写代码,比如在一个按钮里写了如下三行,只给程序一个条件setAction(注意黑体加粗与上边的加粗是对应的关系)。


 
 
  1. Intent intent = new Intent(Intent.ACTION_VIEW);
  2. intent.setAction("android.intent.action.VIEW");
  3. startActivity(intent);
这三行大概的意思是:定义新意图,新意图是浏览,启动新意图。

其中的”android.intent.action.VIEW”内容,是系统已经定义好的,初学者不需要我们修改什么。

再生成程序,执行看看什么样?


出现了好多可选择的应用,因为它们都适合刚才的隐式意图里的条件,就好比说“我要吃……”说的太模糊,就会选项太多,会给客户造成选择障碍。所以,现在我们要把条件再给程序说清楚点,把“类别”条件改为“数据”条件。


 
 
  1. Intent intent = new Intent(Intent.ACTION_VIEW);
  2. intent.setData(Uri.parse( "http://www.baidu.com/"));
  3. startActivity(intent);
数据条件指定的够确切了吧?我就要吃包子,我就要看百度。 再生成程序,执行看看什么样?


结果是只有两个应用程度符合隐式意图描述的条件。当然,如果我们打开的网页是优酷,同样也会出现这样的选项。如果打开的网页即不是优酷,也不是百度,那就会直接出系统自带的浏览器(因为我有这一个浏览器)。

因为我们在“功能清单文件”注册了我们的程序功能,在安装到手机上以后,不管哪个程序调用相关功能,比如在“记事本”APP要打开百度,也会显示我们刚才编写的Activity,是不是很有成就感?


Intent,是各个组件中互相通讯的关键纽带,除了启动Activity,还可以启动服务,绑定服务,通过intent还可以在广播之间传递数据。刚才我们是以“浏览网页”为例,隐性意图还可以:打开电话、短信软件,拨打指定号码(需要系统授权),发指定内容的短信(需要系统授权),播放指定音乐,安装和卸载指定的程序等等。

基本上,刚才我们讲了Intent的意思,显性意图和隐性意图的意思和应用案例,再回头翻翻理论的东西,想掌握这个知识点就不难了,其它更深入的内容需要大家继续找资料自学了。

把刚才的程序的代码共享给和我一样的初学者:

AndroidManifest.xml


 
 
  1. <?xml version= "1.0" encoding= "utf-8"?>
  2. <manifest xmlns:android= "http://schemas.android.com/apk/res/android"
  3. package= "com.example.jeffersli.myapplicationintent">
  4. <uses-permission android:name= "android.permission.CALL_PHONE" />
  5. <application
  6. android:allowBackup= "true"
  7. android:icon= "@mipmap/ic_launcher"
  8. android:label= "@string/app_name"
  9. android:roundIcon= "@mipmap/ic_launcher_round"
  10. android:supportsRtl= "true"
  11. android:theme= "@style/AppTheme">
  12. <activity android:name= ".MainActivity">
  13. <intent-filter>
  14. <action android:name= "android.intent.action.MAIN" />
  15. <category android:name= "android.intent.category.LAUNCHER" />
  16. </intent-filter>
  17. </activity>
  18. <activity android:name= ".SecondActivity">
  19. <intent-filter tools:ignore= "AppLinkUrlError">
  20. <action android:name= "android.intent.action.VIEW" />
  21. <category android:name= "android.intent.category.DEFAULT" />
  22. <data android:scheme= "http" android:host= "www.baidu.com"/>
  23. <data android:scheme= "http" android:host= "www.youku.com"/>
  24. </intent-filter>
  25. </activity>
  26. </application>
  27. </manifest>
activity_main.xml


 
 
  1. <?xml version= "1.0" encoding= "utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android= "http://schemas.android.com/apk/res/android"
  3. android:layout_width= "match_parent"
  4. android:layout_height= "match_parent"
  5. tools:context= "com.example.jeffersli.myapplicationintent.MainActivity">
  6. <Button
  7. android:id= "@+id/button1_browsePage"
  8. android:layout_width= "match_parent"
  9. android:layout_height= "wrap_content"
  10. android:onClick= "browsePageClick"
  11. android:text= "打开指定网页"
  12. tools:layout_editor_absoluteX= "0dp"
  13. tools:layout_editor_absoluteY= "44dp" />
  14. </android.support.constraint.ConstraintLayout>
MainActivity


 
 
  1. package com.example.jeffersli.myapplicationintent;
  2. import android.content.Intent;
  3. import android.net.Uri;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. public class MainActivity extends AppCompatActivity {
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. }
  13. //打开指定网页
  14. public void browsePageClick(View view) {
  15. Intent intent = new Intent(Intent.ACTION_VIEW);
  16. intent.setData(Uri.parse( "http://www.baidu.com/"));
  17. startActivity(intent);
  18. }
  19. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值