Android收发短信

发送短信:

首先在清单文件上, 加上发送短信的权限

  <uses-permission android:name="android.permission.SEND_SMS"/>

发送短信的核心代码

    public void sendMessageClick(View view){
        //获取短信管理器
        SmsManager smsManager = SmsManager.getDefault();
        String message = "3";
        //将短信拆分
        ArrayList<String> list = smsManager.divideMessage(message);

        for (int i = 0; i < list.size(); i++) {
            smsManager.sendTextMessage("10086", null, list.get(i), null, null);

        }
        Toast.makeText(MainActivity.this, "发送成功", Toast.LENGTH_SHORT).show();

    }

接收短信

对于接收短信, 我有点苦恼, 因为我想让我收到的信息可以在Activity中显示, 后来想到了个法子, 可以使用观察设计模式, 还不是很懂的哥们可以去我的另个一个博客中查看http://blog.csdn.net/u013144863/article/details/51489025

SMSReceiver.java

package com.lulu.lsms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;

public class SMSReceiver extends BroadcastReceiver {

    private IBinderSMSReciver iBinderSMSReciver;

    public SMSReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {


        System.out.println("来短信了...");

        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Object[] objs = (Object[]) bundle.get("pdus");
            SmsMessage[] smsMessages = new SmsMessage[objs.length];

            StringBuffer sb = new StringBuffer();
            String content = null;
            for (int i = 0; i < objs.length; i++) {

                smsMessages[i] = SmsMessage.createFromPdu((byte[])objs[i]);
                //获取发送的号码
                String number = smsMessages[i].getDisplayOriginatingAddress();
                //获取发送内容
               content = smsMessages[i].getDisplayMessageBody();
                sb.append(content);
            }
            System.out.println(sb.toString());
            iBinderSMSReciver.setUI(sb.toString());
        }

    }

    //观察者设计模式
    interface IBinderSMSReciver{
        public void setUI(String content);
    }
    //做个监听器
    public void setIBinderSMSReciverListene(IBinderSMSReciver iBinderSMSReciver){
        this.iBinderSMSReciver = iBinderSMSReciver;
    }

}

别忘了清单文件, 加上下面几句

    <uses-permission android:name="android.permission.RECEIVE_SMS" />
        <receiver
            android:name=".SMSReceiver"
            android:enabled="true"
            android:exported="true">
      </receiver>

xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<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="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"
    android:orientation="vertical"
    tools:context="com.lulu.lsms.MainActivity">

    <TextView
        android:id="@+id/tv_sms_content"
        android:text="短信内容"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java文件

package com.lulu.lsms;

import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements SMSReceiver.IBinderSMSReciver{
    public TextView tv_sms_content;
    private SMSReceiver smsReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_sms_content = (TextView) findViewById(R.id.tv_sms_content);

        smsReceiver = new SMSReceiver();
        IntentFilter intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");

        registerReceiver(smsReceiver, intentFilter);

        smsReceiver.setIBinderSMSReciverListene(this);
    }



    @Override
    public void setUI(String content) {
        if(content != null){
            tv_sms_content.setText(content);
        }

    }
}

对于接收短信, 鄙人之前使用的小米手机, 由于小米手机的”良心品牌” , 使得广播接收器接收不到..后来换成了其他手机的品牌做了测试是可以的!!!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值