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. 训练赛第一场D题

    解题报告:一开始不知道ATA的意思,后来才知道原来是转置矩阵乘以原来的矩阵.这题说起来比较麻烦就不说了,直接上代码: #include<cstdio> #include<cstrin ...

  2. numpy_array与PIL.Image之间的互转

    # conding:utf-8 import matplotlib.pyplot as plt import numpy as np import PIL.Image as image # 图片的读取 ...

  3. Go语言的各种Print函数

    Go语言的各种Print函数 func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) func Pr ...

  4. UNIX网络编程 第8章 基本UDP套接字编程

    UDP是无连接的,不需要accept,TCP通过accept API来接受连接,并且将连接客户端的信息写入到accept将返回的新socket中,该新socket中有服务端和客户端的IP地址和端口,因 ...

  5. linux中set、unset、export、env、declare,readonly的区别以及用法

    set命令显示当前shell的变量,包括当前用户的变量;   env命令显示当前用户的变量;   export命令显示当前导出成用户变量的shell变量.           每个shell有自己特有 ...

  6. Hibernate5笔记5--关联关系映射

    关联关系映射: 关联关系,是使用最多的一种关系,非常重要.在内存中反映为实体关系,映射到DB中为主外键关系.实体间的关联,即对外键的维护.关联关系的发生,即对外键数据的改变. 外键:外面的主键,即,使 ...

  7. 在使用FastJson开发遇到的的坑

    1.list中放入同一个对象,会出现内存地址引用{"$ref":"#[0]"},后台可以识别,但是前台不会识别 @Test public void testLi ...

  8. 【前端vue开发架构】vue开发单页项目架构总结

    为营销活动设计的前端架构 主要的技术栈为 Vuejs,Webpack,请自行阅读如下技术或者框架的文档: 一.基础说明: node (https://nodejs.org/en/) npm (http ...

  9. POJ 1141 Brackets Sequence(括号匹配二)

    题目链接:http://poj.org/problem?id=1141 题目大意:给你一串字符串,让你补全括号,要求补得括号最少,并输出补全后的结果. 解题思路: 开始想的是利用相邻子区间,即dp[i ...

  10. HDU 2609 How many(最小表示+set)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2609 题目大意: 题目大意有n个有01组成的字符串,每个字符串都代表一个项链,那么该字符串就是一个环状 ...