Start Play Store app in Android using navigation component - java

I have an Android application with a button to find other games on Play Store. This button should open the Google Play Store application. I have a working version which uses the startActivity() function and Intent flags, just as mentioned in this official doc.
Intent intent = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("https://play.google.com/store/apps?someapp")
.setPackage("com.android.vending");
this.startActivity(intent)
Is this possible to achieve using Navigation Components instead? I have tried implementing a Navigation Deep Link and to set the equivalent of intent actions and data like so:
<activity
android:id="#+id/playstore_activity">
<deeplink
app:uri="https://play.google.com/store/apps?someapp"
app:action="android.intent.action.ACTION_VIEW"
app:targetPackage="com.android.vending"/>
</activity>
But I am getting an ActivityNotFoundException: No activity found to handle Intent error. What am I doing wrong? I am unable to find any further documentation of this.

I figured it out in the end:
<activity
android:id="#+id/playstore"
android:label="Travel to PlayStore"
app:action="android.intent.action.VIEW"
app:data="#string/playstore_path"
app:targetPackage="com.android.vending"
/>

Related

How to make the Android app load into logo then back to main activity?

I am new to App Development and I've been studying Kotlin for only a month now (1 hour everyday). I got the grasp of the functions but I still haven't gotten around using them for the purposes I have in mind.
Using Android Studio, I am trying to make the App load into a logo upon opening the app(Just like Facebook, Reddit), the logo is animated (which is not a problem for me). I have a couple of ways to achieve this but I wanna see what's the most efficient way to do it.
Create a new SplashActivity and change your starting activity in you manifest file like this:
<activity
android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
then in your SplashActivity's OnCreate, start your MainActivity
startActivity(Intent(this, MainActivity::class.java))
You can read more about this here.
You can create one Activity let's say SplashActivity which will have your logo and animations you want. Then from that Activity you can start your MainActivity. If you need to process some data inside your SplashActivity you do that then you start MainActivity. Otherwise, if you don't have any data to process you can simply start MainActivity with some delay, like this:
Handler().postDelayed({
val i = Intent(this, MainActivity::class.java)
startActivity(i)
}, 5000)
This will start your MainActivity after 5000 milliseconds which is 5 seconds. Now try to write some code and if you run into a problem try to find a solution or ask again if you can't. There is a lot of tutorials on the Internet you can find on how to do this.

How to set specific data from database in fields when launch through activity shortcut?

I am developing an android based note taking application with categories.I am supposed to create notes shortcuts on home screen. When user click on the shortcut the relevant activity should be open and the specific data should be set in Edit-texts i.e Its title and description.I unable to understand the logic to do that.
I tried all possible solutions that come into my mind. I passed Id of note in shortcut intent but when it launch from shortcut the fields are still empty.
This is my snippet of code to create shortcut:
Function to create shortcut:
private void createShortcutOfActivity() {
Intent shortcutIntent = new Intent(getApplicationContext(),
TextNotes.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, editTitle.getText().toString());
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.mipmap.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false); //may it's already there so don't duplicate
getApplicationContext().sendBroadcast(addIntent);
}
This function is called when user click on option to create shortcut.
In Menifest use permission:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Also add intent filter and exported property in non launching activity:
<activity
android:name=".TextNotes"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
The activity receive data from intent when open a note from another activity:
Intent newInt=getIntent();
isDoubleClicked=newInt.getBooleanExtra("Chk",false);
Cat=newInt.getStringExtra("Category");
Id=newInt.getIntExtra("Id",0);
String title=newInt.getStringExtra("Message");
String description=newInt.getStringExtra("Message2");
check=newInt.getStringExtra("Check");
editTitle.setText(title);
editText.setText(description);
I also tried to use this id in the shortcut intent of the function but having no change in result.
shortcutIntent.putExtra("key_primary",Id);
I want to keep the data when open using shortcut.For example for different note shortcut the rspected data should be set in fields just like in whatsapp the chat shortcuts of different contacts can be craeted . But unfortunately I am unable to understand that how should it be done because everytime I open using shortcut its fields become empty. Where should I pass id and how to set data when it launch from shortcut.
If you use shortcutIntent.putExtra("key_primary",Id); then you need to retrieve it using
Id=newInt.getIntExtra("key_primary",0);
That is the first parameter (key_primary) is the key that is used to identify the specific Intent Extra. Thus, the key value must match the key value used when putting the Intent Extra for a value (other than the default) to be retrieved.
As such coding Id=newInt.getIntExtra("Id",0); without using the matching/paired shortcutIntent.putExtra("Id",Id); will always result in 0 as the Intent Extra doesn't exist so the default value is returned.

Home screen shortcut wont add

I am trying to add a short on home screen programmatically but it wont work.
The code works for Oreo and above but not below that.
public void addShortcutToHomeScreen() {
if (ShortcutManagerCompat.isRequestPinShortcutSupported(this)) {
ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(this, "#1")
.setIntent(new Intent(this, ActivityMemes.class).setAction(Intent.ACTION_MAIN)) //!!! intent's action must be set on oreo
.setShortLabel("Memeify")
.setIcon(IconCompat.createWithResource(this, R.mipmap.ic_launcher))
.build();
ShortcutManagerCompat.requestPinShortcut(this, shortcutInfo, null);
} else {
Intent shortcutIntent = new Intent(getApplicationContext(),
Activity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Name");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.mipmap.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false);
getApplicationContext().sendBroadcast(addIntent);
}
}
Here is my Manifest file.
<activity
android:name="com.my.package.Activities.Activity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Please, Tell me what am I doing wrong cause I have tried to fixed it and have failed. And is there any better way than this? Any help would be appreciated thanks.
EDIT :
Found this in the Logcat.
2019-01-16 13:27:46.773 735-13377/? W/BroadcastQueue: Permission Denial: broadcasting Intent { act=com.android.launcher.action.INSTALL_SHORTCUT flg=0x10 launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } (has extras) } from com.chameleon.memeify (pid=14824, uid=10300) requires com.android.launcher.permission.INSTALL_SHORTCUT due to receiver com.sec.android.app.launcher/com.android.launcher3.home.InstallShortcutReceiver
you can use this method in your application class
public void createShortCut() {
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutintent.putExtra("duplicate", false);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.logo);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), SplashActivity.class));
sendBroadcast(shortcutintent);
}
}
and apply this permission
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
If your are on Android 8+ you can't use the broadcast anymore and you have to use the ShortcutManager :
https://developer.android.com/about/versions/oreo/android-8.0-changes#as
Android 8.0 (API level 26) includes the following changes to app shortcuts:
The com.android.launcher.action.INSTALL_SHORTCUT broadcast no longer has any effect on your app, because it is now a private, implicit broadcast. Instead, you should create an app shortcut by using the requestPinShortcut() method from the ShortcutManager class.
The ACTION_CREATE_SHORTCUT intent can now create app shortcuts that you manage using the ShortcutManager class. This intent can also create legacy launcher shortcuts that don't interact with ShortcutManager. Previously, this intent could create only legacy launcher shortcuts.
Shortcuts created using requestPinShortcut() and shortcuts created in an activity that handles the ACTION_CREATE_SHORTCUT intent are now fully-fledged app shortcuts. As a result, apps can now update them using the methods in ShortcutManager.
Legacy shortcuts retain their functionality from previous versions of Android, but you must convert them to app shortcuts manually in your app.
To learn more about changes to app shortcuts, see the Pinning Shortcuts and Widgets feature guide.

calling another activity android manifest xml

I have two sperate applications and I want to call start an activity from the second application in the first, here is my code to do so :
Intent intent1 = new Intent(Intent.ACTION_MAIN);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent1.setComponent(new ComponentName("org.two.three.application","org.two.three.application.one));
Context H= context;
H.startActivity(intent1);
And in the android manifest of the project I have this code, I have the line :
<activity android:name=".one">
</activity>
But I keep getting a runtime error, logcat says :
"Unable to find explicit activity class
{org.two.three.application/org.two.three.application.one}; have you
declared this activity in your AndroidManifest.xml?"
Can anyone see my error? The only thing I can think of is my package of the first activity is org.two.three.Class while the second is org.two.three.application.SecondClass. Does this matter?
Thanks in advance
At first try removing those code that you are adding
**
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent1.setComponent(new ComponentName("org.two.three.application","org.two.three.application.one));
Context H= context;
**
Then add following code into an action method like onClick
Intent intent = new Intent(this, NewActivity.class);
startActivity(intent);
Add your Android Manifest configuration file
<activity android:name="NewActivity"></activity>
You just need to make your activity publicly available. To do that just add
android:exported="true"
to the <activity> tag in your manifest.
Normally, activities are not available to other components that are outside of the package. This is the standard default behaviour. But, of course, you can make them available if you want to.

How can I get the package name of the current launcher in android 2.3 and above?

How can I get the package name of the current launcher in android 2.3 and above programmatically in Java ?
I think you should be able to use PackageManager.resolveActivity(), with the home intent.
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
ResolveInfo resolveInfo = getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
String currentHomePackage = resolveInfo.activityInfo.packageName;
With the package visibility changes introduced in Android 11, it is now necessary to add a queries element in your application's manifest file as below before you can query the PackageManager.resolveActivity(intent:flags:) method for the default home (a.k.a. launcher) activity that is installed on the device:
<queries>
<intent>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
</intent>
</queries>
If this queries element is omitted from your application's manifest, then the device will report the com.android.settings.FallbackHome activity as its default home activity and that is most likely not what you want.
For guidance on how to query the PackageManager.resolveActivity(intent:flags:) method, see the accepted answer in this thread.
in general, I agree to #JesusFreke using the PM resolveActivity
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
ResolveInfo resolveInfo = getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
but to get the right package name, you should use
resolveInfo.loadLabel(packageManager).toString()
or
resolveInfo.activityInfo.applicationInfo.loadLabel(packageManager).toString()
Hint:
if there is no default set, this might become "Android System" or "open" as for the general System Intent Receiver
Hint:
if you're looking for web browsers, you might use net.openid.appauth.browser.BrowserSelector#select() (0.7.1+) to implicit get the default browser even there is no one explicit set.

Categories