Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get both simcard operator name in dual SIM mobile

I want to know Both sim card's operator name when mobile is dual sim.In single SIM I got operator name programmatically But For duel SIM I can't although after so many search and try.

If I run My app in dual sim phone than I can get both sim card operator name in my app For example : Idea,Vodafone.

EDIT:

Is anyone know How to get sim operator name of IMEI no then I have IMEI no.

Code:

public class MainActivity extends Activity {

Button btnOne, btnTwo;
TextView tvInfo;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnOne = (Button) findViewById(R.id.btnOne);
    btnTwo = (Button) findViewById(R.id.btnTwo);
    tvInfo = (TextView) findViewById(R.id.tvInfo);

    TelephonyInfo telephonyInfo = TelephonyInfo.getInstance(this);

    boolean isDualSIM = telephonyInfo.isDualSIM();
    boolean isSIM1Ready = telephonyInfo.isSIM1Ready();
    boolean isSIM2Ready = telephonyInfo.isSIM2Ready();

    TelephonyManager manager = (TelephonyManager) getApplicationContext()
            .getSystemService(Context.TELEPHONY_SERVICE);

    try {
        telephonyInfo.imsiSIM1 = telephonyInfo.getDeviceIdBySlot(context,
                "getSimSerialNumberGemini", 0);
        telephonyInfo.imsiSIM2 = telephonyInfo.getDeviceIdBySlot(context,
                "getSimSerialNumberGemini", 1);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String number = manager.getLine1Number();

    String optName1 = getOutput(getApplicationContext(), "getCarrierName", 0);
    String optName2 = getOutput(getApplicationContext(), "getCarrierName", 1);

    final String carrierName = manager.getSimOperatorName();
    tvInfo.setText(" " + isDualSIM + " " + optName1 + " " + optName2 + " "
            + telephonyInfo.imsiSIM1 + " " + telephonyInfo.imsiSIM2 + " "
            + number + " " + isSIM1Ready + " " + isSIM2Ready);

    btnOne.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(
                    Settings.ACTION_ACCESSIBILITY_SETTINGS);
            startActivity(intent);

        }
    });

    btnTwo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            if (carrierName.equalsIgnoreCase("TATA DOCOMO")
                    || carrierName.contains("DOCOMO")) {
                startService(new Intent(MainActivity.this, USSD.class));
                String ussdCode = "*" + "111" + Uri.encode("#");
                startActivity(new Intent(Intent.ACTION_CALL, Uri
                        .parse("tel:" + ussdCode)));
            } else if (carrierName.equalsIgnoreCase("!dea")
                    || carrierName.contains("idea")) {
                startService(new Intent(MainActivity.this, USSD.class));
                String ussdCode = "*" + "121" + Uri.encode("#");
                startActivity(new Intent(Intent.ACTION_CALL, Uri
                        .parse("tel:" + ussdCode)));
            } else if (carrierName.equalsIgnoreCase("AIRTEL")
                    || carrierName.contains("airtel")) {
                startService(new Intent(MainActivity.this, USSD.class));
                String ussdCode = "*" + "123" + Uri.encode("#");
                startActivity(new Intent(Intent.ACTION_CALL, Uri
                        .parse("tel:" + ussdCode)));
            }

        }
    });

}

private static String getOutput(Context context, String methodName,
        int slotId) {
    TelephonyManager telephony = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);
    Class<?> telephonyClass;
    String reflectionMethod = null;
    String output = null;
    try {
        telephonyClass = Class.forName(telephony.getClass().getName());
        for (Method method : telephonyClass.getMethods()) {
            String name = method.getName();
            if (name.contains(methodName)) {
                Class<?>[] params = method.getParameterTypes();
                if (params.length == 1 && params[0].getName().equals("int")) {
                    reflectionMethod = name;
                }
            }
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    if (reflectionMethod != null) {
        try {
            output = getOpByReflection(telephony, reflectionMethod, slotId,
                    false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return output;
}

private static String getOpByReflection(TelephonyManager telephony,
        String predictedMethodName, int slotID, boolean isPrivate) {

    // Log.i("Reflection", "Method: " + predictedMethodName+" "+slotID);
    String result = null;

    try {

        Class<?> telephonyClass = Class.forName(telephony.getClass()
                .getName());

        Class<?>[] parameter = new Class[1];
        parameter[0] = int.class;
        Method getSimID;
        if (slotID != -1) {
            if (isPrivate) {
                getSimID = telephonyClass.getDeclaredMethod(
                        predictedMethodName, parameter);
            } else {
                getSimID = telephonyClass.getMethod(predictedMethodName,
                        parameter);
            }
        } else {
            if (isPrivate) {
                getSimID = telephonyClass
                        .getDeclaredMethod(predictedMethodName);
            } else {
                getSimID = telephonyClass.getMethod(predictedMethodName);
            }
        }

        Object ob_phone;
        Object[] obParameter = new Object[1];
        obParameter[0] = slotID;
        if (getSimID != null) {
            if (slotID != -1) {
                ob_phone = getSimID.invoke(telephony, obParameter);
            } else {
                ob_phone = getSimID.invoke(telephony);
            }

            if (ob_phone != null) {
                result = ob_phone.toString();
            }
        }
    } catch (Exception e) {
         e.printStackTrace();
        // Log.i("Reflection", "Result: " +  e.printStackTrace());
         return null;
    }

    return result;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}
like image 625
KrishnaJ Avatar asked Sep 19 '16 04:09

KrishnaJ


People also ask

How do you switch between SIMs on a Dual SIM phone?

On the Home screen, tap Apps → Settings → SIM card manager → Tap one or both of the switches for the SIM or USIM cards to activate them. Note: If both cards are activated, you can receive calls and messages on each card.

Can 2 SIM cards be active at once?

Dual SIM Dual Active (DSFA) This implementation allows you to make phone calls from both SIM cards inside your smartphone. It can also receive calls on either of the two SIM cards, at the same time. Both SIM cards are permanently active.

How do I find my carrier name on Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken a text view to show the phone network operator name.


2 Answers

Since API version 22 its possible

try following code for device having os greater than android 5.1

List<SubscriptionInfo> subscriptionInfos = SubscriptionManager.from(getApplicationContext()).getActiveSubscriptionInfoList();
for(int i=0; i<subscriptionInfos.size();i++)
{
    SubscriptionInfo lsuSubscriptionInfo = subscriptionInfos.get(i);
    Log.d(TAG, "getNumber "+ lsuSubscriptionInfo.getNumber());
    Log.d(TAG, "network name : "+ lsuSubscriptionInfo.getCarrierName());
    Log.d(TAG, "getCountryIso "+ lsuSubscriptionInfo.getCountryIso());
}

for more information : Documentation: SubscriptionManager - Android Hope it helps. Peace.

like image 135
O_o Avatar answered Nov 15 '22 04:11

O_o


For API >=22:

{
        SubscriptionManager subscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);

        List<SubscriptionInfo> subscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();

        if (subscriptionInfoList != null && subscriptionInfoList.size() > 0) {
            for (SubscriptionInfo info : subscriptionInfoList) {
                String carrierName = info.getCarrierName().toString();
                String mobileNo = info.getNumber();
                String countyIso = info.getCountryIso();
                int dataRoaming = info.getDataRoaming();

            }

        }
    }
like image 30
Anish Vahora Avatar answered Nov 15 '22 04:11

Anish Vahora