Отправить SMS с помощью SMSmanager на мобильный телефон с двумя SIM-картами?
Может кто-нибудь мне помочь. У меня есть скрипт смс андроид. Этот скрипт работает правильно. но у меня проблема с мобильным телефоном с двумя SIM-картами. Мой скрипт успешно отправил сообщения на SIM-карту 1., но не на SIM-карту 2. Может кто-нибудь исправить мой скрипт ниже.
package com.contohaplikasismssederhana;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class BuatPesan extends Activity {
EditText nomorKontak, text, simbol, alamat, telp, usaha, keterangan, aplikasi;
RadioButton rb0, rb1;
RadioGroup grup1;
// contact picker
private static final int CONTACT_PICKER_RESULT = 1001;
// phonecontact
public void doLaunchContactPicker(View view) {
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, uri);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String phone = "";
Cursor contacts = null;
try {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_RESULT:
// gets the uri of selected contact
Uri result = data.getData();
// get the contact id from the Uri (last part is contact
// id)
String id = result.getLastPathSegment();
// queries the contacts DB for phone no
contacts = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone._ID + "=?",
new String[] { id }, null);
// gets index of phone no
int phoneIdx = contacts.getColumnIndex(Phone.DATA);
if (contacts.moveToFirst()) {
// gets the phone no
phone = contacts.getString(phoneIdx);
EditText phoneTxt = (EditText) findViewById(R.id.nomorHp);
// assigns phone no to EditText field phoneno
phoneTxt.setText(phone);
} else {
Toast.makeText(this, "error", Toast.LENGTH_LONG).show();
}
break;
}
} else {
// gracefully handle failure
Toast.makeText(BuatPesan.this, R.string.belumdipilih,
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
} finally {
if (contacts != null) {
contacts.close();
}
}
}
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buatpesan);
final ImageButton send = (ImageButton) findViewById(R.id.send);
text = (EditText) findViewById(R.id.smsBox);
alamat = (EditText) findViewById(R.id.smsBox2);
simbol = (EditText) findViewById(R.id.smsBox3);
telp = (EditText) findViewById(R.id.smsBox4);
usaha = (EditText) findViewById(R.id.smsBox5);
keterangan = (EditText) findViewById(R.id.smsBox6);
nomorKontak = (EditText) findViewById(R.id.nomorHp);
rb0 = (RadioButton) findViewById(R.id.radio0);
rb1 = (RadioButton) findViewById(R.id.radio1);
aplikasi = (EditText) findViewById(R.id.smsBox10);
// fungsi untuk menampilkan isi pesan saat akan diteruskan
Intent i = getIntent();
if (i.getStringExtra("message") != null) {
}
send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (rb0.isChecked()) { //jika yang dipilih rb0
String pesan = aplikasi.getText().toString() + simbol.getText().toString() + text.getText().toString() + simbol.getText().toString() + alamat.getText().toString() + simbol.getText().toString() + telp.getText().toString() + simbol.getText().toString() + usaha.getText().toString() + simbol.getText().toString() + keterangan.getText().toString();
String pesan2 = text.getText().toString();
String pesan3 = alamat.getText().toString();
String pesan4 = keterangan.getText().toString();
String nomor = nomorKontak.getText().toString();
if (pesan4.length() > 0 && pesan3.length() > 0 && pesan2.length() > 0 && nomor.length() > 0) {
try {
// proses kirim sms
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(nomor, null, pesan, null, null);
// proses simpan sms yang terkirim
ContentValues values = new ContentValues();
values.put("address", nomor);
values.put("body", pesan);
getContentResolver().insert(
Uri.parse("content://sms/sent"), values);
Toast.makeText(BuatPesan.this,
"Pesan berhasil dikirim", Toast.LENGTH_SHORT)
.show();
finish();
} catch (Exception e) {
Toast.makeText(BuatPesan.this, "Pesan gagal dikirim",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}} else {
if (pesan4.length() < 1) {
Toast.makeText(BuatPesan.this,
"Isi Keterangan Tidak Boleh Kosong",
Toast.LENGTH_SHORT).show();
}else if(pesan3.length() < 1) {
Toast.makeText(BuatPesan.this,
"Isi Alamat Tidak Boleh Kosong",
Toast.LENGTH_SHORT).show();
}else if(pesan2.length() < 1) {
Toast.makeText(BuatPesan.this,
"Isi Nama Tidak Boleh Kosong",
Toast.LENGTH_SHORT).show();
}else if(nomor.length() < 1) {
Toast.makeText(BuatPesan.this,
"Isi Nomor Tujuan SMS Tidak Boleh Kosong",
Toast.LENGTH_SHORT).show();
}
}
} else if(rb1.isChecked()){
String pesan = text.getText().toString() + simbol.getText().toString() + alamat.getText().toString() + simbol.getText().toString() + telp.getText().toString() + simbol.getText().toString() + usaha.getText().toString() + simbol.getText().toString() + keterangan.getText().toString();
String pesan2 = text.getText().toString();
String pesan3 = alamat.getText().toString();
String pesan4 = keterangan.getText().toString();
String nomor = nomorKontak.getText().toString();
if (pesan4.length() > 0 && pesan3.length() > 0 && pesan2.length() > 0 && nomor.length() > 0) {
try {
// proses kirim sms
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(nomor, null, pesan, null, null);
// proses simpan sms yang terkirim
ContentValues values = new ContentValues();
values.put("address", nomor);
values.put("body", pesan);
getContentResolver().insert(
Uri.parse("content://sms/sent"), values);
Toast.makeText(BuatPesan.this,
"Pesan berhasil dikirim", Toast.LENGTH_SHORT)
.show();
finish();
} catch (Exception e) {
Toast.makeText(BuatPesan.this, "Pesan gagal dikirim",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}} else {
if (pesan4.length() < 1) {
Toast.makeText(BuatPesan.this,
"Isi Keterangan Tidak Boleh Kosong",
Toast.LENGTH_SHORT).show();
}else if(pesan3.length() < 1) {
Toast.makeText(BuatPesan.this,
"Isi Alamat Tidak Boleh Kosong",
Toast.LENGTH_SHORT).show();
}else if(pesan2.length() < 1) {
Toast.makeText(BuatPesan.this,
"Isi Nama Tidak Boleh Kosong",
Toast.LENGTH_SHORT).show();
}else if(nomor.length() < 1) {
Toast.makeText(BuatPesan.this,
"Isi Nomor Tujuan SMS Tidak Boleh Kosong",
Toast.LENGTH_SHORT).show();
}
}
} else {
String pesan = text.getText().toString() + simbol.getText().toString() + alamat.getText().toString() + simbol.getText().toString() + telp.getText().toString() + simbol.getText().toString() + usaha.getText().toString() + simbol.getText().toString() + keterangan.getText().toString();
String pesan2 = text.getText().toString();
String pesan3 = alamat.getText().toString();
String pesan4 = keterangan.getText().toString();
String nomor = nomorKontak.getText().toString();
if (pesan4.length() > 0 && pesan3.length() > 0 && pesan2.length() > 0 && nomor.length() > 0) {
try {
// proses kirim sms
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(nomor, null, pesan, null, null);
// proses simpan sms yang terkirim
ContentValues values = new ContentValues();
values.put("address", nomor);
values.put("body", pesan);
getContentResolver().insert(
Uri.parse("content://sms/sent"), values);
Toast.makeText(BuatPesan.this,
"Pesan berhasil dikirim", Toast.LENGTH_SHORT)
.show();
finish();
} catch (Exception e) {
Toast.makeText(BuatPesan.this, "Pesan gagal dikirim",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}} else {
if (pesan4.length() < 1) {
Toast.makeText(BuatPesan.this,
"Isi Keterangan Tidak Boleh Kosong",
Toast.LENGTH_SHORT).show();
}else if(pesan3.length() < 1) {
Toast.makeText(BuatPesan.this,
"Isi Alamat Tidak Boleh Kosong",
Toast.LENGTH_SHORT).show();
}else if(pesan2.length() < 1) {
Toast.makeText(BuatPesan.this,
"Isi Nama Tidak Boleh Kosong",
Toast.LENGTH_SHORT).show();
}else if(nomor.length() < 1) {
Toast.makeText(BuatPesan.this,
"Isi Nomor Tujuan SMS Tidak Boleh Kosong",
Toast.LENGTH_SHORT).show();
}
}
}
}
});
}
}
1 ответ
Для API 22 или greator, а также получить runtime permission
для API 23
public static SubscriptionManager mSubscriptionManager;
public static List<SubscriptionInfo> subInfoList;
public static List<Integer> sims;
public static void GetCarriorsInformation() {
sims = new ArrayList<Integer>();
mSubscriptionManager = SubscriptionManager.from(context);
subInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
for (int i = 0; i < subInfoList.size(); i++) {
sims.add(subInfoList.get(i).getSubscriptionId());
}
}
// Sim One
SmsManager sm = SmsManager.getSmsManagerForSubscriptionId(sims.get(0));
// Sim Two
SmsManager sm = SmsManager.getSmsManagerForSubscriptionId(sims.get(1));
sm.sendMultipartTextMessage(phoneNumberToSend, null, parts,
sentIntents, deliveryIntents);