Make call using a specified SIM in DualSim Mobile - android

I've been searching a lot for this but got no good result .
I am trying to make a call from inside the app using a specified sim
String x is something like this : "OK>message>*111>1> > >"
public void test_call(String x) {
String simSlotName[] = {
"extra_asus_dial_use_dualsim",
"com.android.phone.extra.slot",
"slot",
"simslot",
"sim_slot",
"subscription",
"Subscription",
"phone",
"com.android.phone.DialingMode",
"simSlot",
"slot_id",
"simId",
"simnum",
"phone_type",
"slotId",
"slotIdx"
};
String encodedHash = Uri.encode("#");
String[] data = x.split(">");
if (!data[4].equals("1") && !data[4].equals("0")) {
Log.d("data :", "E:" + data[4]);
G.is_busy = 0;
return;
}
String ussd = data[3] + encodedHash;
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + ussd));
Log.d("Sim",data[4]);
intent.putExtra("com.android.phone.force.slot", true);
for (String s : simSlotName) {
Log.d("S","s :"+s+"="+data[4]);
intent.putExtra(s, data[4]); // 0 for sim1 , 1 for sim2
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this,
"Call failed, please try again later.",
Toast.LENGTH_SHORT).show();
return;
}
//this.startActivityForResult(intent,1);
startActivity(intent);
G.needscall = "";
}
this is working fine EXCEPT that it always uses sim 0 even if the default SIM in mobile is SIM 1 ! (Android 5.1.1)
this is just use the default SIM in earlier versions
removing this line
intent.putExtra(s, data[4]);
makes the app use the default sim to dial (5.1.1)
..
.
HELP :(

public void call(String simtoiuse, String code) {
String encodedHash = Uri.encode("#");
String ussd = code + encodedHash; // assuming the USSD code to dail is always sent without the # at the end
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + ussd));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)
return; // can put an error message or any thing you want to handle the missing permission
String[] strArr = new String[]{
"extra_asus_dial_use_dualsim",
"com.android.phone.extra.slot",
"slot",
"simslot",
"sim_slot",
"subscription",
"Subscription",
"phone",
"com.android.phone.DialingMode",
"simSlot",
"slot_id",
"simId",
"simnum",
"phone_type",
"slotId",
"slotIdx"
};
intent.putExtra("com.android.phone.force.slot", true);
intent.putExtra("Cdma_Supp", true); // this was missing
for (String putExtra : strArr) intent.putExtra(putExtra, sim);
if (Build.VERSION.SDK_INT >= 23) {
// also this must be added for api 23
Object obj;
List callCapablePhoneAccounts = ((TelecomManager) this.getSystemService(TELECOM_SERVICE)).getCallCapablePhoneAccounts();
String str3 = "android.telecom.extra.PHONE_ACCOUNT_HANDLE";
if (callCapablePhoneAccounts != null && callCapablePhoneAccounts.size() > 0) {
try {
obj = callCapablePhoneAccounts.get(sim);
intent.putExtra(str3, (Parcelable) obj);
} catch (Exception e){} // if the device is 1 sim only this may generate an exception
}
}
startActivity(intent);
// these next lines were missing in my code too.
intent.replaceExtras(new Bundle());
intent.setAction(null);
intent.setData(null);
intent.setFlags(0);
}
so :
call("0","*100"); // will use the first sim to dial *100#
call("1","*100") ; // will use the second sim to dial *100#
AND IT WORKS NOW.

Related

xamrin android visual studio 2019 community version for WhatsApp chat with button click on specific number

I want to add button in my app. when I click on the button this will redirect me to WhatsApp chat having that specific number and chat with that number. I have the following code but its not working :(
public void Button1_Click(object sender, System.EventArgs e)
{
string phoneNumberWithCountryCode = "+9233623xxx";
string message = "Hallo";
StartActivity(new Intent(
Intent.ActionView, Android.Net.Uri.Parse("https://api.whatsapp.com/send?phone=" + phoneNumberWithCountryCode + "&text=" + message)));
}
Button button1 = FindViewById<Button>(Resource.Id.watsapp_op);
button1.Click += (sender, e) => {
Button1_Click(sender, e);
};
I saw the working sample , it sets Package in Intent , so try the following code .
private void OpenWhatsApp(Activity activity, string number, string message)
{
try
{
PackageManager packageManager = activity.PackageManager;
Intent i = new Intent(Intent.ActionView);
String url = "https://api.whatsapp.com/send?phone=" + number + "&text=" + URLEncoder.Encode(message, "UTF-8");
i.SetPackage("com.whatsapp");
i.SetData(Uri.Parse(url));
if (i.ResolveActivity(packageManager) != null)
{
activity.StartActivity(i);
}
else
{
//Error message
}
}
catch (Exception e)
{
// exception
}
}
Update
Does the button on the current content view or not ?
You can see SetContentView(Resource.Layout.activity_main) in OnCreate method .
You have to ensure the button is placed in activity_main.xml , if the button is in another layout , you have to find the view first .
var view = LayoutInflater.Inflate(Resource.Layout.anotherlayout,null);
var button = view.FindViewById<Button>(Resource.Id.button);

Failure to pass # as input in dialer application

I am building an android dialer application, but when my input contains string of form *123# the number which gets dialed is *123.
How can I pass # also in the dialer application?
Below is the code:
public void onDial(View v) {
if (input.getText().length() <3) {
Toast.makeText(this, "Please Enter the Valid Number", Toast.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(Intent.ACTION_CALL);
String hash = input.getText().toString();
if (hash.contains("#")) {
hash.replace("#", "%23");
}
intent.setData(Uri.parse("tel:" + hash));
if (ActivityCompat.checkSelfPermission(this,Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(intent);
}else{
requestCallPermission();
}
}
}
You where close! Encoding will work, but the replace method returns a new string! :-)
if (hash.contains("#")) {
hash = hash.replace("#", "%23"); // Need to set the hash reference to the new string generated by replace()!
}
intent.setData(Uri.parse("tel:" + hash));
Refs:
http://zetcode.com/kotlin/strings/
How to use Uri.parse() with # at the end

android wear phone call

I have 2 questions
Can we make phone calls programatically from Android wear app?
I have created a custom notifcation on Android wear app. Is it possible to open the mobile dialer app, when user tap on the action on custom notification?
Any help woud be appreciated.
Thanks.
yes i thing it is possible. try to use:
yes thing it is possible. try to use:
/*start a direct call, make sure you have call permission declared on your manifest
*<uses-permission android:name="android.permission.CALL_PHONE" />
*/
public static void phoneCall(String n, Activity currentActivity) {
char ench[] = n.toCharArray();
String tel = "";
for (int i = 0; i < ench.length; i++) {
if (ench[i] == '#')
tel = tel + Uri.encode("#");
else
tel = tel + ench[i];
}
String toDial = "tel:" + tel;// msgbox(Intent.ACTION_ALL_APPS);
currentActivity.startActivityForResult(
new Intent(hasPermission(currentActivity,
permission.CALL_PHONE) ? Intent.ACTION_CALL
: Intent.ACTION_DIAL, Uri.parse(toDial)), 1024);
}
//open phonne compositor with phone number as n
public static void phoneDial(String n, Activity currentActivity) {
char ench[] = n.toCharArray();
String tel = "";
for (int i = 0; i < ench.length; i++) {
if (ench[i] == '#')
tel = tel + Uri.encode("#");
else
tel = tel + ench[i];
}
String toDial = "tel:" + tel;// msgbox(Intent.ACTION_ALL_APPS);
Intent intent=new Intent(Intent.ACTION_DIAL,
Uri.parse(toDial));
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
currentActivity.startActivityForResult(intent, 1024);
}
//control if your application have some permission
public static boolean hasPermission(Context context, String permission) {
int res = context.checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}

href sms: || smsto: not working on android with html+cordova(phonegap)

I'm building an android app using cordova and html(js/css etc..)
I'm trying to open the sms application when the user click on a link.
this is the html code:
Send Sms
while the tel: and mailto: schemes works, the sms: or smsto: not working.
INFO:
using the sms: scheme I get this error: No Activity found to handle Intent
E/Cordova(28360): Error sending sms sms:052xxxx808:android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW typ=vnd.android-dir/mms-sms (has extras) }
And using the sms: scheme I get this error: ERR_UNKNOWN_URL_SCHEME
D/Cordova(27207): CordovaWebViewClient.onReceivedError: Error code=-10 Description=net::ERR_UNKNOWN_URL_SCHEME URL=smsto:052xxxx808
I'm using Nexus 5 with Kitkat 4.4.2 to test the app..
P.S: on Galaxy 4 the sms: scheme is working...(not KitKat)
UPDATE:
Clicking on a href link in the browser with the sms: scheme does work, So maybe I missing permission or something like that?
I was able to get this working by creating an SMS Plugin that combined two previous answers here and here. The only catch is that you have to check for the intent to see if build version is KitKat to do differently than in the past.
Here's my plugin code:
public class SmsPlugin extends CordovaPlugin {
public final String ACTION_SEND_SMS = "SendSMS";
#Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
if (action.equals(ACTION_SEND_SMS)) {
try {
String phoneNumber = args.getString(0);
String message = args.getString(1);
String method = args.getString(2);
if (method.equalsIgnoreCase("INTENT")) {
invokeSMSIntent(phoneNumber, message);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.NO_RESULT));
} else {
sendSMS(phoneNumber, message);
}
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
return true;
} catch (JSONException ex) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
}
}
return false;
}
private void invokeSMSIntent(String phoneNumber, String message) {
Intent intent;
Activity activity = this.cordova.getActivity();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // Android 4.4 and up
{
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(activity);
intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + Uri.encode(phoneNumber)));
intent.putExtra("sms_body", message);
// Can be null in case that there is no default, then the user would be able to choose any app that supports this intent.
if (defaultSmsPackageName != null) {
intent.setPackage(defaultSmsPackageName);
}
} else {
intent = new Intent(Intent.ACTION_VIEW);
intent.setType("vnd.android-dir/mms-sms");
intent.putExtra("address", phoneNumber);
intent.putExtra("sms_body", message);
}
activity.startActivity(intent);
}
private void sendSMS(String phoneNumber, String message) {
SmsManager manager = SmsManager.getDefault();
PendingIntent sentIntent = PendingIntent.getActivity(this.cordova.getActivity(), 0, new Intent(), 0);
manager.sendTextMessage(phoneNumber, null, message, sentIntent, null);
}
}
And here is how I invoke it from JavaScript:
var sms = function() {
message: function (phnum, callback) {
if (Ext.os.is.iOS) {
cordova.exec(callback, function (err) {
callback('The following error occurred: ' + err);
}, "Sms", "send", [ {"recipients": [phnum]} ]);
} else if (Ext.os.is.Android) {
cordova.exec(callback, function (err) {
callback('The following error occurred: ' + err);
}, "SmsPlugin", "SendSMS", [phnum, "", "INTENT"] );
} else {
document.location.href = "sms:" + phnum
}
}
};
module.exports = sms;
Be sure to add this to you config.xml:
<feature name="SmsPlugin">
<param name="android-package" value="my.plugin.SmsPlugin" />
</feature>

For looping a list in Android for a list of permissions

I am looping through a list and trying to append a list of applications to a String but so far i am only able to append 1 application only. It works fine if i try to add into an array and display it in a listview.
for (PackageInfo pi : apps) {
String[] permissions = pi.requestedPermissions;
String internetpermissionsText = "";
if (permissions != null) {
for (String permission : permissions) {
Log.d("TAG", permission);
if (permission.equals("android.permission.INTERNET")) {
String appname=pi.applicationInfo.loadLabel(packageManager).toString();
internetpermissionsText += appname + "\n";
Log.e("TAG", "Im in!");
}
internetPermission.setText(internetpermissionsText);
}
The list of applications should display like
Angry Birds
Facebook
Twitter
Internet
but instead it just displays
Angry Birds
Logcat (Too much spam)
http://pastebin.com/PEs8z0Kf
Try it this way, as I stated in the comment above:
String internetpermissionsText = "";
for (PackageInfo pi : apps) {
String[] permissions = pi.requestedPermissions;
if (permissions != null) {
for (String permission : permissions) {
Log.d("TAG", permission);
if (permission.equals("android.permission.INTERNET")) {
String appname=pi.applicationInfo.loadLabel(packageManager).toString();
internetpermissionsText += appname + "\n";
Log.e("TAG", "Im in!");
}
}
}
}
internetPermission.setText(internetpermissionsText);

Categories

Resources