方法调用如下.
sendSMS("98********","This is test message");
方法签名如下.
/* * broadcastReceiver mBrSend; broadcastReceiver mBrReceive; */private voID sendSMS(String phoneNumber,String message) { ArrayList<PendingIntent> sentPendingIntents = new ArrayList<PendingIntent>(); ArrayList<PendingIntent> deliveredPendingIntents = new ArrayList<PendingIntent>(); PendingIntent sentPI = PendingIntent.getbroadcast(mContext,new Intent(mContext,SmsSentReceiver.class),0); PendingIntent deliveredPI = PendingIntent.getbroadcast(mContext,SmsDeliveredReceiver.class),0); try { SmsManager sms = SmsManager.getDefault(); ArrayList<String> mSMSMessage = sms.divIDeMessage(message); for (int i = 0; i < mSMSMessage.size(); i++) { sentPendingIntents.add(i,sentPI); deliveredPendingIntents.add(i,deliveredPI); } sms.sendMultipartTextMessage(phoneNumber,null,mSMSMessage,sentPendingIntents,deliveredPendingIntents); } catch (Exception e) { e.printstacktrace(); Toast.makeText(getBaseContext(),"SMS sending Failed...",Toast.LENGTH_SHORT).show(); }}
现在还有两个类SmsDeliveredReceiver,SmsSentReceiver如下.
public class SmsDeliveredReceiver extends broadcastReceiver {@OverrIDepublic voID onReceive(Context context,Intent arg1) { switch (getResultCode()) { case Activity.RESulT_OK: Toast.makeText(context,"SMS delivered",Toast.LENGTH_SHORT).show(); break; case Activity.RESulT_CANCELED: Toast.makeText(context,"SMS not delivered",Toast.LENGTH_SHORT).show(); break; }}
}
现在SMSSentReceiver.
public class SmsSentReceiver extends broadcastReceiver {@OverrIDepublic voID onReceive(Context context,"SMS Sent",Toast.LENGTH_SHORT).show(); break; case SmsManager.RESulT_ERROR_GENERIC_FAILURE: Toast.makeText(context,"SMS generic failure",Toast.LENGTH_SHORT) .show(); break; case SmsManager.RESulT_ERROR_NO_SERVICE: Toast.makeText(context,"SMS no service",Toast.LENGTH_SHORT) .show(); break; case SmsManager.RESulT_ERROR_NulL_PDU: Toast.makeText(context,"SMS null PDU",Toast.LENGTH_SHORT).show(); break; case SmsManager.RESulT_ERROR_RAdio_OFF: Toast.makeText(context,"SMS radio off",Toast.LENGTH_SHORT).show(); break; }}
}
现在Permissions打开你的AndroIDManifest.xml并添加下面的行
<uses-permission androID:name="androID.permission.SEND_SMS"/>
并完成…….
总结以上是内存溢出为你收集整理的如何使用Android在BACKGROUND中发送短信息?全部内容,希望文章能够帮你解决如何使用Android在BACKGROUND中发送短信息?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)