我在Android上面临一个奇怪的问题。当我通过我的应用程序发送短信时,我想听取发送意图。这是我到目前为止所做的:
private void shootSMS(String number,long currentTime){
SmsManager smsManager = SmsManager.getDefault();
Intent sentIntent = new Intent(SentBroadcastReceiver.SENT_ACTION);
Intent deliveredIntent = new Intent(DeliveredBroadcastReceiver.DELIVERED_ACTION);
Bundle b = new Bundle();
b.putString("num",number);
b.putString("record_time",currentTime+"");
sentIntent.putExtras(b);
deliveredIntent.putExtras(b);
PendingIntent sentPendingIntent = PendingIntent.getBroadcast(this,(int)currentTime,sentIntent,PendingIntent.FLAG_ONE_SHOT);
PendingIntent deliveredPendingIntent = PendingIntent.getBroadcast(this,(int)currentTime,deliveredIntent,PendingIntent.FLAG_ONE_SHOT);
smsManager.sendTextMessage(number,null,getString(R.string.app_name),sentPendingIntent,deliveredPendingIntent);
}
现在的问题是,当调用广播接收器的onReceive()
方法时,我调用getResultCode()
方法,它总是返回-1!即使手机关闭,也无法跟踪短信是否已发送!
我使用GoSMSPro检查了号码并发送了一条失败的短信。有趣的是,当我将手机置于飞行模式时,我得到的结果代码等于2
SmsManager.RESULT_ERROR_RADIO_OFF
现在问题是这里有什么不对?
答案 0 :(得分:0)
sendTextMessage州的文档:
sentIntent:如果不为NULL,则在成功发送消息或失败时广播此PendingIntent。结果代码为Activity.RESULT_OK ...
如果你去查看Activity.RESULT_OK,则值为-1。
所以resultCode为-1实际上是RESULT_OK。
一般来说,最好不要查看数值,而是试图找出它们代表的常数。
答案 1 :(得分:0)
这样做:
private void sendSms(String phonenumber, String message) {
SmsManager manager = SmsManager.getDefault();
PendingIntent piSend = PendingIntent.getBroadcast(this, 0, new Intent(
SMS_SENT), 0);
PendingIntent piDelivered = PendingIntent.getBroadcast(this, 0,
new Intent(SMS_DELIVERED), 0);
int length = message.length();
if (length > MAX_SMS_MESSAGE_LENGTH) {
ArrayList<String> messagelist = manager.divideMessage(message);
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
int numParts = messagelist.size();
;
for (int i = 0; i < numParts; i++) {
sentIntents.add(PendingIntent.getBroadcast(this, 0,
new Intent(
SMS_SENT), 0));
deliveryIntents.add(PendingIntent.getBroadcast(this, 0,
new Intent(SMS_DELIVERED), 0));
}
manager.sendMultipartTextMessage(phonenumber, null,
messagelist, sentIntents, deliveryIntents);
} else {
manager.sendTextMessage(phonenumber, null, message, piSend,
piDelivered);
}
}
和你的接收者:
注意使用&#34; Activity.RESULT_OK&#34;而不是数字
private BroadcastReceiver deliveredreceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String info = "Delivery information: ";
switch (getResultCode()) {
case Activity.RESULT_OK:
info += "delivered";
break;
case Activity.RESULT_CANCELED:
info += "not delivered";
break;
}
Toast.makeText(getBaseContext(), info, Toast.LENGTH_SHORT).show();
}
};
许可:
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
此权限用于发送和接收短信。
答案 2 :(得分:0)
尝试此方法: 要发送短信,请使用SMS Manager中的sendTextMessage。
sendTextMessage(String destinationAddress, String scAddress, String text,PendingIntent sentIntent, PendingIntent deliveryIntent)
当邮件成功发送或失败时,将触发第一个Pending Intent参数(sentIntent)。接收此Intent的Broadcast接收器的结果代码将是以下之一。
Activity.RESULT_OK - To indicate a successful transmission.
Sms.Manager.RESULT_ERROR_GENERIC_FAILURE - To indicate a nonspecific failure
Sms.Manager.RESULT_ERROR_RADIO_OFF - To indicate the phone radio is turned off
Sms.Manager.RESULT_ERROR_NULL_PDU - To indicate a PDU failure.
SmsManager.RESULT_ERROR_NO_SERVICE - To indicate that no cellular service is currently available.
仅在收件人收到您的短信后才会触发第二个待处理参数(deliveryIntent)。
发送和发送事件后,通过Toast显示相应的消息。
<强> activity_main.xml中强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/bg"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/pn"
android:textSize="15sp"
android:textColor="@android:color/white"
android:background="@android:color/black"
/>
<EditText
android:id="@+id/phno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:ems="10" >
</EditText>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/msg"
android:textSize="15sp"
android:textColor="@android:color/white"
android:background="@android:color/black"
/>
<EditText
android:id="@+id/smstxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:background="@android:color/darker_gray"
android:lines="5"
android:gravity="top" />
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/b1" />
</LinearLayout>
<强> MainActivity.java 强>
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btnSend;
EditText etPhoneNo;
EditText etMsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etPhoneNo = (EditText) findViewById(R.id.phno);
etMsg = (EditText) findViewById(R.id.smstxt);
btnSend = (Button) findViewById(R.id.send);
btnSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String phoneNo = etPhoneNo.getText().toString();
String msg = etMsg.getText().toString();
try {
String SENT = "sent";
String DELIVERED = "delivered";
Intent sentIntent = new Intent(SENT);
/*Create Pending Intents*/
PendingIntent sentPI = PendingIntent.getBroadcast(
getApplicationContext(), 0, sentIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Intent deliveryIntent = new Intent(DELIVERED);
PendingIntent deliverPI = PendingIntent.getBroadcast(
getApplicationContext(), 0, deliveryIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
/* Register for SMS send action */
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String result = "";
switch (getResultCode()) {
case Activity.RESULT_OK:
result = "Transmission successful";
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
result = "Transmission failed";
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
result = "Radio off";
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
result = "No PDU defined";
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
result = "No service";
break;
}
Toast.makeText(getApplicationContext(), result,
Toast.LENGTH_LONG).show();
}
}, new IntentFilter(SENT));
/* Register for Delivery event */
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "Deliverd",
Toast.LENGTH_LONG).show();
}
}, new IntentFilter(DELIVERED));
/*Send SMS*/
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, msg, sentPI,
deliverPI);
} catch (Exception ex) {
Toast.makeText(getApplicationContext(),
ex.getMessage().toString(), Toast.LENGTH_LONG)
.show();
ex.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
希望这有帮助!