I'm building an app for universal access, my app works in fullscreen with a custom dialer. So if users press home or back during a phone call I need to give them the opportunity to return the call somehow. (In fact I'm thinking about reopening the call in progress automatically if they leave).
I know how to start a call with a number but I don't know how to open the incall screen during a call, I tried doing an Intent.ACTION_CALL without a number but it initiates a second phone call on top of the other:
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"));
startActivity(callIntent);
I think this should be doen with an intent or by simply bringing it to front. But I don't know how to do it. How can I reopen a call in progress programatically?
if it's still relevant or for other people interested in this:
ACTION_CALL_BUTTON do exactly that.
if the call is in progress, it would bring it to the front, and if it's not, it would
bring the call log.
http://developer.android.com/reference/android/content/Intent.html#ACTION_CALL_BUTTON
I found a different solution that seems to work well. I many combinations of Intents and also tried the ACTION_CALL_BUTTON as suggested above but in 4.3 it opens a new dialer and does not take you to the current call in progress. Not sure how this behaves in older sdks. After much research and tries I found this to work well, although it could probably use some optimizing...
telephonyManager = (TelephonyManager) getSystemService( Context.TELEPHONY_SERVICE );
if( telephonyManager.getCallState() == TelephonyManager.CALL_STATE_OFFHOOK )
{
ActivityManager m = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = m.getRunningTasks( 10 );
for( int i = 0; i < tasks.size(); ++i )
{
RunningTaskInfo task = tasks.get( i );
ComponentName component = task.baseActivity;
String packageName = component.getPackageName();
if( packageName.contains( "com.android.phone" ) )
{
m.moveTaskToFront( task.id, 0 );
return;
}
}
}
You will also need to add the permissions to your manifest file:
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.REORDER_TASKS" />
Related
hope someone can point me in the right direction.
I have created an app with a widget. The widget is a simple button that runs a bit of code and then returns a message to the user via a toast. However, I realise in Android 12 toasts are now truncated to two lines and often the message is longer. I'm not keen on snackbars but appreciate my main app can be converted to use them but what should I use to do the same job via a widget?
Snackbars require a layout view to work but I can't figure out how to retirve it from teh widget code. I could use an alertdialogue but although the code compiles they simple don't seem to show up. I'm open to suggestions here...
What is the best way of popping up a message on the homescreen when a widget is pressed? Is there any way of telling the app not to truncate toasts or if not what's the best alternative?
Many thanks
Nat
Finally worked out how to do this myself... Its kinda obvious when you know but isn't that always the way?
Create a new activity. You can make it look like a dialog by using this theme in your manifest - android:theme="#style/Theme.AppCompat.Dialog". Then from your widget simply call showmessage as outlined below...
public void showamessage (String thetitle, String themessage) {
PendingIntent pendingIntent;
Intent intent = new Intent();
intent.setClass(thecontext,MessageActivity.class);
intent.putExtra("TheTitle",thetitle);
intent.putExtra("TheMessage",themessage);
pendingIntent = PendingIntent.getActivity(thecontext, 0, intent, FLAG_IMMUTABLE);
try {
pendingIntent.send(thecontext, 0, intent);
} catch (PendingIntent.CanceledException e) {
System.out.println( "Sending pendingintent failed: " );
}
}
Looking to handle a deep link from the Google Assistant. As I only have an emulator at the moment I am having trouble testing it (from what I have read it requires a real device). That said, I was wondering if I am handling it the correct way. I am unfamiliar with Kotlin and my code was turning into Spaghetti trying to integrate, so I put this together in my existing launcher activity just to try and get it bootstrapped for now. The manifest and actions.xml were set up like the fitness app tutorial.
Am I doing this correctly?
if (mAuth.getCurrentUser() != null) {
data = this.getIntent().getData();
if (data != null && data.isHierarchical()) {
uriData = data.toString();
containsStart = containsIgnoreCase(uriData,"start");
containsRun = containsIgnoreCase(uriData,"run");
if(containsStart && containsRun) {
Intent intent = new Intent(getApplication(), RunActivity.class);
intent.putExtra("runStart", true);
startActivity(intent);
}
}
else {
checkUserAccType();
}
//Else, if there is no current user, start the Authentication activity
}
A few observations and recommendation about your code:
Instead of using containsIgnoreCase uses getPath() and match the path. See example.
Also, for the activity parameter use URL query param instead of containsIgnoreCase. See example
Starting the activity or fragment. I assume startActivity and checkUserAccType will handle that part. See example.
// Else... section should go one line below.
Authentication. It looks fine. And it seems you're using Firebase by the getCurrent method signature. See example
I want to create an intent in android, which go to the timer application and input directly the numbers. I've tried this code, but it only starts the timer application, but not enter the numbers.
Intent intent = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
intent = new Intent(AlarmClock.ACTION_SET_TIMER);
intent.putExtra(AlarmClock.EXTRA_HOUR,hour);
intent.putExtra(AlarmClock.EXTRA_MINUTES,minutes);
intent.putExtra(AlarmClock.EXTRA_SKIP_UI,true);
startActivity(intent);
}
Do you have any idea?
Thanks for every response.
AlarmClock.EXTRA_HOUR and AlarmClock.EXTRA_MINUTES are for AlarmClock.ACTION_SET_ALARM.
AlarmClock.ACTION_SET_TIMER only supports the extras AlarmClock.EXTRA_LENGTH, AlarmClock.EXTRA_MESSAGE and AlarmClock.EXTRA_SKIP_UI
I suppose you don't want to create a timer (a.k.a. a counter that counts the seconds from the start) but instead you'd like to create a countdown. For this, I'd use a CountDownTimer: https://developer.android.com/reference/android/os/CountDownTimer.html
You can even embed it in your own app instead of launching an external clock app.
As can be seen from the documentation here, the flag ACTION_SET_TIMER does not have the flags you are trying to use as optional request parameters. For the ones you are using you should be setting the ACTION_SET_ALARM flag.
I have an intent designed to open the Data Usage Summary view of the system settings app (undocumented; from this Stack Overflow answer):
Intent openIntent = new Intent(Intent.ACTION_MAIN);
openIntent.setComponent(new ComponentName("com.android.settings", "com.android.settings.Settings$DataUsageSummaryActivity"));
openIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(openIntent);
Is it possible to check whether this component exists and whether the intent will execute successfully?
A similar question gave answers that do not work for this intent in the Android (5.0) Emulator (causing the Settings app to crash several times over – see stacktrace). The below code answers return true (i.e. success), even though my above code will crash the Settings app. My activity intent has so far only crashed on the Emulator presumable due to there being no data plan set(?)
private boolean isCallable(Intent intent) {
List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
From this answer..
and
boolean activityExists = intent.resolveActivityInfo(getPackageManager(), 0) != null;
From this one..
Thanks.
The reason why the code snippets you tried are saying that the component is available is because the component is available. The component happens to crash when you try starting it. Whether this is due to an emulator bug, an Android bug, or the fact that you happen to be starting an activity that isn't documented to be started by third-party apps, I can't say.
Is it possible to check whether this component exists
Use the code snippets from your question. In this case, the component exists.
Is it possible to check whether... the intent will execute successfully?
Not in general. Third-party applications are written by third parties. Not only might they have bugs/limitations, but you have no means of determining whether they do from your app.
I'm writing an application which should be able to add widgets (just text boxes) to the home screen of the user's phone when the user instructs my app to do so. How can I do such a thing?
I know that I can add an app widget but how about adding more?
It is not possible from a app to place a widget in the home screen. Only the home screen can add app widgets to the home screen.
similar links link1, link2, link3
But you can offer user to pick widget from widgetpicker.
Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetID);
startActivityForResult(pickIntent, KEY_CODE);
This was answered a long time ago, but in case anyone stumbles upon this question I thought I should provide an up-to-date answer.
As of Android O (API 26), you can now pin widgets to the user's launcher from your app!
Simply create the widget in your app's AndroidManifest file and use AppWidgetManager to request that the widget be pinned to the launcher. Note that it is up to the launcher to support this feature, so you must call AppWidgetManager's isRequestPinAppWidgetSupported() method before requesting to pin it.
Here's some documentation from Google that goes into more detail: https://developer.android.com/preview/features/pinning-shortcuts-widgets.html#widgets
Hope this helps!
Edit: It looks like the documentation pages have changed since I posted this answer. Here is a more helpful link that gives a code example of how to pin a widget to a launcher: https://developer.android.com/guide/topics/appwidgets/#Pinning
Looks like Dalvik Droid's links are updated again, the newest link is at requestPinAppWidget
Example:
in MainActivity.java:
private void requestToPinWidget(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AppWidgetManager appWidgetManager =
getSystemService(AppWidgetManager.class);
ComponentName myProvider =
new ComponentName(this, AppWidget.class);
assert appWidgetManager != null;
if (appWidgetManager.isRequestPinAppWidgetSupported()) {
Intent pinnedWidgetCallbackIntent = new Intent(this, MainActivity.class);
PendingIntent successCallback = PendingIntent.getBroadcast(this, 0,
pinnedWidgetCallbackIntent, PendingIntent.FLAG_UPDATE_CURRENT);
appWidgetManager.requestPinAppWidget(myProvider, null, successCallback);
}
}
}
Any anywhere in you code you have call requestToPinWidget() to prompt to see if the user wants to add it to the desktop.
Only thing is that this will not add to user's home screen, it will be appended to the page where you see all the apps:
AFAIK default launcher app does not support this. The reason is that user should place everything on the home screen himself. Allowing to place widgets from an application would open doors for apps to "spam" user's home with their "useful" widgets.