Android:发送短信在模拟器上工作,但不在真实设备上
更新: 我已经在另一部手机(4.1.2)上试过这段代码,它完美地工作。但是当我使用4.4.2在手机上试用时,它不起作用,它返回“无线电关闭”。我的应用程序被设置为处理消息FYI的默认应用程序。Android:发送短信在模拟器上工作,但不在真实设备上
这里是我的活动发送短信:
public class ViewConversation extends Activity {
SQLiteManager sql;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inbox_conversation);
init();
initListView();
initSendSMS();
}
@Override
public void onBackPressed() {
startActivity(new Intent(this, MainLayout.class));
}
private void init() {
sql = new SQLiteManager(this);
}
private void initListView() {
ViewConversationAdapter adapter = new ViewConversationAdapter(this, getMessages());
ListView lv = (ListView) findViewById(R.id.lv_conversation);
lv.setAdapter(adapter);
}
private String[] getMessages() {
Bundle b = getIntent().getExtras();
String fk_conversation_info = b.getString("fk_conversation_info");
sql.open();
String[] result = sql.getConversationSMSs(fk_conversation_info);
sql.close();
return result;
}
/* ---------- SEND SMS PART ---------- */
private void initSendSMS() {
final EditText et_phone = (EditText) findViewById(R.id.et_addPhoneNum);
final EditText et_message = (EditText) findViewById(R.id.et_messageBody);
Button bt_send = (Button) findViewById(R.id.bt_send);
bt_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String phoneNumber = et_phone.getText().toString();
String message = et_message.getText().toString();
sendSms(phoneNumber, message);
}
});
}
private void sendSms(String phoneNumber, String message) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), PendingIntent.FLAG_UPDATE_CURRENT);
//---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;
}
ViewConversation.this.unregisterReceiver(this);
}
}, 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);
}
}
这是我此活动的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="vn.hak_developers.spamsms" >
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainLayout"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".setting.SettingMethods"
android:label="@string/app_name" >
</activity>
<activity
android:name=".viewsms.ViewBanSim"
android:label="@string/app_name" >
</activity>
<activity
android:name=".viewsms.ViewNhaDat"
android:label="@string/app_name" >
</activity>
<activity
android:name=".viewsms.ViewNganHang"
android:label="@string/app_name" >
</activity>
<activity
android:name=".viewsms.ViewThongBao"
android:label="@string/app_name" >
</activity>
<activity
android:name=".viewsms.ViewNhaHang"
android:label="@string/app_name" >
</activity>
<activity
android:name=".viewsms.ViewThuRac"
android:label="@string/app_name" >
</activity>
<activity
android:name=".viewsms.ViewConversation"
android:label="@string/app_name"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</activity>
<!-- BroadcastReceiver that listens for incoming SMS messages -->
<receiver android:name=".smsmanager.SmsReceiver"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_DELIVER" />
<action android:name="android.provider.Telephony.SMS_RECEIVER" />
</intent-filter>
</receiver>
<!-- BroadcastReceiver that listens for incoming MMS messages -->
<receiver android:name=".smsmanager.MmsReceiver"
android:permission="android.permission.BROADCAST_WAP_PUSH">
<intent-filter>
<action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
<data android:mimeType="application/vnd.wap.mms-message" />
</intent-filter>
</receiver>
<!-- Service that delivers messages from the phone "quick response" -->
<service android:name=".smsmanager.HeadlessSmsSendService"
android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE"/>
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</service>
</application>
</manifest>
它完美,当我仿真器之间发送短信,但是当我运行应用程序在我的手机上,它会关闭无线电(我的手机不处于飞行模式)。
请指教。
我做了一个应用程序,去年发送了短信,但我没有发送方法的意图。 我是这样做的:
sms.sendTextMessage(Number, null, message, null, null);
试试这个,也许它工作。
编辑
有没有必要使用所有的代码中sendSMS(String phoneNumber, String nessage)
简单的做到这些:
private void sendSMS(String phoneNumber, String message)
{
//PendingIntent pi = PendingIntent.getActivity(this, 0,
// new Intent(this, ViewConversation.class), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);//edited
Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_SHORT).show();
}
的PendingIntent
对象(PI)被简单地指向同一个活动( ViewConversation.java),所以当发送短信时,什么都不会发生。
希望这会有所帮助。
嗨阿什坎,谢谢你的帮助。我试过你的代码,但它也没有工作! –
我试过了你的代码,它崩溃了: java.lang.RuntimeException:无法恢复活动{vn.hak_developers.spamsms/vn.hak_developers.spamsms.views.msn.ViewConversation}:java.lang.NullPointerException –
请问您可以发布整个错误。并且,尝试删除pendingIntent部分。查看编辑。 –
我修好了,你不能发送如果SIM卡在SIM卡2 ...我设置SIM卡发送,SIM 1呼叫正在工作。
您需要在AndroidManifest.xml中设置正确的权限 – EJTH
嗨EJTH,我刚刚添加了我的清单代码。请检阅,谢谢。 –