安卓端打电话和发送短信
2016-07-21 09:58
471 查看
如图所示:
![](http://img.blog.csdn.net/20160721093851615)
需求:
当点击打电话时会把电话号码带到打电话的界面,当长按打电话 时,会直接打出电话,当点击发短信时,会把电话号码,以及短信内容带 到发短信的界面,当长按发短信时会直接发出短信。
xml里的代码就不用多说
主类中代码
最重要的是设置访问权限
主要步骤有:
主要涉及的知识点
需求:
当点击打电话时会把电话号码带到打电话的界面,当长按打电话 时,会直接打出电话,当点击发短信时,会把电话号码,以及短信内容带 到发短信的界面,当长按发短信时会直接发出短信。
xml里的代码就不用多说
主类中代码
package com.example.callandsendsms; import java.util.ArrayList; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.telephony.SmsManager; import android.telephony.SmsMessage; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnLongClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity implements OnLongClickListener{ private EditText edt_number; private EditText edt_content; private Button btn_call; private Button btn_sendSMS; private OnClickListener onclickListener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(v==btn_call) { //Toast.makeText(MainActivity.this, "点击电话", 0).show(); String action = "android.intent.action.DIAL"; //创建Intent意图(隐式) Intent intent = new Intent(action); //得到电话号码 String number = edt_number.getText().toString(); //把电话号码传送过去 intent.setData(Uri.parse("tel:"+number)); //跳转 startActivity(intent); }else if(v==btn_sendSMS) { //Toast.makeText(MainActivity.this, "点击短信", 0).show(); Intent intent = new Intent(Intent.ACTION_SENDTO); //携带数据 String number = edt_number.getText().toString(); String content =edt_content.getText().toString(); intent.setData(Uri.parse("smsto:"+number)); intent.putExtra("sms_body", content); startActivity(intent); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); //点击事件 btn_call.setOnClickListener(onclickListener); btn_sendSMS.setOnClickListener(onclickListener); //长按事件 btn_call.setOnLongClickListener(this); btn_sendSMS.setOnLongClickListener(this); } protected void init() { edt_content = (EditText) findViewById(R.id.edt_content); edt_number = (EditText) findViewById(R.id.edt_number); btn_call = (Button) findViewById(R.id.btn_call); btn_sendSMS = (Button) findViewById(R.id.btn_sendSMS); } //07-20 13:12:04.956: I/ActivityManager(299): Displayed com.android.contacts/.activities.DialtactsActivity: +2s381ms //android.intent.action.DIAL @Override public boolean onLongClick(View v) { // TODO Auto-generated method stub if(v==btn_call) { //Toast.makeText(this, "长按电话", 0).show(); //07-20 14:01:39.566: I/ActivityManager(299): Displayed com.android.phone/.PrivilegedOutgoingCallBroadcaster: +772ms //android.intent.action.CALL android.intent.action.CALL //String action ="android.intent.action.CALL"; Intent intent = new Intent(Intent.ACTION_CALL); String number = edt_number.getText().toString(); intent.setData(Uri.parse("tel:"+number)); //跳转 startActivity(intent); }else if(v==btn_sendSMS) { // Toast.makeText(this, "长按短信", 0).show(); String content = edt_content.getText().toString(); String number = edt_number.getText().toString(); SmsManager manager = SmsManager.getDefault(); manager.sendTextMessage(number, null, content, null, null); /*SmsManager manager = SmsManager.getDefault(); ArrayList<String> texts = manager.divideMessage(content); for(String text:texts) { manager.sendTextMessage(number, null, text, null, null); }*/ } return true;//改为true后,长按后不会再有点击事件 } }
最重要的是设置访问权限
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission> <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
主要步骤有:
1)设置按钮,并将其初始化 2)设置监听事件,有两种:长按监听和点击监听 3)分别在长按监听和点击监听中判断是发短信还是打电话,写出相应的代码 1))创建Intent(隐式视图) 2))得到数据,携带数据 3))跳转
主要涉及的知识点
1)两种监听事件 2)Intent intent = new Intent("");//隐式视图 3)Intent.ACTION_SENDTO有的软件上直接给出,没有直接给出的就要查找activityManager 4)携带数据的两种形式 1))intent.setDate(Uri.parse(""));//最里面的括号之所以加上tel:或smsto是因为系统代码上是这样的格式 2))intent.putExtra(name,value); //这里的name也不是随便给的,是固定的,发送短信是sms_body 5)打开activity(跳转) 6)发短信的知识点上次已经给出
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories