如何在广播接收器中使用setter?

时间:2014-12-15 15:56:44

标签: java android

我有java类以编程方式从Android应用程序发送短信。我想在发送meesage之后生成一个报告,我需要消息活动的状态

发送状态;

接收状态;

状态在toast中正确显示,但是当我尝试为这些设置setter时,它返回默认值。可能是因为

mContext.registerReceiver(new BroadcastReceiver()...........在另一个线程上,如何设置setter或者可以从此功能块中获取发送状态?

public class SMSSending
{
private String phoneNo;
private String message;
private Context mContext;
Settings.Global global;


public void setSendingStatus(String sendingStatus) {
this.sendingStatus = sendingStatus;
}

private String sendingStatus;

public void setReceivingStatus(String receivingStatus) {
this.receivingStatus = receivingStatus;
}

private String receivingStatus;

public SMSSending(String phoneNo, Context mContext, String testID)
{
this.phoneNo = phoneNo;
this.mContext = mContext;
message = "This is a test message from " + testID;
}

public String getSendingStatus()
{
return sendingStatus;
}

public String getReceivingStatus()
{
return receivingStatus;
}

public void sendingMessage()
{
try
{
sendingStatus = "Failed to send SMS message";
receivingStatus = "Failed to deliver SMS ";

String SENT = "sent";
String DELIVERED = "delivered";

PendingIntent sentPI = PendingIntent.getBroadcast(mContext, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(mContext, 0,new Intent(DELIVERED), 0);
//setSendingStatus("Transmission successful");
//Register for SMS send action
mContext.registerReceiver(new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{

switch (getResultCode())
{
case Activity.RESULT_OK:
sendingStatus = "Transmission successful";
break;
default:
sendingStatus = "Transmission failure";
break;
}
// Save sendingStatus in Database as test report for sending message.
Toast.makeText(mContext, sendingStatus,Toast.LENGTH_LONG).show();
setSendingStatus(sendingStatus);
}
}, new IntentFilter(SENT));

/* Register for Delivery event */
mContext.registerReceiver(new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
receivingStatus = "SMS Delivered Succesfully";
// Save sendingStatus in Database as test report for receiving message.


// This function will only be executed if message has been successful received
Toast.makeText(mContext, receivingStatus, Toast.LENGTH_LONG).show();
setReceivingStatus(receivingStatus);
}

},new IntentFilter(DELIVERED));
enter code here
//Send SMS
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, sentPI, deliveredPI);
}

catch (Exception ex)
{
sendingStatus = "Failed to send SMS message";
receivingStatus = "Failed to deliver SMS ";
}
}

0 个答案:

没有答案