网站建设费用计入固定资产,学习网站大全,网上做期末试卷的网站,自由策划网站建设1、发送短信涉及到权限#xff0c;我们需要把权限加上 2、当我们发送短信时#xff0c;不管发送是否成功#xff0c;接收方是否接收到#xff0c;系统都会发送广播 3、这时我们注册广播去接收一下就可以了 4、布局文件很简单#xff0c;里面就两个EditText和一个button 下… 1、发送短信涉及到权限我们需要把权限加上 2、当我们发送短信时不管发送是否成功接收方是否接收到系统都会发送广播 3、这时我们注册广播去接收一下就可以了 4、布局文件很简单里面就两个EditText和一个button 下面上代码里面有注释: 发送广播接收器: package com.example.fanlei.cutnotedemo;import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.widget.Toast;/*** 监听短信是否已经发送成功*/
public class MySmsSendReceiver extends BroadcastReceiver {Overridepublic void onReceive(Context context, Intent intent) {if (intent.getAction().equals(SMS_SEND)){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();break;case SmsManager.RESULT_ERROR_NO_SERVICE:break;case SmsManager.RESULT_ERROR_NULL_PDU:break;case SmsManager.RESULT_ERROR_RADIO_OFF:break;}}}
} 接收方的广播 package com.example.fanlei.cutnotedemo;import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;/*** 监听对方是否发已经接收到短信*/
public class MySmsDeliverReceiver extends BroadcastReceiver {Overridepublic void onReceive(Context context, Intent intent) {if (intent.getAction().equals(SMS_DELIVER)){switch(getResultCode()){case Activity.RESULT_OK:Toast.makeText(context,对方已接收,Toast.LENGTH_SHORT).show();break;case Activity.RESULT_CANCELED:break;}}}
} 主函数 package com.example.fanlei.cutnotedemo;import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;import java.util.List;public class MainActivity extends ActionBarActivity {private EditText editText1,editText2;private MySmsSendReceiver mySmsSendReceiver; //发送短信的广播private MySmsDeliverReceiver mySmsDeliverReceiver;//对方是否已经接收的广播
Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mySmsSendReceiver new MySmsSendReceiver();mySmsDeliverReceiver new MySmsDeliverReceiver();editText1 (EditText) findViewById(R.id.editText1);//手机号editText2 (EditText) findViewById(R.id.editText2);//内容//初始化启动意图Intent sendIntent new Intent();sendIntent.setAction(SMS_SEND);Intent deliverIntent new Intent();deliverIntent.setAction(SMS_DELIVER);//满足条件后执行Intentfinal PendingIntent send PendingIntent.getBroadcast(MainActivity.this,0 ,sendIntent,0);final PendingIntent deliver PendingIntent.getBroadcast(MainActivity.this,0,deliverIntent,0);//发送短信按钮findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {String phoneNumber editText1.getText().toString();String content editText2.getText().toString();if (phoneNumber.equals()){Toast.makeText(MainActivity.this,手机号码为空,Toast.LENGTH_SHORT).show();}else {//获得短信的管理者SmsManager sm SmsManager.getDefault();//短信字数限制如果70则需要拆分短信发送if (content.length() 70){ListString contents sm.divideMessage(content);for (String sms : contents){sm.sendTextMessage(phoneNumber,null,sms,send,deliver);//发送短信}}else {sm.sendTextMessage(phoneNumber,null,content,send,deliver);//发送短信}}}});//注册广播registerReceiver(mySmsSendReceiver,new IntentFilter(SMS_SEND));registerReceiver(mySmsDeliverReceiver,new IntentFilter(SMS_DELIVER));}
} 布局文件 RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:paddingLeftdimen/activity_horizontal_marginandroid:paddingRightdimen/activity_horizontal_marginandroid:paddingTopdimen/activity_vertical_marginandroid:paddingBottomdimen/activity_vertical_margintools:context.MainActivityEditTextandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:idid/editText1android:hint手机号android:inputTypephone/EditTextandroid:idid/editText2android:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_belowid/editText1android:layout_alignParentLefttrueandroid:layout_alignParentStarttrueandroid:hint内容/Buttonandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:idid/buttonandroid:layout_centerVerticaltrueandroid:layout_centerHorizontaltrueandroid:text发送短信 //RelativeLayout 小伙伴们可以用10086进行测试 转载于:https://www.cnblogs.com/819158327fan/p/4932788.html