Android中Intent的使用分为显示Intent和隐式Intent 之前已经介绍过显示Intent的用法了,今天来介绍一下隐式Intent的用法.

当我们在使用一款软件时,如果需要从该软件内部开始拨号或者发短信,则需要通过使用隐式Intent将当前应用的Activity跳转到系统的拨号程序或者发送短信程序.

总之 使用一下两种情况需要使用隐式Intent

1.从当前应用跳转到系统自带应用

2.从当前应用跳转到另一款应用软件中

下面就通过一个例子让大家更好的了解隐式Intent的用法,该程序的功能是使当前应用自动将号码或者短信数据携带跳转至系统拨号和发送短信程序,同时也可以实现直接在当前应用拨打电话和发送短信.下面就将代码贴出

先贴布局文件:activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/a1"
tools:context="yang_a20160221dial_sendmessage.exercise.MainActivity" > <LinearLayout android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="20dp"
android:orientation="horizontal" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#CCCCFF"
android:text="电话号码:" /> <EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入号码"
android:ems="10" > <requestFocus />
</EditText> </LinearLayout> <LinearLayout android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="10dp"
android:orientation="horizontal" > <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="短信内容:"
android:textColor="#CCCCFF"/> <EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入短信"
android:ems="10" > <requestFocus />
</EditText> </LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:orientation="horizontal"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:text="打电话"
android:textColor="#CCFF00"
android:id="@+id/btn1"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发短信"
android:textColor="#CCFF00"
android:id="@+id/btn2"/> </LinearLayout> </LinearLayout>

再贴Java代码:MainActivity.java

 package yang_a20160221dial_sendmessage.exercise;

 import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener, OnLongClickListener { private EditText editText1;
private EditText editText2;
private Button btn1;
private Button btn2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
editText1=(EditText) findViewById(R.id.editText1);
editText2=(EditText) findViewById(R.id.editText2);
btn1=(Button) findViewById(R.id.btn1);
btn2=(Button) findViewById(R.id.btn2); btn1.setOnClickListener(this);
btn2.setOnClickListener(this); btn1.setOnLongClickListener(this);
btn2.setOnLongClickListener(this); } @Override
public void onClick(View v) {//点击按钮进入拨打电话和编辑短信界面
if (v==btn1) {
Toast.makeText(MainActivity.this, "点击拨号", 0).show();
String action=Intent.ACTION_DIAL;//找到拨号界面
Intent i1=new Intent(action);//创建一个隐式Intent 使其跳转到打电话界面
//携带数据
String number=editText1.getText().toString();
i1.setData(Uri.parse("tel:"+number));//"tel"必须这么写
//StartActivity
startActivity(i1); }
else if(v==btn2){ Toast.makeText(MainActivity.this,"点击发短信", 0).show(); String action=Intent.ACTION_SENDTO;//ACTION_SENDTO
Intent intent=new Intent(action);//指向编辑短信界面
String number=editText1.getText().toString();//获取电话号码
String content=editText2.getText().toString();//获取短信内容
intent.setData(Uri.parse("smsto:"+number));//将电话号码携带过去
intent.putExtra("sms_body", content);//将短信内容携带过去
startActivity(intent);
} } @Override
public boolean onLongClick(View v) {//长按直接拨号和发短信 if (v==btn1) {
Toast.makeText(MainActivity.this, "正在拨号", 0).show();
Intent i2=new Intent(Intent.ACTION_CALL);
String number=editText1.getText().toString();
i2.setData(Uri.parse("tel:"+number));//前缀必须是"tel"指向打电话的界面
startActivity(i2);
}
else if(v==btn2){
Toast.makeText(MainActivity.this,"正在发短信", 0).show(); SmsManager manager=SmsManager.getDefault();
String number=editText1.getText().toString();
String content=editText2.getText().toString();
//text短信内容
//destinationAddress->->目的地地址 电话号码
//service center address->->service center address服务中心
manager.sendTextMessage(number, null, content, null, null); }
return true;//此事件已经本消费了,不会再出发点击

注意:要调用系统的拨号和发送短信程序需要在AndroidManifest中添加权限,具体代码如下

  <!-- 添加打电话的权限 -->>
<uses-permission android:name="android.permission.CALL_PHONE"/> <!-- 添加打电话的权限 -->>
<uses-permission android:name="android.permission.SEND_SMS"/>

效果如图所示:

        

[ 原创 ]学习笔记-Android中隐式Intent 的使用的更多相关文章

  1. [ 原创 ]学习笔记-Android 学习笔记 Contacts (一)ContentResolver query 参数详解 [转载]

    此博文转载自:http://blog.csdn.net/wssiqi/article/details/8132603 1.获取联系人姓名 一个简单的例子,这个函数获取设备上所有的联系人ID和联系人NA ...

  2. [ 原创 ]学习笔记-Android 中关于Cursor类的介绍

    此博文转载自:http://www.cnblogs.com/TerryBlog/archive/2010/07/05/1771459.html 主讲Cursor的用法 使用过 SQLite 数据库的童 ...

  3. Android动画学习笔记-Android Animation

    Android动画学习笔记-Android Animation   3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中 ...

  4. android隐式intent使用场景解析

    Android 隐式intent相信大家都有用过,大部分场景我们用显式intent已经能满足我们的业务需求,隐式intent大部分都是用来启动系统自带的Activity或Service之类的组件.昨天 ...

  5. Mina框架的学习笔记——Android客户端的实现

    Apache MINA(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络 ...

  6. Android学习笔记三:用Intent串联activity

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7513399.html 一:Intent Intent可以理解为 意图. 我们可以通过创建intent实例来定义 ...

  7. 学习笔记-----Android的View绘制过程

    边看源码边参考别人的博客等,做一下学习笔记. 要了解View的绘制,首先得知道View树的结构:(可以参考http://blog.csdn.net/qinjuning/article/details/ ...

  8. 第一行代码阅读笔记----显示隐式Intent的基本用法

    1.显示Intent意图明显,通过Intent启动另外一个活动,这是安卓中各组件进行交互的一种重要方式.一般用于启动活动,启动服务,发送广播等场景. 实现方法,这里我只说思路,实践还是要自己实操才能明 ...

  9. Android 隐式 Intent 跳转注意事项

    前几天正好看到<阿里巴巴 Android 开发手册>中提到的: “Activity 间通过隐式 Intent 的跳转,在发出 Intent 之前必须通过 resolveActivity 检 ...

随机推荐

  1. C++中全排列函数next_permutation用法

    最近做了TjuOj上关于全排列的几个题,室友告诉了一个非常好用的函数,谷歌之,整理如下: next_permutation函数 组合数学中经常用到排列,这里介绍一个计算序列全排列的函数:next_pe ...

  2. Python概念-Attr系列(林海峰教的)

    这个Attr系列是egon老师自创的,个人还是可以接受这种文化底蕴的,所以直接拿来用,也是毫无违和感的 所谓Attr系列,其实是__setattr__,__delattr__,__getattr__ ...

  3. JS设计模式——11.适配器模式

    适配器模式概述 适配器模式可用来在现有接口和不兼容的类之间进行适配.使用这种模式的对象又叫包装器(wrapper). 适配器特点 从表面看,适配器模式很像门面模式.她们都要对别的对象进行包装并改变其呈 ...

  4. 判断最小生成树是否为一(krustra)

    题目链接:https://vjudge.net/contest/66965#problem/K 具体思路: 首先跑一遍最短路算法,然后将使用到的边标记一下,同时使用一个数组记录每一个权值出现的次数,如 ...

  5. UNIX环境高级编程 第6章 系统数据文件和信息

    UNIX系统的正常运作需要用到大量与系统有关的数据文件,例如系统用户账号.用户密码.用户组等文件.出于历史原因,这些数据文件都是ASCII文本文件,并且使用标准I/O库函数来读取. 口令文件 /etc ...

  6. CCN与CDN区别

    CCN与CDN区别 相同点: 1.针对目前互联网上存在问题,提出解决方案,让数据传输更快更稳定. 2.都均衡网络流量. 区别: 1.CDN是内容分发网络,是基于目前的TCP/IP体系结构的补充方法.C ...

  7. 2016.07.13-vector<vector<int>>应用2——Two Sum扩展

    收获: vector<vector<int> >res,不能直接用res[j].push_back(number),因为res[j]是空的,没有初始化 可以先定义 vector ...

  8. Dream------Hadoop--FSDataInputStream和FSDataOutputStream

    一.FSDataInputStream    FileSystem中的open()方法实际上返回的是一个FSDataInputStream,而不是标准的java.io类.这个类是java.io.Dat ...

  9. 数据库名(DB_NAME)、实例名(Instance_name)、以及操作系统环境变量(ORACLE_SID)

    数据库名(DB_NAME).实例名(Instance_name).以及操作系统环境变量(ORACLE_SID) 在ORACLE7.8数据库中只有数据库名(db_name)和数据库实例名(instanc ...

  10. MySQL基础 - Navicat及HeidiSQL可视化数据库管理工具

    你还在使用终端界面查看数据库吗?是的,用来用去还是觉得命令行好用.....这里先留个位子,改天再介绍下这俩工具的使用,虽然好像觉得没啥需要介绍的.