I know it was very simple to do it but I come across a very strange issue. I have to call Police in danger Situation by just tapping a button. So I have used following code to call.
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:100"));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(callIntent);
Added CALL_PHONE permission in AndroidManifest.xml. The Issue is that it is opening the 100 on Dial Pad but not making call directly. I want that to happen immediately when user clicks on the button.
When I tried to to put +91 before 100 it is calling the number automatically but why plus is required for such numbers. So Someone help me how to solve this issue
From the documentation of ACTION_CALL:
Note: there will be restrictions on which applications can initiate a call; most applications should use the ACTION_DIAL.
Note: this Intent cannot be used to call emergency numbers. Applications can dial emergency numbers using ACTION_DIAL, however.
So it seems this behavior is on purpose.
There could be a problem that the android system doesnt recognize 100 as a valid phone number, instead if you put the country code before it then it works fine. TO solve such issue take a look at this library libnhonenumber. You could use it something like this
public static ArrayList<String> extractPhoneNumber(String content) {
ArrayList<String> numbers = new ArrayList<String>(0);
PhoneNumberUtil instance = PhoneNumberUtil.getInstance();
//Change IT with your contry code
Iterable<PhoneNumberMatch> matches = instance.findNumbers(content, "IT");
Iterator<PhoneNumberMatch> iterator = matches.iterator();
while (iterator.hasNext()) {
numbers.add(instance.format(iterator.next().number(), PhoneNumberFormat.INTERNATIONAL));
}
return numbers;
}
private void phoneCall()
{
String phoneCallUri = "tel:91";
Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(phoneCallUri));
startActivity(phoneCallIntent);
}
Best way to directly call without user intervention..
String uri = "tel:" + num.trim();
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);
There are two intents to call/start calling: ACTION_CALL and ACTION_DIAL.
ACTION_DIAL will only open the dialer with the number filled in, but allows the user to actually call or reject the call. ACTION_CALL will immediately call the number and requires an extra permission.
So make sure you have the permission
A Long time passed. But may help someone else.
If you want to call directly, you should use requestPermissions method.
1. Add this line to your manifest file:
<uses-permission android:name="android.permission.CALL_PHONE" />
2. Define a class variable in the activity class:
private static Intent phoneCallIntent; //If use don't need a member variable is good to use a static variable for memory performance.
3. Add these lines to the onCreate method of the activity:
final String permissionToCall = Manifest.permission.CALL_PHONE;
//Assume that you have a phone icon.
(findViewById(R.id.menuBarPhone)).setOnClickListener(new OnClickListener(){
public void onClick(View view) {
phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(getString(R.string.callNumber))); //Uri.parse("tel:your number")
if (ActivityCompat.checkSelfPermission(MainFrame.this, permissionToCall) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainFrame.this, new String[]{permissionToCall}, 1);
return;
}
startActivity(phoneCallIntent);
}
});
4. And for making a call immediately after clicking on Allow button, override onRequestPermissionsResult method:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults){
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 1){
final int permissionsLength = permissions.length;
for (int i = 0; i < permissionsLength; i++) {
if(grantResults[i] == PackageManager.PERMISSION_GRANTED){
startActivity(phoneCallIntent);
}
}
}
When a user give the permission, next time there will be no dialogue box and call will be make directly.
Related
until Android API28 when requesting Media Projection Services android allowed the user to grant access just once by selecting "Don't show again" check-box. So that everytime app wants to use media projection services the access was already granted.
In the android AP29 the dialog changed and this option does not exist anymore.
What can be done in order to the user just grant the access once to the app to use media projection? Thank you.
Well the problem was that I was calling
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
every time, which is not necessary (createScreenCaptureIntent() leads to the dialog window which requests user interaction) My solution makes the dialog appear only once (if application was closed it will ask for permission one time again). All I had to do was making addiotional private static variables of type Intent and int.
private static Intent staticIntentData;
private static int staticResultCode;
On Activity result I assign those variables with the passed result code and intent:
if(staticResultCode == 0 && staticIntentData == null) {
sMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);
staticIntentData = data;
staticResultCode = resultCode;
} else {
sMediaProjection = mProjectionManager.getMediaProjection(staticResultCode, staticIntentData)};
}
Every time I call my startprojection method, I will check if they are null:
if(staticIntentData == null)
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
else
captureScreen();
If null it will request permission, if not it will start the projection with the static intent data and static int resultcode, so it is not needed to ask for that permission again, just reuse what you get in activity result.
sMediaProjection = mProjectionManager.getMediaProjection(staticResultCode, staticIntentData);
Simple as that! Now it will only showing one single time each time you use the app. I guess thats what Google wants, because there no keep decision checkbox in that dialog like in previous android versions
I am trying to determine which app the user selects when clicking on a directions button in my app. I want the "Always" and "Just Once" options of the default chooser but I need to know which app the user is sent to, which is why I find myself creating a custom chooser. I've done research on SO and have come across the following posts:
Custom chooser to allow me to know what app the user selects: https://stackoverflow.com/a/23494967/3957979
ActionProvider (doesn't work in my case since this is not a MenuItem):
https://stackoverflow.com/a/23495696/3957979
Anyone have any idea on how to do this? Please let me know if there is a way to use the default chooser and still determine the app the user selects. Otherwise, please let me know if there is a way to integrate the "Always" and "Just Once" options into a custom chooser.
Below is the code I am implementing:
public View onCreateView(...) {
...
shiftActionButtonDirections.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = directions(mLatitude, mLongitude, location.getFullAddress());
// Check if any apps can handle geo intent
if (intent.resolveActivity(getAppContext().getPackageManager()) != null) {
showChooser(intent, "Get Directions with...");
} else {
Toast.makeText(getAppContext(), R.string.error_maps, Toast.LENGTH_LONG).show();
}
}
});
...
}
public Intent directions(String latitude, String longitude, String address) {
String uri = String.format("geo:%s,%s?q=%s", latitude, longitude, Uri.encode(address));
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return i;
}
private void showChooser(#NonNull final Intent intent, String title) {
final List<ResolveInfo> activities = getAppContext().getPackageManager().queryIntentActivities(intent, 0);
List<String> appNames = new ArrayList<String>();
for (ResolveInfo info : activities) {
appNames.add(info.loadLabel(getAppContext().getPackageManager()).toString());
}
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(TextUtils.isEmpty(title) ? "With..." : title);
builder.setItems(appNames.toArray(new CharSequence[appNames.size()]), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Map<String, Object> properties = new HashMap<>();
ResolveInfo info = activities.get(item);
if (info.activityInfo.packageName.contains("google")) {
// Google Maps was chosen
} else {
// Another app was chosen
}
// start the selected activity
intent.setPackage(info.activityInfo.packageName);
startActivity(intent);
}
});
AlertDialog alert = builder.create();
alert.show();
}
Please let me know if there is a way to use the default chooser and still determine the app the user selects.
Set your minSdkVersion to 22, then use the three-parameter flavor of createChooser(). Note that this does not allow you to change anything about the user's choice (e.g., substitute a different Intent); it merely gives you a chance to find out the choice.
Or, if you do not want to set your minSdkVersion to 22, use the three-parameter flavor of createChooser() on API Level 22+ devices, and do something else on older ones (custom chooser, live without the choice information, etc.).
Otherwise, please let me know if there is a way to integrate the "Always" and "Just Once" options into a custom chooser.
No, that is not possible.
In my android app I want to change the input method. So I start a new Activity which shows the language settings in the device. Then user can change it. However then I want to know that if the user has changed it. So I wrote a function for that also. My code so far is...
Intent enableIME = new Intent(android.provider.Settings.ACTION_INPUT_METHOD_SETTINGS);
startActivityForResult(enableIME,0);
if(isInputMethodEnabled()){
activateshadow.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_pressed));
activateshadow.setText("Deactivate Shadow");
prefs.edit().putBoolean("Activate", false).commit();
}else{
Toast.makeText(MainActivity.this,"You haven't change the input method to simpleIME.In order to activate you must change it.",Toast.LENGTH_SHORT).show();
}
my is inputMethodEnabled function is....
public boolean isInputMethodEnabled() {
boolean isIME ;
String id = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
String [] name = id.split("/.");
// Toast.makeText(MainActivity.this,"s:"+name[1]+":s",Toast.LENGTH_SHORT).show();
if(name[1].contains("SimpleIME") ){
isIME = true ;
}else{
isIME = false;
}
// Toast.makeText(MainActivity.this,"Returning..."+isIME,Toast.LENGTH_SHORT).show();
return isIME;
}
if(isInputMethodEnabled()) always fails because when the new intent(settings) opens and it take some time to change the input method to simpleIME . How to fix this problem?
You catch when a launched Activity returns in onActivityResult. The requestCode you supplied to startActivityForResult will be a parameter, as will the Activity's result. The Activity may also set other data which you didn't ask about.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 555) {//555 is the intent ID you gave in startActivityForResult(enableIME,555);
if (resultCode == /*Result1*/)
//Do something
else {
//Do something else
}
}
}
You need a unique id when calling startActivityForResult(enableIME,0);
startActivityForResult(enableIME, 555);
Better still replace 555 with a named variable.
if u look at android life cycle, when activity is finished whole android call onDestroy() method.
so u can call and override this method.
just need write:
#override
protected void onDestroy(){
// code
super.onDestroy();
}
u can manage and override all of life cycle's parts in android
e.g: onResume to get current activity
i hope this help u
Because the Android SDK 23 gives users the possibility to deny apps access to certain functionalities I wanted to update one of my apps to request permissions as it is described in here: https://developer.android.com/preview/features/runtime-permissions.html.
In one of the activities I embed a SupportMapFragment. To make it work you need to have the WRITE_EXTERNAL_STORAGE permission, so I request it when I start the activity which results in a creation of a permission request dialog.
Now the problem is that when the dialog is still open and I rotate the device the activity will be restarted and open a new permission request dialog while the old one is still there. The result is two of those dialogs on top of each other and only one of it being useful.
Is there a way to get rid of the dialog that was started first?
As CommonsWare said in his comment the best solution is to put a boolean into the savedInstanceState-Bundle to know if the dialog is still open.
Example:
// true if dialog already open
private boolean alreadyAskedForStoragePermission = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null) {
alreadyAskedForStoragePermission = savedInstanceState.getBoolean(STORAGE_PERMISSION_DIALOG_OPEN_KEY, false);
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(KEY, alreadyAskedForStoragePermission);
}
private void checkStoragePermission(){
if(alreadyAskedForStoragePermission){
// don't check again because the dialog is still open
return;
}
if(ActivityCompat.checkSelfPermission(this, STORAGE_PERMISSIONS[0]) != PackageManager.PERMISSION_GRANTED){
// the dialog will be opened so we have to keep that in memory
alreadyAskedForStoragePermission = true;
ActivityCompat.requestPermissions(this, STORAGE_PERMISSIONS, STORAGE_PERMISSION_REQUEST_CODE);
} else {
onStoragePermissionGranted();
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode){
case STORAGE_PERMISSION_REQUEST_CODE:
// the request returned a result so the dialog is closed
alreadyAskedForStoragePermission = false;
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
onStoragePermissionGranted();
}
break;
}
}
As #user1991776 mentioned there is actually an undocumented extra that contains whether or not there is a permission dialog open at the moment, in Activity:
private static final String HAS_CURENT_PERMISSIONS_REQUEST_KEY =
"android:hasCurrentPermissionsRequest";
However there is a better way. When you request a permission dialog the second time (due to a rotation), Activity automatically cancels the old dialog by calling your onRequestPermissionResult() with empty arrays:
public final void requestPermissions(#NonNull String[] permissions, int requestCode) {
if (mHasCurrentPermissionsRequest) {
Log.w(TAG, "Can reqeust only one set of permissions at a time");
// Dispatch the callback with empty arrays which means a cancellation.
onRequestPermissionsResult(requestCode, new String[0], new int[0]);
return;
}
Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null);
mHasCurrentPermissionsRequest = true;
}
Or course this behaviour isn't documented because this is Android, and who wants to document complex behaviour?
Anyway you can just always request permissions in onCreate() and then ignore calls to onRequestPermissionsResult() with zero-length permissions arrays.
I guess as this is a system dialog you cannot control it. You could instead prevent that your activity gets reloaded if you turn your device.
when I use this code, first comes the dial pad screen with this number.
Intent dialintnt = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:911"));
startActivityForResult(dialintnt, CALLING);
I don't want that screen. I want that when I click button directly calling that number.
So how can I call a number onclick?
It's not possible. This is for user protection.
It's been a long time. But may help someone else.
If you want to call directly, you should use requestPermissions method.
1. Add this line to your manifest file:
<uses-permission android:name="android.permission.CALL_PHONE" />
2. Define a class variable in the activity class:
private static Intent phoneCallIntent; //If use don't need a member variable is good to use a static variable for memory performance.
3. Add these lines to the onCreate method of the activity:
final String permissionToCall = Manifest.permission.CALL_PHONE;
//Assume that you have a phone icon.
(findViewById(R.id.menuBarPhone)).setOnClickListener(new OnClickListener(){
public void onClick(View view) {
phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(getString(R.string.callNumber))); //Uri.parse("tel:your number")
if (ActivityCompat.checkSelfPermission(MainFrame.this, permissionToCall) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainFrame.this, new String[]{permissionToCall}, 1);
return;
}
startActivity(phoneCallIntent);
}
});
4. And for making a call immediately after clicking on Allow button, override onRequestPermissionsResult method:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults){
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 1){
final int permissionsLength = permissions.length;
for (int i = 0; i < permissionsLength; i++) {
if(grantResults[i] == PackageManager.PERMISSION_GRANTED){
startActivity(phoneCallIntent);
}
}
}
When a user give the permission, next time there will be no dialogue box and call will be make directly.
See the answer
You will need add CALL_PHONE and CALL_PRIVILEGED permissions to manifest file.
Then the number can be called using:
Uri callUri = Uri.parse("tel://911");
Intent callIntent = new Intent(Intent.ACTION_CALL,callUri);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_USER_ACTION);
startActivity(callIntent);
Just change ACTION_DIAL to ACTION_CALL. Like this:
Intent dialintnt = new Intent(Intent.ACTION_CALL,Uri.parse("tel:911"));
startActivityForResult(dialintnt, CALLING);
Try this:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" +phone_number));
startActivity(callIntent);
Number has to be proper formatted as if you try with country code it will take you default dailer. i tested it on my app when i am using number as 198 call is happening directly and when i am using 9999999999 this will invoke the dailer so that user can correct the number by adding country code.
`val intent = Intent(Intent.ACTION_CALL, Uri.parse("tel:+91987654310" ))
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent)`