'How to open Google Fit app programmatically?
So, I have an app that uses google sign-in and Google Fit apis to register some data on the be, I would also like to open the Google Fit app from the app's settings.
I've already tried to open it using an Intent and passing the Google Fit's app package name but with no luck.
How I could open it programmatically on user's click?
Solution 1:[1]
hello my friend,
First: you should use PackageManager class from Android
Second:
The App that you need to launch must decalare its FirstActivity as (Intent#CATEGORY_LAUNCHER) Or (Intent#CATEGORY_INFO) this is not complex these of two category declare in ManifestFile here is sample ManifestFile of destination App:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mkyong.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="List of Mobile OS"
android:name=".ListMobileActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="List of Fruits"
android:name=".ListFruitActivity" >
</activity>
</application>
Ok we consider that Google Fit App has at least one of those flags so now by Packagemanger class we can launch that app
Kotlin:
val launchIntent =
packageManager.getLaunchIntentForPackage("package-name")
if (launchIntent != null) {
startActivity(launchIntent)
} else {
Toast.makeText(
this@MainActivity,
"There is no package available in android",
Toast.LENGTH_LONG
).show()
}
Java:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("package-name");
if (launchIntent != null) {
startActivity(launchIntent);
} else {
Toast.makeText(MainActivity.this, "There is no package available in android", Toast.LENGTH_LONG).show();
}
- package manger and those two Flags in manifest of destination app are Key
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | soheil ghanbari |
