android使用Intent操作拨打号码发送短信
Activity程序Activity.java
package com.example.intentcaseproject;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button mybut=null;
private EditText tel=null;
private EditText content=null;
private Button addbut=null;
private Button telbut=null;
private static final int PICK_CONTEACT_SUBACTIVITY=1;//自定义操作标记
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
this.mybut=(Button)super.findViewById(R.id.mybut);//取得组件
this.tel=(EditText)super.findViewById(R.id.tel);//取得组件
this.content=(EditText)super.findViewById(R.id.content);//取得组件
this.addbut=(Button)super.findViewById(R.id.addbut);//取得组件
this.telbut=(Button)super.findViewById(R.id.telbut);//取得组件
this.mybut.setOnClickListener(new OnClickListenerlmpl());//设置发送短信按钮事件
this.addbut.setOnClickListener(new ContactsOnClickListener());//设置查找联系按钮事件
this.telbut.setOnClickListener(new TelClickListener());// 设置拨打电话号码事件
}
private class TelClickListener implements OnClickListener{//拨号按钮
@Override
public void onClick(View v) {
String telStr=MainActivity.this.tel.getText().toString();//获取电话号码
Uri uri=Uri.parse("tel:"+telStr); //拨打电话号码的URI格式
Intent it=new Intent(); //实例化Intent
it.setAction(Intent.ACTION_CALL); //指定Action
it.setData(uri); //设置数据
MainActivity.this.startActivity(it);//启动Acitivity
}
}
private class ContactsOnClickListener implements OnClickListener{//联系人查找按钮+
@Override
public void onClick(View arg0) {
Uri uri=Uri.parse("content://contacts/people");//连接URi
Intent it=new Intent(Intent.ACTION_PICK,uri);//指定Intent
MainActivity.this.startActivityForResult(it, PICK_CONTEACT_SUBACTIVITY);//调用Intent
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case PICK_CONTEACT_SUBACTIVITY://接受返回数据
Uri ret=data.getData();//单个数据的URI
String selection=ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?";//查询条件
String[] selectionArgs={String.valueOf(ContentUris.parseId(ret))};//查询参数
Cursor result=super.managedQuery(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, selection, selectionArgs, null);//查询该联系人的全部号码
for(result.moveToFirst();!result.isAfterLast();result.moveToNext()){//循环取出号码
MainActivity.this.tel.setText(//设置到文本框中
result.getString(
result.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
break;
}
}
private class OnClickListenerlmpl implements OnClickListener{//发送短信按钮
@Override
public void onClick(View v) {
String telStr=MainActivity.this.tel.getText().toString();// 取出电话号码
String note=MainActivity.this.content.getText().toString();//取出短信内容
Uri uri=Uri.parse("smsto:"+telStr); //指定路径
Intent it=new Intent(); //实例化Intent
it.setAction(Intent.ACTION_SENDTO); //指定Action
it.putExtra("sms_body", note);
it.setType("vnd.android-dir/mms-sms");
it.setData(uri); //设置数据
MainActivity.this.startActivity(it); //启动Activity
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
界面配置文件,采用线性布局内嵌表格布局activity_mian.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<TextView
android:layout_width="90px"
android:layout_height="wrap_content"
android:text="号码:"/>
<EditText
android:id="@+id/tel"
android:layout_width="260px"
android:layout_height="wrap_content"
android:numeric="integer"/>
<Button
android:id="@+id/addbut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="90px"
android:layout_height="wrap_content"
android:text="内容:"/>
<EditText
android:id="@+id/content"
android:layout_width="260px"
android:layout_height="wrap_content"
android:gravity="top"
android:lines="6"/>
</TableRow>
</TableLayout>
<LinearLayout
android:orientation="horizontal"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/mybut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送短信" />
<Button
android:id="@+id/telbut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拨打电话 "/>
</LinearLayout>
</LinearLayout>
权限设置AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intentcaseproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.intentcaseproject.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
</manifest>
版权声明:本文为博主原创文章,未经博主允许不得转载。
android使用Intent操作拨打号码发送短信的更多相关文章
- android 入门 002 (拨打电话,发送短信)
一.拨打电话 1.首先做好界面,代码如下: layout =>activity_main.xml 中 <LinearLayout xmlns:android="http://sc ...
- Android-读取操作系统通话记录并/拨打电话/发送短信/复制号码到拨号盘
apps目录的contacts应用(有读取通话记录功能),是访问provider目录的provider.contacts应用(有暴露通话记录),所以要阅读Android操作系统源码-->pack ...
- Arduino+sim800C家居安防火灾报警 拨打电话 发送短信例程程序
家居安防报警器,参考程序. 火灾报警 涉及用sim800c发短信,拨打电话通知. 接线: Sim800c 3.3V -> Arduino 3.3V Sim800c GND -> Ardui ...
- iOS_拨打电话/发送短信
GitHub address : https://github.com/mancongiOS/makeACallAndSendMessage.git 功能一: 拨打电话 1.可以有提示框.提示该电话号 ...
- 调用 url_launcher 模块打开外部浏 览器 打开外部应用 拨打电话 发送短信
1.Flutter url_launcher 模块 Flutter url_launcher 模块可以让我们实现打开外部浏览器.打开外部应用.发送短信.拨打电话等功能. https://p ...
- IOS中调用系统拨打电话发送短信
一.调用打电话界面 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat ...
- Cocos2d-x3.3RC0通过JNI调用Android的Java层URI代码发送短信
1.Jni不在赘述.翻看前面博客 2.直接上代码 1)Java层,直接加在AppActivity.java中 public class AppActivity extends Cocos2dxActi ...
- Android软件开发之发送短信与系统短信库解析
今天我和同学们讨论一下Android平台下如何调用系统方法发送短信.接收短信.系统的短信库相关的问题.进入正题,我们先使用Eclipse工具模拟给自己的模拟器发送一条短信.在Eclipse下打开DDM ...
- phoneGap的Android下编写phonegap 发送短信插件
一.前端代码的编写 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
随机推荐
- Netty笔记
1 基本介绍 Bootstrap Netty应用程序通过设置 bootstrap(引导)类开始,该类提供了一个用于应用程序网络层配置的容器.Bootstrap有两种类型,一种是用于客户端的Bootst ...
- Unity3D之Mecanim动画系统学习笔记(四):Animation State
动画的设置 我们先看看Animation Clip的一些设置: Loop time:动画是否循环播放. 下面出现了3个大致一样的选项: Root Transform Rotation:表示为播放动画的 ...
- ecshop后台限制IP登录
ecshop是开源系统,所以难免会有漏洞 黑客攻击网站,往往是通过漏洞获取后台管理员权限,然后再做一些破坏 如果我们在后台文件里限制指定的IP才能登录后台,就相对安全多了 下面给出大家解决方案: ...
- java读取properties的工具类PropertiesUtil
package org.properties.util; import java.io.FileInputStream; import java.io.FileOutputStream; import ...
- LCD_ILI9320横竖屏方向的问题。
发现仅仅设置R03H是不能设置方向的,还需要设置R32H,R33H的坐标位置. 比如我现在是 R03H=0x1000H,R20H=239-x,R21H=319-y:竖直正向 R03H=0x1030H, ...
- Android学习笔记(3)
今天我试着往应用里添加广告,结果adView一操作就闪退,换了很多种方法都不行. 最后解决过程有点坑爹,原来是还没setcontentview就开始adview了,哈哈 虽然我现在菜得不行,还没入门. ...
- Python 动态语言
1.在C++中,Animal a = Person(); 这样写是不行的,因为a的内容不能使用Person的内容来填充. 2.在Python中,变量不需要声明,而且可以赋任何值.Python是如何做到 ...
- Android - FrameLayout覆盖顺序
FrameLayout覆盖顺序 本文地址: http://blog.csdn.net/caroline_wendy FrameLayout: Child views are drawn in a st ...
- Linux - 打印文件夹全部文件 代码(C)
列出文件夹全部文件 代码(C) 本文地址:http://blog.csdn.net/caroline_wendy 首先配置环境,參考:http://blog.csdn.net/caroline_wen ...
- [安卓学习]AndroidManifest.xml文件内容详解
一,重要性 AndroidManifest.xml是Android应用程序中最重要的文件之一.它是Android程序的全局配置文件,是每个 android程序中必须的文件.它位于我们开发的应用程序的根 ...