更新 我在另一部手机(4.1.2)上试过这个代码,它运行得很好。但是当我使用4.4.2尝试使用手机时,它不起作用,它会返回" Radio Off"。我的应用程序被设置为处理消息FYI的默认应用程序。
这是我发送短信的活动:
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>
当我在模拟器之间发送短信时,它工作正常,但是当我在手机上运行应用程序时,它会关闭收音机(我的手机不在飞行模式下)。
请告知。
答案 0 :(得分:0)
我做了一个去年发送短信的应用,但我没有发送方法的意图。 我是这样做的:
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),所以当发送短信时,什么都不会发生。
希望这有帮助。
答案 1 :(得分:0)
我修好了,你不能发送SIM卡2中的SIM卡...我设置SIM卡发送,SIM 1中的呼叫正在工作。