您的位置:首页 > 移动开发 > Android开发

安卓端打电话和发送短信

2016-07-21 09:58 471 查看
如图所示:



需求:

当点击打电话时会把电话号码带到打电话的界面,当长按打电话 时,会直接打出电话,当点击发短信时,会把电话号码,以及短信内容带 到发短信的界面,当长按发短信时会直接发出短信。

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)发短信的知识点上次已经给出
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android