why package manager is always is null? - java

i try to show user location in my android app , but it dosnt work! package manager is always null.
private void openPrefredLocationInMap (){
String location =
PreferenceManager.getDefaultSharedPreferences(this)
.getString(getString(R.string.pref_location_key)
, getString(R.string.pref_defult));
Uri geoLocation = Uri.parse("geo:0,0?").buildUpon()
.appendQueryParameter("q", location).build();
Intent intent= new Intent(Intent.ACTION_VIEW,geoLocation);
if (intent.resolveActivity(getPackageManager()) != null)
startActivity(intent);
else
Log.d("package","couldnt call"+location);
}

Your device does not have an application that could handle such intent. Install an application that can handle that intent or make one yourself. To be more specific, you might not have a map application that would be able to display geoLocation intent.
Try installing Google Maps for example.

Related

Trying to send a picture with text to Instagram stories but com.instagram.share.ADD_TO_STORY does nothing

I have read a lot of information on various sites, but not a single method works. There are also a lot of old solutions on the sites that don't work either. Here is my code:
Uri stickerAssetUri = Uri.parse("https://firebasestorage.googleapis.com/v0/b/schooly-47238.appspot.com/o/miners%2Ffimw.png?alt=media&token=9798e9ea-15a0-4ef2-869b-63ce4dc95b78");
String sourceApplication = "com.egormoroz.schooly";
Intent intent = new Intent("com.instagram.share.ADD_TO_STORY");
intent.putExtra("source_application", sourceApplication);
intent.setType("image/зтп");
intent.putExtra("interactive_asset_uri", stickerAssetUri);
intent.putExtra("top_background_color", "#33FF33");
intent.putExtra("bottom_background_color", "#FF00FF");
Activity activity = getActivity();
activity.grantUriPermission(
"com.instagram.android", stickerAssetUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (activity.getPackageManager().resolveActivity(intent, 0) != null) {
activity.startActivityForResult(intent, 0);
}
When this part of the code is triggered, nothing happens to the application. It remains on the same screen.
Based on official documentation from https://developers.facebook.com/docs/instagram/sharing-to-stories/.
You can change this
intent.setType("image/зтп");
to
intent.setType("image/*");
or
intent.setType(MEDIA_TYPE_JPEG);
I think sourceApplication should be your Facebook App ID, not your package name.

Some Android phones not opening “About device” page in Settings?

Some Android phones don't do anything when the the code below is ran. It's supposed to open the "About device" page in Settings.
For example, I know for a fact that it has no effect on the Huawei Y9 Prime 2019 running Android 10.
startActivity(new Intent(Settings.ACTION_DEVICE_INFO_SETTINGS));
What's the best way to safeguard against this issue when it occurs? (In my app, I open this page to ask the user to perform a specific action there)
Use PackageManager.resolveActivity() to check whether such an Activity exists. If that returns null, there is no activity that matches the Intent and you have to guide your customers to the settings in another way:
Intent intent = new Intent(Settings.ACTION_DEVICE_INFO_SETTINGS);
ResolveInfo resolveInfo = getPackageManager().resolveActivity(intent, 0);
if (resolveInfo == null) {
// help customers by describing how to get to the device info settings
} else {
startActivity(intent);
}
If an activity is not found, the method startActivity will throw android.content.ActivityNotFoundException. You should catch this exception and notify the user:
try {
startActivity(new Intent(Settings.ACTION_DEVICE_INFO_SETTINGS));
} catch (ActivityNotFoundException e) {
// Notify the user, eg. using a popup so they can open settings manually
}

not entering the condition if(intent.resolve(getPackageManager()){} neither else{}

Hey I want my app to locate someone on a map so I made an intent but it's not working and I've no error so I'm a bit lost. I think I've no app that can perform a map location on my emulator but even then I should have that said in my logs. Here is the code:
private void openLocationInMap(){
Log.d("map","in fct");
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
final String BASE_URL ="geo:0,0?q=";
Log.d("map","1");
String locationData = sharedPreferences.getString(getString(R.string.pref_location_key),
getString(R.string.pref_location_default));
Log.d("map","2");
Uri uriGeolocalisation = Uri.parse(BASE_URL).buildUpon()
.appendQueryParameter("q", locationData).build();
Log.d("map","3");
Intent intent = new Intent (Intent.ACTION_VIEW);
intent.setData(uriGeolocalisation);
Log.d("map","4");
if(intent.resolveActivity(getPackageManager()) != null) {
Log.d("map", "in if");
startActivity(intent);
}else{
Log.d("map", "couldn't open intent");
}
Log.d("map","5");
}
And in my log debug I get :
in fct, 1,2,3,4.
Edit: if that can help it works on my phone but still not working on the emulator. I'm guessing it's because I don't have google map on my emulator but still I should see something in the logs which I don't.

Installing shortcut wihout duplicating native handly created one

I want to add a shortcut to my application, but I cannot manage not to duplicate native handly created one (by using drag and drop on application Icon in Application Menu to Home Screen for example).
Here is my code:
public void addShortcut(Context context)
{
this.manageShortcutAction(context, "com.android.launcher.action.INSTALL_SHORTCUT");
}
public void deleteShortcut(Context context)
{
this.manageShortcutAction(context, "com.android.launcher.action.UNINSTALL_SHORTCUT");
}
private void manageShortcutAction(Context context, String intentAction)
{
Context applicationContext = context.getApplicationContext();
Intent shortcut = new Intent(intentAction);
ApplicationInfo appInfo = applicationContext.getApplicationInfo();
PackageManager packageManager= applicationContext.getPackageManager();
String applicationName = (String) packageManager.getApplicationLabel(appInfo);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, applicationName); // Shortcut name
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, packageManager.getLaunchIntentForPackage(appInfo.packageName));// Setup activity should be shortcut object
shortcut.putExtra("duplicate", false); // Just create once
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(applicationContext, appInfo.icon));// Set shortcut icon
applicationContext.sendBroadcast(shortcut);
}
And my manifest required permissions:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
By the way, I had overwritten the Application code which is now MainApplication extending Application.
I have already tried to create a component to create the Intent.EXTRA_SHORTCUT_INTENT, without the expecting result.
If anyone has an idea...
I found out that in order to prevent the app from creating a duplicate of the shortcut created at app installation (or by copying from the menu), I have to create one with the same parameters of the existing one - using the same shortcut name is not enough.
After a lot of testing and thanks to the logcat, I've been able to create an exact replica as follows:
private void installShortcut() {
final Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
final Intent intent = new Intent();
intent.putExtra("duplicate", false);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(intent);
}
The difference with other answers I found on SO is in these 3 lines:
shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
which I figured out by looking at the log entry printed when manually launching the app using the system created shortcut
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.myapp/.activities.main.MainActivity bnds=[365,73][475,191] } from pid 279
After having discuss with Sony's developers, there is no way not to duplicate manually created shortcuts...
I found some methods for Samsung but these were not generic.

Start Android Market from App

I'm developing a lite version for an app on the Android. How can I start an Intent to open the Android Market, preferably with the full version of my app displayed? This is difficult to test on an emulator (which is the closest thing to a device I have), as there seems to be no legal way of installing the Market on it.
That query above works, but when I tried it, it looked like it was bringing up search results based on the name.
If you use something like
intent.setData(Uri.parse("market://details?id=com.wolinlabs.SuperScorepad"));
instead, it will go right to the Android Market page for your app.
I think that's more what you wanted (?)
Found answer in the end:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://search?q=pname:MyApp"));
startActivity(intent);
No way of testing on emulator, though.
Hi I was trying the achieve the same but with one small difference
I DIDN'T WANT TO OPEN IT EMBEDDED ON MY APP
public void start(JSONArray args, CallbackContext callback) {
Intent launchIntent;
String packageName;
String activity;
String uri;
ComponentName comp;
try {
packageName = args.getString(0); //com.android.vending
activity = args.getString(1); //com.google.android.finsky.activities.LaunchUrlHandlerActivity
uri = args.getString(2); //'market://details?id=com.triplingo.enterprise'
launchIntent = this.cordova.getActivity().getPackageManager().getLaunchIntentForPackage(packageName);
comp = new ComponentName(packageName, activity);
launchIntent.setComponent(comp);
launchIntent.setData(Uri.parse(uri));
this.cordova.getActivity().startActivity(launchIntent);
callback.success();
} catch (Exception e) {
callback.error(e.toString());
}
}
THE BIG DIFFERENCE HERE IS THAT YOU START A NEW APP NOT JUST SHOW GOOGLE PLAY IN YOUR
APP
This code is part of a Cordova plugin but is pretty obvious what you need to do to use it natively.
THE IMPORTANT LINES
launchIntent = this.cordova.getActivity().getPackageManager().getLaunchIntentForPackage(packageName);
comp = new ComponentName(packageName, activity);
launchIntent.setComponent(comp);
launchIntent.setData(Uri.parse(uri));
Regards

Categories

Resources