Basically I'd like to start the current phone call ui from a button in one of my activities.
So far, I've been able to start a new call using
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:000000"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
or to move to the phonebook using:
Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I've also tried to use that code, but it seems not to be working on HTC devices and some Huawei (ri is null).
Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
intentToResolve.setPackage("com.android.phone");
ResolveInfo ri = getPackageManager().resolveActivity(intentToResolve, 0);
if (ri != null)
{
Intent intent = new Intent(intentToResolve);
intent.setClassName(ri.activityInfo.applicationInfo.packageName, ri.activityInfo.name);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
What I'd like to get is move directly to the call ui. The behaviour is the same as in this procedure:
Be in a phone call
Go back to the Home screen
Expand the notification bar
Click on the call row
I don't know how to achievve and how to make it works on all devices.
Thanks for your precious help.
you have to do as suggested on this answer: android.permission.CALL_PHONE for tablets
add the call phone permission:
<uses-permission android:name="android.permission.CALL_PHONE"/>
and then call with Intent.ACTION_CALL.
Intent intentcall = new Intent(
Intent.ACTION_CALL,
Uri.parse("tel:" + phonenumber));
startActivity(intentcall);
edit:
there's a way you could do, but it won't work in Lollipop and forward, as it's been deprecated due to security reasons.
You could use the ActivityManager to get the running tasks and then analyse the content of each RunningTaskInfo to determine which one is the "phone" and then use the task ID to call moveTaskToFront. It possibly work, but as I said, it will not work with Lollipop and future versions.
Related
I'm trying to bring my app from background to foreground. In onHandleIntent() of my custom IntentService class, I have:
Intent intent = new Intent();
intent.setClass(getApplicationContext(), MainActivity.class); // Also tried with "this" instead of getApplicationContext()
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
Now this code works at first sight but I found a scenario where it doesn't work. If you have the app opened and you put it to background via home button and execute startActivity() within ~5 second, there will be a delay before your app will come to foreground. This is a known implementation and you can find the topic discussed on stackoverflow. In this scenario, the app succeeded in coming from background to foreground.
If you repeat the same experiment above, but instead of waiting for the app to come to foreground, go browse (scroll, swipe, etc) around your phone (I was browsing around the google playstore). The result is that startActivity() will get called but the app will not come to the foreground.
I'm not asking for a solution but more of an explanation on why this is happening. Is this intended behavior?
Use the context of your class.
For instance :
Intent intent= new Intent(context, other.class)
Instead of getapplicationContext()
Use the code :
private void startMenuActivity() {
Intent i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
}
the below code works for me,
val login = Intent(applicationContext, SignInActivity::class.java)
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
login.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
applicationContext.startActivity(login)
I have a situation in my current app where on tapping on a dialog fragment I have to restart the current app in background (or take to home activity) and open calendar app at the same time. I tried firing 2 intents at the same time but it didn't work. I also tried to use AsyncTask's executeOnExecutor but the order is not always guaranteed. Can someone please explain me how to handle situation like these?
you have write following code to launch activity first and calendar intent after that:
public void openCalendar(Context mCtx){
Intent activityIntent = new Intent(mCtx, HomeActivity.class);
startActivity(activityIntent );
Intent i = new Intent();
//Froyo or greater (mind you I just tested this on CM7 and the less than froyo one worked so it depends on the phone...)
cn = new ComponentName("com.google.android.calendar", "com.android.calendar.LaunchActivity");
//less than Froyo
cn = new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity");
i.setComponent(cn);
startActivity(i);
}
I have the folowing code and im starting it from a dialog fragment button:
uri = "tel:"+ServerDialogCallUs.this.contents.getString("phone_number");
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
Instead of making a normal phone call this code starts a skype call. How can I give the user the option to chose between the normal call and a skype call.
Thanks
actually, i was missing the permission:
<uses-permission android:name="android.permission.CALL_PHONE" />
after adding this permission to the manifest I can chose between a normal and a skype call.
Thanks
Try to call setPackage() method, works fine:
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setPackage("com.android.phone");
intent.setData(Uri.parse("tel:" + number));
startActitivy(intent);
Had the same issue and tried some answers:
Intent.ACTION_CALL will call, which needs probably the right permission, BUT using Intent.ACTION_DIAL and use an Intent Chooser, I got the right behaviour.
Regarding USSD like "*123*03#" which pops up also as question in this context, where android says, it cannot handle the intent
2.1. Uri.encode("#") is not the solution, it just adds a "23" to the phone numer
2.2. Skype handles the number wrong, it adds the country-code like +001. So Skype does not handle USSDs correctly. But it seems, I have no chance, to exclude this.
So, I did:
final Intent intent = new Intent();
intent.setAction(Intent.ACTION_DIAL);
intent.setData(Uri.fromParts("tel", "123456", null));
startActivity(Intent.createChooser(intent, ""), REQUEST_CODE));
From my point of view
intent.setPackage("com.android.phone");
is device dependent or at least does not leave "phone" included on my device.
That's because
There is only skype installed as a phone call handler or
You associated skype as default handler for phone calls.
In the latter case, go to settings, apps, skype, and remove that association on the bottom of the app details screen.
Your phone probably has Skype as the default app to handle the Intent.ACTION_CALL
Try the following code:
uri = "tel:"+ServerDialogCallUs.this.contents.getString("phone_number");
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(Intent.createChooser(intent, "Call Using..."));
hi Its been 2 days looking for this simple problem. I want to launch Android own launcher from my application EVEN if its not set as default.
final PackageManager packageManager=getPackageManager();
Intent intent = packageManager.getLaunchIntentForPackage("com.android.launcher");
this return null for Android own launcher but if I try custom launcher is give me successfully
Found the solution, after investigating source code of getLaunchIntentForPackage. As per documentation,
The current implementation will look first for a main activity in the
category CATEGORY_INFO, next for a main activity in the category
CATEGORY_LAUNCHER, or return null if neither are found.
So, the function don't look for CATEGORY_HOME, i rewrote it in following way, it worked perfectly.
Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
intentToResolve.addCategory(Intent.CATEGORY_HOME);
intentToResolve.setPackage("com.android.launcher");
ResolveInfo ri = getPackageManager().resolveActivity(intentToResolve, 0);
if (ri != null)
{
Intent intent = new Intent(intentToResolve);
intent.setClassName(ri.activityInfo.applicationInfo.packageName, ri.activityInfo.name);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
Are you sure the default Google Android Launcher being installed on your device? If not, then it's truly NULL.
The following code is launching gps setting screen in samsung device but in htc device it is launching security screen of setting.How can i write code so that it will launch gps screen independent of the devices.Please help is there any alternative solution
final ComponentName toLaunch = new ComponentName("com.android.settings","com.android.settings.SecuritySettings");
final Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(toLaunch);
startActivity(intent);
Get rid of the setComponent() and addCategory() calls and see if that helps. You should not be using those in any case, particularly the ComponentName that hard-wires in invalid package and class names.
I think this code is helpful for you
Intent intent1 = new Intent();
intent1.setClassName("com.android.settings",
"com.android.settings.SecuritySettings");
context.startActivity(intent1);