Android开发短信相关的知识
来源:互联网 发布:淘宝客api怎么用 编辑:程序博客网 时间:2024/05/16 08:47
http://blog.csdn.net/yangshuangyue/article/details/51910083
一、打电话
1、添加打电话的权限在manifast文件中。
<uses-permission android:name="android.permission.CALL_PHONE"/>
2、使用Uri.parse(String a)创建Uri。
Uri uri = Uri.parse("tel:"+1008611);
3、创建打电话的意图。
Intent intent = new Intent(Intent.ACTION_CALL, uri);
4、启动系统打电话页面。
startActivity(intent);
二、发短信
方式一:直接发送短信
1、添加发送短信的权限在manifast文件中。
<uses-permission android:name="android.permission.SEND_SMS"/>
2、获取android.telephony.SmsManager对象(PS:android.telephony.gsm.SmsManager已经废弃)。
SmsManager smsManager = SmsManager.getDefault();
3、声明一个短信内容的常量。
String content = "Hello World!";
4、将短信内容分块,发送一条短信最多能够发送70个中文字符,超过这个值系统会将短信内容分为多块进行发送。
ArrayList<String> list = smsManager.divideMessage(content);
5、分条进行发送。
for (int i = 0; i < list.size(); i++) {
smsManager.sendTextMessage("10086", null, list.get(i), null, null);
}
方式二:调用系统的发送短信的界面,需要输入号码
1、创建意图
Intent intentFinalMessage = new Intent(Intent.ACTION_VIEW);
2、设置类型
intentFinalMessage.setType("vnd.android-dir/mms-sms");
3、打开系统短信界面
startActivity(intentFinalMessage);
方式三:调用系统的发送短信的界面,不需要输入号码
1、创建Uri,设置行为和号码
Uri uri2 = Uri.parse("smsto:"+10086);
2、创建意图。
Intent intentMessage = new Intent(Intent.ACTION_VIEW,uri2);
3、打开系统短信界面,号码已经填写,只需填写要发送
startActivity(intentMessage);
三、sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)
参数说明:
1、destinationAddress:给这个号码发送短信。
2、scAddress:使用这个号码发送短信,为null时表示使用本机发送。
3、text:短信内容。
4、sentIntent:发送短信成功或失败之后发送广播。
5、deliveryIntent:对方接受到短信之后发送广播。
四、发送广播的短信发送
1、注册广播
注册自己发送短信的广播:
SEND_SMS_ACTION = "3";
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
//根据结果码判断是否发送成功
if(Activity.RESULT_OK == getResultCode()){
Toast.makeText(MainActivity.this, "发送成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "发送失败", Toast.LENGTH_SHORT).show();
}
}
}, new IntentFilter(SEND_SMS_ACTION));
注册对方接受到短信的广播:
BACK_SMS_ACTION = "4";
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
//只要接收到这个广播,表示接收短信成功
Toast.makeText(MainActivity.this, "对方接收到短信", Toast.LENGTH_SHORT).show();
}
}, new IntentFilter(BACK_SMS_ACTION));
2、发送 短信,监听发送情况、监听对方接受情况,如果短信发送成功或失败
PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(SEND_SMS_ACTION), 0)会发送广播,
如果对方接受到短信
PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(BACK_SMS_ACTION), 0));会发送广播
for (int i = 0; i < list.size(); i++) {
smsManager.sendTextMessage("10086", null, list.get(i),
PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(SEND_SMS_ACTION), 0),
PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(BACK_SMS_ACTION), 0));
}
五、使用smsManager发送其它格式的短信
1、sendDataMessage(String destinationAddress, String scAddress, short destinationPort, byte[] data, PendingIntent sentIntent, PendingIntent
deliveryIntent)
其它参数一样,第三个参数
short destinationPort,给这个号码的这个端口号发送这条短信,短信内容为字节数组格式。
2、
sendMultipartTextMessage(String destinationAddress, String scAddress, ArrayList<String> parts,ArrayList<PendingIntent> sentIntents, ArrayList<PendingIntent> deliveryIntents)
给这个号码发送多条短信。
=========================================================================================
http://www.cnblogs.com/zhoujian315/archive/2013/06/08/3127394.html
Android 调用系统发短信界面,给指定号码发短信,并带短信内容
本文SDk 版本 Android 4.2.2很多时候 我们要使用 转发功能,即方便 、省时间又避免错误字。
直接使用系统的发短信功能,给指定号码发短信,并带短信内容,直接上 代码
工具类如下ContentUtil.java
复制代码
/**
*
*/
package com.zhoucj.messagedemo.util;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;
/**
* @author zhoucj
*
* 2013-6-8
*/
public class ContentUtil {
public static void showToast(Context context,String msg)
{
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}
/**
* 调用系统界面,给指定的号码发送短信,并附带短信内容
*
* @param context
* @param number
* @param body
*/
public static void sendSmsWithBody(Context context, String number, String body) {
Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(Uri.parse("smsto:" + number));
sendIntent.putExtra("sms_body", body);
context.startActivity(sendIntent);
}
}
复制代码
布局文件
复制代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/edittext1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="你好吗?"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edittext1"
android:text="转发" />
</RelativeLayout>
复制代码
Activity 文件如下
复制代码
package com.zhoucj.messagedemo;
import com.zhoucj.messagedemo.util.ContentUtil;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText editText;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText)findViewById(R.id.edittext1);
button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(editText.getText().toString()!="")
{
ContentUtil.sendSmsWithBody(MainActivity.this, "", editText.getText().toString());
}
}
});
}
@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;
}
}
复制代码
作者:zhoucj
出处:http://www.cnblogs.com/zhoujian315/archive/2013/06/08/3127394.html
原创作品,欢迎转载,转载是请注明 出处
- Android开发短信相关的知识
- Android短信的相关知识
- Android界面开发的相关知识
- Android界面开发的相关知识
- Android界面开发的相关知识
- android NDK开发库相关的知识
- Android开发相关概念知识
- Android开发像素相关知识
- Android测试开发相关知识
- Android短信开发相关信息记录
- Android应用与开发环境的相关知识总结
- phonegap+ionic+angularjs移动开发相关的知识(android)
- 软件开发的相关知识
- WEB开发的相关知识
- Web开发的相关知识
- WEB开发的相关知识
- JavaWEB开发的相关知识
- JavaWEB开发的相关知识
- iFunk:最美的风景只在走过的路上
- 一个函数有多少行代码比较合适?
- dubbo使用异步方式调用对象
- python常见错误集合
- 3D数学 学习笔记(1) 向量、坐标系
- Android开发短信相关的知识
- Atcoder CODE FESTIVAL 2017 qual C 总结+ABCD题解
- diff: /../Podfile.lock: No such file or directory diff: /Manifest.lock: No such file or directory er
- TCP三次握手四次断开
- 随机森林进行特征重要性度量的详细说明
- java中的接口
- Java多线程系列-悲观锁和乐观锁实战
- 开源项目网站
- set zip sort sorted