用于发送消息和获取送达报告的android应用程序不起作用
我正在创建一个android应用程序,它使用广播接收器发送消息并返回交付报告。 应用程序发送消息,但我没有收到任何传递报告来通知我消息已传递 有人能帮我解决这个问题吗 显示 SMS.java用于发送消息和获取送达报告的android应用程序不起作用,android,sms,broadcastreceiver,Android,Sms,Broadcastreceiver,我正在创建一个android应用程序,它使用广播接收器发送消息并返回交付报告。 应用程序发送消息,但我没有收到任何传递报告来通知我消息已传递 有人能帮我解决这个问题吗 显示 SMS.java package com.devleb.smssenddemo; 导入android.content.BroadcastReceiver; 导入android.content.Context; 导入android.content.Intent; 导入android.os.Bundle; 导入android.t
package com.devleb.smssenddemo;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.telephony.gsm.sms消息;
导入android.widget.Toast;
公共类SMS扩展广播接收机{
@凌驾
公共void onReceive(上下文、意图){
//TODO自动生成的方法存根
Bundle=intent.getExtras();
SmsMessage[]msgs=null;
字符串str=“”;
if(bundle!=null){
Object[]pdus=(Object[])bundle.get(“pdus”);
msgs=新SMS消息[PDU.length];
对于(int i=0;i
这里有一个来自Manikandan的有用链接,他在其中帮助另一个人解决交付报告问题。这真是一个可耻的人,我有一个可以发送和接收短信的应用程序,但我还没有实现交付报告。您是否有任何具体的错误可以帮助我们?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.devleb.smssenddemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.devleb.smssenddemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SMS" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" >
</action>
</intent-filter>
</receiver>
</application>
</manifest>
package com.devleb.smssenddemo;
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.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.Menu;
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 {
EditText edit_txt_phone;
EditText edit_txt_SMS;
Button btn;
// private static final String SMS_DELIVERED = "SMS_DELIVERED";
BroadcastReceiver smsDelivered = new SMSdelivered();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit_txt_phone = (EditText) findViewById(R.id.editText1);
edit_txt_SMS = (EditText) findViewById(R.id.editText2);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String strphone = edit_txt_phone.getText().toString();
String strSMS = edit_txt_SMS.getText().toString();
if (strphone.length() > 0 && strSMS.length() > 0) {
sendSMS(strphone, strSMS);
} else {
Toast.makeText(getBaseContext(),
"plz enter the phone or the sms",
Toast.LENGTH_SHORT).show();
}
}
private void sendSMS(String phoneNo, String SMS) {
// TODO Auto-generated method stub
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent pi = PendingIntent.getBroadcast(getBaseContext(),
0, new Intent(SENT), 0);
PendingIntent piDelevered = PendingIntent.getBroadcast(
getBaseContext(), 0, new Intent(DELIVERED), 0);
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));
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(phoneNo, null, SMS, pi, piDelevered);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
package com.devleb.smssenddemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;
public class SMS extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
str += "SMS from" + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}