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);
}
Related
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.
I am trying to make an app that would send a MMS without using the native Android messaging app. I followed the example here. My log statements seem to be correctly printing, but I can't figure out why the MMS is not being sent.
Also on a different note, I am a bit confused about where in the example the attachment (like an image) is being selected to send as MMS. I tried to import the demo into Android Studio but I ran into issues.
My function for sending MMS is below:
public void sendMMS() {
Log.d(TAG, "sendMMS()");
Random random = new Random();
final String fileName = "send." + String.valueOf(Math.abs(random.nextLong())) + ".dat";
final File mSendFile = new File(mContext.getCacheDir(), fileName);
// Making RPC call in non-UI thread
AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
#Override
public void run() {
final byte[] pdu = buildPdu();
Uri writerUri = (new Uri.Builder())
.authority("com.example.appname")
.path(fileName)
.scheme(ContentResolver.SCHEME_CONTENT)
.build();
Log.d(TAG, "sendMMS(): Uri: " + writerUri.toString());
FileOutputStream writer = null;
Uri contentUri = null;
try {
writer = new FileOutputStream(mSendFile);
writer.write(pdu);
contentUri = writerUri;
Log.d(TAG, "sendMMS(): just wrote file");
} catch (final IOException e) {
Log.d(TAG, "sendMMS(): FAILED: couldn't write file");
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
}
}
}
if (contentUri != null) {
SmsManager.getDefault().sendMultimediaMessage(mContext, contentUri, null, null, null);
Log.d(TAG, "sendMMS(): just sent");
} else {
Log.d(TAG, "sendMMS(): FAILED: couldn't write file so didn't send");
}
}
});
}
Helper functions
private byte[] buildPdu() {
final SendReq req = new SendReq();
// from
final String lineNumber = getSimNumber();
if (!TextUtils.isEmpty(lineNumber)) {
req.setFrom(new EncodedStringValue(lineNumber));
}
// to
String[] destsArray = mDestList.toArray(new String[mDestList.size()]);
EncodedStringValue[] encodedNumbers = EncodedStringValue.encodeStrings(destsArray);
if (encodedNumbers != null) {
req.setTo(encodedNumbers);
}
// date
req.setDate(System.currentTimeMillis() / 1000);
// body
PduBody body = new PduBody();
// message text
final int size = addMessagePart(body, true/* add text smil */);
req.setBody(body);
// message size
req.setMessageSize(size);
// message class
req.setMessageClass(PduHeaders.MESSAGE_CLASS_PERSONAL_STR.getBytes());
// expiry
req.setExpiry(DEFAULT_EXPIRY_TIME);
try {
// priority
req.setPriority(DEFAULT_PRIORITY);
// delivery report
req.setDeliveryReport(PduHeaders.VALUE_NO);
// read report
req.setReadReport(PduHeaders.VALUE_NO);
} catch (InvalidHeaderValueException e) {}
return new PduComposer(mContext, req).make();
}
private String getSimNumber() {
TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
return telephonyManager.getLine1Number();
}
private int addMessagePart(PduBody pb, boolean addTextSmil) {
PduPart part = new PduPart();
part.setCharset(CharacterSets.UTF_8);
part.setContentType(ContentType.TEXT_PLAIN.getBytes());
part.setContentLocation(TEXT_PART_FILENAME.getBytes());
int index = TEXT_PART_FILENAME.lastIndexOf(".");
String contentId = (index == -1) ? TEXT_PART_FILENAME : TEXT_PART_FILENAME.substring(0, index);
part.setContentId(contentId.getBytes());
part.setData(mMessage.getBytes());
pb.addPart(part);
if (addTextSmil) {
String smil = String.format(sSmilText, TEXT_PART_FILENAME);
addSmilPart(pb, smil);
}
return part.getData().length;
}
private void addSmilPart(PduBody pb, String smil) {
PduPart smilPart = new PduPart();
smilPart.setContentId("smil".getBytes());
smilPart.setContentType(ContentType.APP_SMIL.getBytes());
smilPart.setContentLocation("smil.xml".getBytes());
smilPart.setData(smil.getBytes());
pb.addPart(0, smilPart);
}
Relevant parts of my manifest
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Relevant instance variables
private final long DEFAULT_EXPIRY_TIME = 7 * 24 * 60 * 60;
private final String TEXT_PART_FILENAME = "text_0.txt";
private final int DEFAULT_PRIORITY = PduHeaders.PRIORITY_NORMAL;
private String mMessage;
private ArrayList<String> mDestList;
private Context mContext;
private static final String sSmilText =
"<smil>" +
"<head>" +
"<layout>" +
"<root-layout/>" +
"<region height=\"100%%\" id=\"Text\" left=\"0%%\" top=\"0%%\" width=\"100%%\"/>" +
"</layout>" +
"</head>" +
"<body>" +
"<par dur=\"8000ms\">" +
"<text src=\"%s\" region=\"Text\"/>" +
"</par>" +
"</body>" +
"</smil>";
I already do input checks, so by the time sendMMS() is called, my message and destList are not null.
The flow should be as such:
Create the Mms send-request - new SendReq() and config its date, body, to, etc.
Create the Mms body - new PduBody().
Create Parts via new PduPart() for each attachment, and add to the body: body.addPart(pdu)
Add the body to the request - req.setBody(body)
Convert the send-request to a byte[] ready to be sent by calling new PduComposer(context, mySendReq).make() - note that you'll need to copy lots of code from Android's source code to get the PduComposer class.
Now's the interesting part - you save the byte[] to a local file accessible to your app only, and add ContentProvider class that allows other apps to request access to your file, this is MmsFileProvider class in the sample app, don't forget to declare your provider in your manifest file.
Now, when you call the SmsManager.sendMultimediaMessage api, your file provider will wake up to serve the file containing the pdu bytes to the system SmsManager that will read it and send it on the wire.
Having that said, this API is only working for me on some devices (e.g. Nexuses), but not on some others (e.g. HTC One).
See my SO question here:
SmsManager MMS APIs on HTC/LG
I am using Android market API
MarketSession session = new MarketSession();
session.setAuthSubToken(token);
session.getContext().setAndroidId( Settings.Secure.getString(getContentResolver(),
"android_id"));
String query = "map";
AppsRequest appsRequest = AppsRequest.newBuilder().setQuery(query)
.setStartIndex(0).setStartIndex(0).setEntriesCount(10)
.setWithExtendedInfo(true).build();
session.append(appsRequest, new Callback<AppsResponse>() {
#Override
public void onResult(ResponseContext context,
AppsResponse response) {
Debug.e("", "Total App: " + response.getAppList().size());
for (int i = 0; i < response.getAppCount(); i++) {
Debug.e("", "" + response.getApp(i).getTitle());
}
}
});
session.flush();
My logcat
04-03 15:35:44.748: W/System.err(28330): Caused by: java.lang.RuntimeException: Response code = 400, msg = Bad Request
It's issue of setAndroidId because if I am using dead000beef as Android is then it works and above Android id (my galaxy s3 android id) does not work.
Is it android id actually?
You are using a wrong android id , this way worked for me
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String udid = telephonyManager.getDeviceId();
session = new MarketSession();
session.getContext().setAndroidId(udid);
session.setAuthSubToken(token);
Don't forget to add this permission
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
I am sending dtmf tone via this code.
String number="tel:+962791212121,2,3,3";
Intent c1= new Intent(android.content.Intent.ACTION_CALL, Uri.parse(number));
startActivity(c1);
It send perfect dtmf as this. 2+(2 sec delay)+3+(2 sec delay)+3.
but I want to remove that 2 sec delay or I want to control that delay.
How can I control (2 sec delay)?
Or any other method to send dtmf tone during call?
To remove the delay remove the comma.
Try this method().
private void call(int profileid) {//call procedure logic
ProfileDo profile = adapter.getProfile(profileid);
if (profile.getStepCount() == 0) {
Toast.makeText(getApplicationContext(), "Please edit the profile and add atleast one value to make a call", 10000).show();
return;}
String call = "tel:";
for (StepDO step : profile.getSteps()) {
String value = URLEncoder.encode(step.getValue());
int delay = step.getDelay();
String pausesStr = "";
for (int i = 0; i < delay / 2; i++) {
pausesStr += PhoneNumberUtils.PAUSE;
}
call += value + pausesStr;
}
startActivity(new Intent("android.intent.action.CALL", Uri.parse(call)));
}
Hope this helps you .
Try this:
Intent signalIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:12334566778"+","+","+"1"));
startActivity(signalIntent);
Is it possible to send DTMF tones in active call in android ? I tried it with proxyphone.sendDtmf() but it was useless.
How can i achieve it ?
In VOIP only it is possible,Android applications have no access to the in-call audio stream. You can fake a it a bit in speakerphone mode.
Try this method() .It is getting the number and delay from user.
private void call(int profileid) {//call procedure logic
ProfileDo profile = adapter.getProfile(profileid);
if (profile.getStepCount() == 0) {
Toast.makeText(getApplicationContext(), "Please edit the profile and add atleast one value to make a call", 10000).show();
return;}
String call = "tel:";
for (StepDO step : profile.getSteps()) {
String value = URLEncoder.encode(step.getValue());
int delay = step.getDelay();
String pausesStr = "";
for (int i = 0; i < delay / 2; i++) {
pausesStr += PhoneNumberUtils.PAUSE;
}
call += value + pausesStr;
}
startActivity(new Intent("android.intent.action.CALL", Uri.parse(call)));
}