android - Send a SMS using SMSmanager in Dual SIM mobile? -
can me. have script sms android. script working properly. have problem dual sim card mobile phone. script sent messages in sim card 1. not on sim card 2. can fix script below.
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 uri of selected contact uri result = data.getdata(); // contact id uri (last part contact // id) string id = result.getlastpathsegment(); // queries contacts db 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 phone no phone = contacts.getstring(phoneidx); edittext phonetxt = (edittext) findviewbyid(r.id.nomorhp); // assigns phone no 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(); } { 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 = 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(); } } } } }); } }
for api 22 or 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(); (int = 0; < subinfolist.size(); i++) { sims.add(subinfolist.get(i).getsubscriptionid()); } } // sim 1 smsmanager sm = smsmanager.getsmsmanagerforsubscriptionid(sims.get(0)); // sim 2 smsmanager sm = smsmanager.getsmsmanagerforsubscriptionid(sims.get(1)); sm.sendmultiparttextmessage(phonenumbertosend, null, parts, sentintents, deliveryintents);
Comments
Post a Comment