Android wear: calling a "default fitness app" from java - java

I'm trying to have a function in my App for Android Wear to call the "default Fitness App"...
I already see in a WatchFace that it is possible tapping on an element to call the App to display a statistic of heart rate or steps count or similar.
If I change the App (for example from Google Fit to Moto Body), and I tap again on the element the new App will be started.
So it must be possible to call an action (Intent?) with some value and Android call the properly App.
I searched this "some value" but I didn't found anything. I'm sure, it exists...
Can someone help me?
Thanks a lot
Luca

After many trials and debugging other Apps, I found a solution...
I post it here in the case someone other needs it:
Intent intent = new Intent(Fitness.ACTION_VIEW, null);
intent.setType("vnd.google.fitness.data_type/com.google.heart_rate.bpm");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try
{
startActivity(intent);
}
catch(ActivityNotFoundException e)
{
Log.v(PROGRAM, "Unable to start Fitness App\n" + e.toString());
}
Regards
Luca

Related

Alternative for toast when using widgets in Android 12 as toasts are now truncated

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: " );
}
}

Offline SpeechRecognizer on Android is not working

I am working with SpeechRecognizer. The problem was if there is no active internet connection SpeechRecognizer will throw error SpeechRecognizer.ERROR_NETWORK or SpeechRecognizer.ERROR_SERVER.
This is my RecognizerIntent
final Intent recognizerIntent;
recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true);
And I put recognizerIntent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true);
But no luck. again it will throw the same error.
And I got these 2 errors frequently.
SpeechRecognizer.ERROR_RECOGNIZER_BUSY
SpeechRecognizer.ERROR_NO_MATCH
Please help me.
First of all, you need to make sure if you have offline package of language you put to EXTRA_LANGUAGE_PREFERENCE installed on the device.
To enable Offline Speech input in supported devices, follow below steps:
Go to Settings
Click on “Language and input”
On-screen keyboard
Select Google voice typing
Select Offline speech recognition
Install desired language
If it still does not work offline, try to change your EXTRA_LANGUAGE_PREFERENCE value to something more specific, "en-US" for example (That did the trick for me)
And also, if you want to tell SpeechRecognizer wich language it should recognize, I guess you should use EXTRA_LANGUAGE parameter instead of EXTRA_LANGUAGE_PREFERENCE
Hope it will help

How does Facebook add badge numbers on app icon in Android?

I know there are several Qs here that ask if its possible to add badges to an android app and they all end up with a NO answer...
But somehow the latest Facebook beta version for Android seems to do something which at least look like a badge even if it is not technically exactly that.
In that post one of the commenters says that it is somehow related to TouchWiz.
And also here they mention it as a feature of the "S3 TouchWiz Jelly Bean Addon".
I still would appreciate information on how does this can be done and if there is some API for this that I can use in my own app (when running in an appropriate environment - i.e. the same device where FB demonstrates this behavior) ?
Hi you can use this lib simply.
Support : Sony,Samsung,LG,HTC,Xiaomi,ASUS,ADW,APEX,NOVA,Huawei,ZUK,OPPO
ShortcutBadger
Add :
int badgeCount = 1;
ShortcutBadger.applyCount(context, badgeCount);
Remove :
ShortcutBadger.applyCount(context, 0);
I have figured out how this is done for Sony devices.
I've blogged about it here. I've also posted a seperate SO question about this here.
Sony devices use a class named BadgeReciever.
Declare the com.sonyericsson.home.permission.BROADCAST_BADGE permission in your manifest file:
Broadcast an Intent to the BadgeReceiver:
Intent intent = new Intent();
intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", "com.yourdomain.yourapp.MainActivity");
intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", true);
intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", "99");
intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", "com.yourdomain.yourapp");
sendBroadcast(intent);
Done. Once this Intent is broadcast the launcher should show a badge on your application icon.
To remove the badge again, simply send a new broadcast, this time with SHOW_MESSAGE set to false:
intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", false);
I've excluded details on how I found this to keep the answer short, but it's all available in the blog. Might be an interesting read for someone.
There is not a standard way to achieve this; Many makers such as Sony or Samsung have implemented it in their own Android customization.
For example in Samsung, you have to broadcast an intent with BADGE_COUNT_UPDATE action, let MainActivity be your main activity class and count be the number you want to display in your app icon, note that 0 will hide the badge:
Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
intent.putExtra("badge_count", count);
intent.putExtra("badge_count_package_name", context.getPackageName());
intent.putExtra("badge_count_class_name", MainActivity.class.getName());
context.sendBroadcast(intent);
Sony devices uses "com.sonyericsson.home.action.UPDATE_BADGE" action with their custom extras as #Marcus Answered, so you have to add "com.sonyericsson.home.permission.BROADCAST_BADGE" permission to your app manifest and:
Intent intent = new Intent("com.sonyericsson.home.action.UPDATE_BADGE");
intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", MainActivity.class.getName());
intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", true);
intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueOf(count));
intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());
context.sendBroadcast(intent);
Note: it's desirable to query your app's data (context.getPackageName(), MainActivity.class.getName()) rather than hardcode it just in case you do some refactoring in the future.
But somehow the latest Facebook beta version for android does just that...
Not according to the forum thread that contains the screenshot that you linked to. Quoting vakama94:
Well, that's actually TouchWiz and not just the app. I have a Galaxy S II running JellyBean 4.1.2 and it makes the same thing but with some other applications
Whether Samsung has a public API to allow apps to publish numbers to be used as badges, I cannot say. This could be something that they did privately with a few firms.
You are welcome to provide evidence of seeing these badges on a stock Android home screen, such as one of the Nexus series devices.
I answer to this assuming that some flutter dev can search this...
In Flutter, you can achieve this by using
Flutter app badger library.
It is as simple as
FlutterAppBadger.updateBadgeCount(1); // show
FlutterAppBadger.removeBadge(); // hide

Android: How to launch call intent using google voice?

How to launch specific intent (such as call) using google voice? How to pass phone number using intent? Following code launches google voice but what value to be passed for making call using google voice as intent extras?
final Intent intent = new Intent();
intent.setComponent(new ComponentName("com.google.android.apps.googlevoice", "com.google.android.apps.googlevoice.activity.conversationlist.ConversationListActivity"));
intent.putExtra("label", "<phone number>");
startActivity(intent);
Here what should i put in label to start the intent that launches a call using google voice?
Any help is appreciated... Thanks in Advance...
NEVER target applications directly like that UNLESS it is in your package. You should be using the Intent filter to catch that particular application. Sometimes you have to target an application like this, but this brings up the risk of change in package name errors.
To handle your particular application, you need to look at how information is being passed into Google voice. this will give you insight and how to target it WITHOUT targeting the exact package name.
What #JoxTraex said makes sense. However some clients need funny features like this, so we have no way but to implement this:
try {
Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:" + mobile));
intent.setPackage("com.google.android.apps.googlevoice");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} catch (ActivityNotFoundException anfe) {
GMHintManager.getInstance().showError(context, "Google Voice not installed");
}
Yes, you should try-catch ActivityNotFoundException.

Starting Google Maps activity

I am trying to start Google Maps activity like this:
Intent start = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:"+58.0+","+58.0));
startActivity(start);
But I am getting activity not found exception.
Do I have to declare Google Maps Activity?
Any help is appreciated thanks.
This mean Google Maps is not installed (nor anything else that thinks it can handle this link). You need to catch the exception and prompt the user to download from the market.

Categories

Resources