I am working with SMS Manager for sending sms in android.The code i am using is as below:
private void sendSms(String Phnno, String Message) {
if (Utils.checkSIM(MyActivity.this)) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(Phnno, null, Message, null, null);
Utils.showMessage(MyActivity.this,"","Thank You for your interest,please check your inbox for the results.");
} else {
showErrorMessage(MyActivity.this, "SIM Error!",
"Please insert SIM");
}
}
This code works for me perfectly on single sim phones but when i check this in dual sim phones i am getting following warning and SMS never sends.
01-11 15:56:13.664: W/sendTextMessage(6656): use single sim interface to sendTextMessage by double sim interface
Please suggest how i can achieve it on my dual sim phone.Thanks in Advance.
This Will work for both scenario. If Already user has been selected the default sim it will automatically takes that and goto the next process, Otherwise while click on the send button it will ask the confirmation for choose any sim to send the sms. we have tested its working fine.
Sample Source Code:
try
{
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body","Body");
sendIntent.putExtra("address", "PhoneNumber");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(),"SMS faild, please try again later!",Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
sendTextMessage() has the scAddress parameter. This is used to define the SMS center address. I think if you set it correctly, you'll be able to send a message.
You can find the number by following this tutorial:
http://algorithmic-indian.blogspot.hu/2011/03/how-to-change-message-center-number-in.html
You may try this as well how to get the smsc number of a phone in android? Apparently there doesn't seem to be a way to programmatically get the number.
Try this code for sim selection, then use SMS sending method to send an SMS!
//above Android API 22
if (Build.VERSION.SDK_INT > 22) {
//for dual sim mobile
SubscriptionManager localSubscriptionManager = SubscriptionManager.from(this);
if (localSubscriptionManager.getActiveSubscriptionInfoCount() > 1) {
//if there are two sims in dual sim mobile
List localList = localSubscriptionManager.getActiveSubscriptionInfoList();
SubscriptionInfo simInfo = (SubscriptionInfo) localList.get(0);
SubscriptionInfo simInfo1 = (SubscriptionInfo) localList.get(1);
final String sim1 = simInfo.getDisplayName().toString();
final String sim2 = simInfo1.getDisplayName().toString();
}else{
//if there is 1 sim in dual sim mobile
TelephonyManager tManager = (TelephonyManager) getBaseContext()
.getSystemService(Context.TELEPHONY_SERVICE);
String sim1 = tManager.getNetworkOperatorName();
}
}else{
//below android API 22
TelephonyManager tManager = (TelephonyManager) getBaseContext()
.getSystemService(Context.TELEPHONY_SERVICE);
String sim1 = tManager.getNetworkOperatorName();
}
Related
My code successfully sends MMS. The approach uses ConnectivityManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
NetworkRequest.Builder builder = new NetworkRequest.Builder();
builder.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_MMS);
NetworkRequest networkRequest = builder.build();
cm.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback()
{
#Override
public void onAvailable(Network network)
{
super.onAvailable(network);
// do MMS sending here using HTTP/network APIs
cm.unregisterNetworkCallback(this);
}
});
}
In a dual SIM phone, this approach seems to pick a default SIM and send MMS out of that number (I am not sure how the default is picked).
How does one specify a SIM slot or a Subscription ID as part of the NetworkRequest? Or is there another way?
Just a reiterate this question is only about MMS, not SMS. Also, the question is not about sending MMS programmatically (that is discussed elsewhere in SO). The question is specific to dual SIM devices.
Thank you
You can get sim info by below method
//above Android API 22
if (Build.VERSION.SDK_INT > 22) {
//for dual sim mobile
SubscriptionManager localSubscriptionManager = SubscriptionManager.from(this);
if (localSubscriptionManager.getActiveSubscriptionInfoCount() > 1) {
//if there are two sims in dual sim mobile
List localList = localSubscriptionManager.getActiveSubscriptionInfoList();
SubscriptionInfo simInfo = (SubscriptionInfo) localList.get(0);
SubscriptionInfo simInfo1 = (SubscriptionInfo) localList.get(1);
final String sim1 = simInfo.getDisplayName().toString();
final String sim2 = simInfo1.getDisplayName().toString();
}else{
//if there is 1 sim in dual sim mobile
TelephonyManager tManager = (TelephonyManager) getBaseContext()
.getSystemService(Context.TELEPHONY_SERVICE);
String sim1 = tManager.getNetworkOperatorName();
}
}else{
//below android API 22
TelephonyManager tManager = (TelephonyManager) getBaseContext()
.getSystemService(Context.TELEPHONY_SERVICE);
String sim1 = tManager.getNetworkOperatorName();
}
I would like to send sms by SmsManager its working but in first sim i have no balance in this case sms failed. so how to send sms by second sim only from my application.
other app like whatsApp send msg at reg time from my second sim if not availible balance in first sim.so i think its possible. pls help me
String mobileNo = phoneNumber1.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(mobileNo, null, "BAL", null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
I looked into SubscriptionManager class and found a method getActiveSubscriptionInfoForSimSlotIndex(simIndex) to get subscriptionInfo for perticular sim slot using this subscriptionInfo we can get subscriptionId of perticular simSlot. Using subscriptionID we can get SMSManager for desired simSlot
This worked for me:
public void sendSMS(final String number,final String text){
final PendingIntent localPendingIntent1 =
PendingIntent.getBroadcast(mContext, 0, new Intent(this.SENT), 0);
final PendingIntent localPendingIntent2 =
PendingIntent.getBroadcast(mContext, 0, new Intent(this.DELIVERED), 0);
if (Build.VERSION.SDK_INT >= 22)
{
SubscriptionManager subscriptionManager=((Activity)mContext).getSystemService(SubscriptionManager.class);
SubscriptionInfo subscriptionInfo=subscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(simIndex);
SmsManager.getSmsManagerForSubscriptionId(subscriptionInfo.getSubscriptionId()).sendTextMessage(number, null, text, localPendingIntent1, localPendingIntent2);
}
SmsManager.getSmsManagerForSubscriptionId(subscriptionInfo.getSubscriptionId()).sendTextMessage(number, null, text, localPendingIntent1, localPendingIntent2);
}
Where simIndex is 0 for sim1 and 1 for sim2
After analyzing the logs of Mlais MX28 phone (with a customized ROM), I found the following trick to switch the SIM for SMS sending. Unfortunately, it seems to switch the SIM globally for all apps (I couldn't figure out a way to 'save and restore' the prevailing setting yet). Plus, you will probably have to go through the trouble of finding out the IDs (sim_id) assigned to each of your SIM cards (probably via checking the phone's log output; I couldn't find a programmatic way to do this, either).
int simId = <place desired SIM ID here>;
Intent simIntent = new Intent("android.intent.action.SMS_DEFAULT_SIM");
simIntent.putExtra("simid", simId);
sendBroadcast(simIntent);
I'm not sure if this would work for you (although the code seems manufacturer-independent); but it's still worth a shot, I suppose.
Update:
Strangely, the earlier approach ceased to work after some attempts. However, after some more log analysis I came across another approach which uses direct settings manipulation (which, unfortunately, requires the android.permission.WRITE_SETTINGS permission).
ContentValues val = new ContentValues();
val.put("value", "here goes the preferred SIM ID");
getContentResolver().update(Uri.parse("content://settings/system"), val, "name='sms_sim_setting'", null);
I believe it is adaptable for other multi-SIM-concerned operations as well, as the settings database on my device contains entries for settings like gprs_connection_sim_setting, voice_call_sim_setting, video_call_sim_setting, etc.
I'm Trying the following code to get System phone number
TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
mPhoneNumber = tMgr.getLine1Number();
But it is not working.
In my phone, i use
Settings->About Phone-->Status-Phone Number
is there a simple app i can use ? , I m writing from Turkey. does sim card make a difference ?
Not all telecom carriers store the phone number on the SIM card. The approach you are using only works if the SIM card in the device has the mobile number stored on it.
If you cannot see your phone number in the mobile settings, then it means that it isn't stored on the SIM card. You will have to manually ask the user to enter it into your app if you absolutely need it. There is no other way.
You can try something like this. It works perfectly.
public String getPhoneNumber(Context context) {
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String num = mTelephonyMgr.getLine1Number();
return fillPhoneNumber(num);
}
private String fillPhoneNumber(String num) {
try{
if (num != null && num.length() > 0) {
if (num.length() >= 9) {
num = num.replaceAll("\\D", "");
if (num.substring(0, 1).equals("8")) {
num = "+3" + num;
} else if (num.substring(0, 1).equals("0")) {
num = "+38" + num;
} else if (num.substring(0, 1).equals("3")) {
num = "+" + num;
}
}
return num;
}
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
Note: Not all phones return a phone number.
There is no deterministic way to find the MSISDN. The above approach only works if SIM has stored the MSISDN. Other work around is to retrieve the number from facebook. It also may or may not work.
The only sure shot way is to create account with some SMS gateway provider like http://trumpia.com, then send SMS to the toll free number, and then call the API to retrieve the MSISDN.
I found this code in this forum but i have the problem that i can’t use this char (# or /u0023) in the phone number.
I know that this question was already asked but you may know the App "Go SMS Pro" and this app can actually call this number. Now I want to ask you whether you know how to manage it.
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
String number = "tel:*100#"; /This is the number
callIntent.setData(Uri.parse(asd));
startActivity(callIntent);
TelephonyManager tManager = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener listener = new PhoneStateListener();
tManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
} catch (ActivityNotFoundException activityException) {
Log.e("telephony-example", "Call failed", activityException);
}
Url encode it.
String number = "tel:*100%23";
callIntent.setData(Uri.parse(asd));
Or if you need a more general approach (like if you numbers change...) - encode the data part of the URI with a URLEncoder.
String number = "tel:"+ URLEncoder.encode("*100#");
....
Is there a way that I can access the other sim in my android? I have an android Cherry Mobile Orbit which is a dual sim android phone. I want to develop an SMS application for my phone, but only the 1st sim will be access. I want to access the other sim, so that when i press enter to send the message, there will be a prompt, that which sim the message will be sent.
I know that it's propreitary, but the phone manufacturer has no feedback about it. Is there any other way? tweaking or something?
I think you can use dex2jar or apktool to reverse those app which runs on dual sim card devices, once you found the related "service manager", "telephony manager",some interface stub, or some key words like "Gemini" you can then use Java Reflection, reflects all of method of this class. Then you can achieve this goal.
This method can be used to select one SIM from 2 SIMs!
//above Android API 22
if (Build.VERSION.SDK_INT > 22) {
//for dual sim mobile
SubscriptionManager localSubscriptionManager = SubscriptionManager.from(this);
if (localSubscriptionManager.getActiveSubscriptionInfoCount() > 1) {
//if there are two sims in dual sim mobile
List localList = localSubscriptionManager.getActiveSubscriptionInfoList();
SubscriptionInfo simInfo = (SubscriptionInfo) localList.get(0);
SubscriptionInfo simInfo1 = (SubscriptionInfo) localList.get(1);
final String sim1 = simInfo.getDisplayName().toString();
final String sim2 = simInfo1.getDisplayName().toString();
}else{
//if there is 1 sim in dual sim mobile
TelephonyManager tManager = (TelephonyManager) getBaseContext()
.getSystemService(Context.TELEPHONY_SERVICE);
String sim1 = tManager.getNetworkOperatorName();
}
}else{
//below android API 22
TelephonyManager tManager = (TelephonyManager) getBaseContext()
.getSystemService(Context.TELEPHONY_SERVICE);
String sim1 = tManager.getNetworkOperatorName();
}
Then to send an SMS you can use the below method
public static boolean sendSMS(Context context, String smsText) {
SmsManager smsMan = SmsManager.getDefault();
SharedPreferences prefs = context.getSharedPreferences("uinfo", MODE_PRIVATE);
String sim = prefs.getString("sim", "No name defined");
String simName = prefs.getString("simName", "No name defined");
String toNum = "";
try {
if (Build.VERSION.SDK_INT > 22) {
SubscriptionManager localSubscriptionManager = SubscriptionManager.from(context);
if (localSubscriptionManager.getActiveSubscriptionInfoCount() > 1) {
int simID = Integer.parseInt(sim);
List localList = localSubscriptionManager.getActiveSubscriptionInfoList();
SubscriptionInfo simInfo = (SubscriptionInfo) localList.get(simID);
ArrayList<String> parts = smsMan.divideMessage(smsText);
SmsManager.getSmsManagerForSubscriptionId(simInfo.getSubscriptionId()).sendMultipartTextMessage(toNum, null, parts, null, null);
}else{
ArrayList<String> parts = smsMan.divideMessage(smsText);
smsMan.sendMultipartTextMessage(toNum, null, parts, null, null);
}
return true;
}else{
ArrayList<String> parts = smsMan.divideMessage(smsText);
smsMan.sendMultipartTextMessage(toNum, null, parts, null, null);
}
} catch (Exception e) {
}
return false;
}
Officially right now there is no API to support this, you have to contact the device manufacturer.
Assuming that you are developing the app for your own phone, and you are willing to go through the trouble of finding out the IDs (sim_id) assigned to each of your SIM cards (probably via checking the phone's log outputs, searching for sim_id, which was what I did), you can use the following code to set the default SIM for SMS sending:
int simId = <place desired SIM ID here>;
Intent simIntent = new Intent("android.intent.action.SMS_DEFAULT_SIM");
simIntent.putExtra("simid", simId);
sendBroadcast(simIntent);
Combined with some other UI prompt stuff (for actually 'picking' the preferred SIM), this should do the trick.
I'm not at all sure if this approach would work for you (although the code seems quite 'standard'); I figured it out with trial and error on my Mlais MX28 (with a customized ROM). But it's still worth a shot, I suppose. :)
UPDATE:
Strangely, the solution stopped working unexpectedly after a few updates to the app I was working on. But I came across another way (which seems to be more promising). (I believe this can be extended for other SIM selection scenarios as well, as the settings cache contains entries with names gprs_connection_sim_setting, voice_call_sim_setting, video_call_sim_setting and the like.)
ContentValues val = new ContentValues();
val.put("value", "here goes the preferred SIM ID");
getContentResolver().update(Uri.parse("content://settings/system"), val, "name='sms_sim_setting'", null);
(Unfortunately, this requires the android.permission.WRITE_SETTINGS permission.)