问题描述
先生
我是Android上的新手,现在在发送和接收短信方面遇到问题.实际上,我在项目名称ZigbeeActivity,SettingActivity,OnOffActivity等中分别有5个Activity类,现在我想发送一个SMS,这是一个单独的类SendSMS和SMSReceiver类从SettingActivit编写并调用一个函数到SendSMSclass的SMSsend,但是在发送致命异常时会在Settingctiviy屏幕上显示.我将SendSMS类的源代码绑定在pl下方.帮帮我.
hi sir
I am new on Android and I am now being face a problem in sms sending and receiving Actually I have 5 Activity class in project name ZigbeeActivity,SettingActivity,OnOffActivity etc each and a seprate class SendSMS and SMSReceiver Class now I wish to send a sms which compose from SettingActivit and call a function to SMSsend of SendSMSclass but when sending the Fatal Exception is shown on Settingctiviy screen.I have tied the source code of SendSMS class below pl. help me.
package com.sms;
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 Object obj;
public SendSMS(Object object){
this.obj = object;
}
//---sends an SMS message to another device---
@SuppressWarnings("unused")
public void sendSMS(String phoneNumber, String message)
{
try{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getActivity(this, 0, new Intent(this,obj.getClass()), 0);
PendingIntent deliveredPI = PendingIntent.getActivity(this, 0, new Intent(this,obj.getClass()), 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 sent",
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, deliveredPI);
}catch(Exception e){
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
感谢n问候
Om Parkash
thanks n regards
Om Parkash
推荐答案
这篇关于关于Android中的致命意外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!