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"/>

效果如图所示:

        

最新文章

  1. 构建自己的PHP框架--实现Model类(3)
  2. xmpp openfire smack 介绍和openfire安装及使用
  3. iOS企业级开发初级课程-UIView与控件(20集)
  4. [Scrapy] Mac安装Scrapy
  5. WCF学习心得------(七)消息协定
  6. POJ-2718 Smallest Difference
  7. (转)实战Memcached缓存系统(1)Memcached基础及示例程序
  8. c-整型家族(integer family)
  9. DIY一款C/C++编译器
  10. mysql分表方法实现
  11. php基础(六)Include
  12. GridView 编辑,更新,删除 等操作~~
  13. 【lucene系列学习四】log4j日志文件实现多线程的测试
  14. Jmeter接口自动化
  15. Confluence 6 配置管理员会话安全的备注
  16. [Mac]secureCRT私钥转换为mac ssh私钥
  17. ARM linux内核启动时几个关键地址【转】
  18. MyEclipse WebSphere开发教程:安装和更新WebSphere 6.1, JAX-WS, EJB 3.0(三)
  19. Spinnaker 介绍
  20. package.xml

热门文章

  1. 《C语言程序设计基础I》秋季学习总结
  2. postman pre-request-script 操作方法记录
  3. 【leetcode 简单】 第九十二题 第N个数字
  4. HDU 1561 The more, The Better (有依赖背包 || 树形DP)
  5. 数位DP入门(A - 不要62 HDU - 2089 &amp;&amp;B - Bomb HDU - 3555 )
  6. 自然语言处理词向量模型-word2vec
  7. Material Design In Action——重构bilibili客户端
  8. Find Minimum in Rotated Sorted Array I &amp; II
  9. aarch64_p1
  10. Percona XtraBackup 实现全备&amp;增量备份与恢复【转】