简体   繁体   English

在双卡手机中使用 SMSmanager 发送短信?

[英]Send a SMS using SMSmanager in Dual SIM mobile?

Can anyone help me.谁能帮我。 I have a script sms android.我有一个脚本短信android。 This script is working properly.此脚本运行正常。 but I have a problem with the dual SIM card mobile phone.但是我的双SIM卡手机有问题。 My script was successfully sent messages in the SIM CARD 1. but not on the SIM card 2. Can anyone fix my script below.我的脚本已在 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();
                        }
                    }

                }



            }
        });

    }

}

For API 22 or greator and also get runtime permission for API 23对于 API 22 或更高版本,并获得 API 23 的runtime permission

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM