发送带有确认的短信 [英] Sending SMS with Confirmation
本文介绍了发送带有确认的短信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
先生,我是Android平台上的新手,我编写了一个单独的类用于发送带有确认代码的SMS,但是当我通过该代码发送SMS时,我遇到的问题是,由于Null Pointer异常,您的应用程序意外关闭了实际上我已经发送了短信通过在此定义的send_SMS方法从SettingScreen活动中获取,先生,请帮我在这方面将代码与查询绑定起来
谢谢
Om Parkash Kaushik
Hi
Sir , I am new on Android platform and I have written a separate class for Sending SMS with confirmation code but when I send SMS through this then I face a problem that Your application has closed unexpectedly due to Null Pointer exception Actually I have send the sms from SettingScreen Activity through send_SMS method defined here sir pl help me I bind that code with the query in this regards
thanks
Om Parkash Kaushik
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.app.PendingIntent;
import android.telephony.SmsManager;
import android.widget.Toast;
import android.app.Activity;
public class SendSMS extends Activity {
private Context ctx;
public SendSMS(Context object){
this.ctx = object;
}
//---sends an SMS message to another device---
public void send_SMS(String phoneNumber, String message)
{
try{
String SENT = "SMS_SENT";
// String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(ctx.getApplicationContext(), 0, new Intent(SENT),0);
//getBroadcast( ctx, 0, new Intent(ctx,ctx.getClass()), 0);
// PendingIntent deliveredPI = PendingIntent.getBroadcast( ctx, 0, new Intent(DELIVERED), 0);
/// ---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS_SNT",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//
// //---when the SMS has been delivered---
// registerReceiver(new BroadcastReceiver(){
// @Override
// public void onReceive(Context arg0, Intent arg1) {
// switch (getResultCode())
// {
// case Activity.RESULT_OK:
// Toast.makeText(getBaseContext(), "SMS delivered",
// Toast.LENGTH_SHORT).show();
// break;
// case Activity.RESULT_CANCELED:
// Toast.makeText(getBaseContext(), "SMS not delivered",
// Toast.LENGTH_SHORT).show();
// break;
// }
// }
// }, new IntentFilter(DELIVERED));
//
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, null);
}catch(Exception e){
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
推荐答案
更改以下行...
catch(Exception e)
致
catch(Throwable e)
完成后,您将得到一个异常(假定那里的代码可以正常工作.)
Change the following line...
catch(Exception e)
To
catch(Throwable e)
Once you do that you will get an exception (presuming the code you have there works.)
这篇关于发送带有确认的短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文