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

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.

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.

Login with multiple login credientials

I am creating an app for my company and I am very new to coding and app development. I am currently working on a login screen that has multiple login credentials. But I am not sure how to set it up properly. I was able to make it work with a single login but now I am trying to get it to work with more than just 1. So here is my setup I just think it is a formatting error. The Error I get is that"" }else{ "unexpected token" "" Any help would be great thanks in advance!!!
private void validate(String userName, String userPassword) {
if ((userName.equals("Admin")) && (userPassword.equals("Pass")) ||
((userName.equals("Shop")) && (userPassword.equals("4321"))));
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}else{
counter--;
Info.setText("No of attempts remaining: " + String.valueOf(counter));
if (counter== 0){
Login.setEnabled(false);

Understand SharedPreferences Android

In android I want to make a basic login and registration application. I am following this tutorial. The application works properly and runs. I am just trying to understand the code now and after many google searches I can not understand some of the code and was wondering if somebody could help me understand it.
Below I have posted the method I do not understand and in comments highlighted what I do not understand - any clarification is much appreciated, I have also commented the code to what I believe the code does, if any of it is incorrect please tell me, you can also view all of the code on the tutorial website.
I am mainly confused about how the sharedpreferences works I have followed his tutorial on sharedpreferences too I understand that but do not understand this. Thank you and sorry if the problem is very basic
private void checkLogin(final String email, final String password) {
// Tag used to cancel the request
String tag_string_req = "req_login";
// Dialog stating trying to login
pDialog.setMessage("Logging in ...");
showDialog();
// Send the request over to the database to check details
StringRequest strReq = new StringRequest(Method.POST,
AppConfig.URL_LOGIN, new Response.Listener<String>() {
// Do this once you get a response
#Override
public void onResponse(String response) {
Log.d(loginName, "Login Response: " + response.toString());
hideDialog();
// Break the response up into individual things and store in variables
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
// Check for error node in json
if (!error) {
// I DO NOT UNDERSTAND THIS!!! how does this bit work?
// it sets the shared preferences login to true correct?
// but how does it set it true to only this particular user?
// Because it doesnt store the email and password along with it
// and sets its tag "isLoggedIn" and then saves it to the shared
// preferences
session.setLogin(true);
// Now store the user in SQLite
String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
String name = user.getString("name");
String email = user.getString("email");
String created_at = user
.getString("created_at");
//I DO NOT UNDERSTAND THIS!!! Why do you need to do this & does this
//affect the MySQL DB at all?
db.addUser(name, email, uid, created_at);
// I DO NOT UNDERSTAND THIS!!! Why do you need to write LoginActivity.this
// do you not just write MainActivity?
Intent intent = new Intent(LoginActivity.this,
MainActivity.class);
startActivity(intent);
finish();
} else {
// Error in login. Get the error message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e(loginName, "Login Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
/***************************************************************/
//I DO NOT UNDERSTAND THIS WHOLE METHOD WHY DO YOU DO THIS?!!!
/***************************************************************/
#Override
protected Map<String, String> getParams() {
// Posting parameters to login url
Map<String, String> params = new HashMap<String, String>();
params.put("email", email);
params.put("password", password);
return params;
}
};
// FINALLY I ALSO DO NOT UNDERSTAND WHY YOU DO THIS! AND WHAT DOES IT DO
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
This adds a user to an SQL database:
db.addUser(name, email, uid, created_at);
There should be a class somewhere that defines the actual function, which then creates the query that actually interacts with the database.
The intent changes the activity (what is rendered on the screen and what logic is handled):
LoginActivity.this: the context in the current class - this can be simplified to just this, but it's a bit of syntactic sugar in Java that attempts to clarify which this is being referred to.
MainActivity.class: the target activity
Intent intent = new Intent(LoginActivity.this,
MainActivity.class);
The difference between two activities can be explained with the content of a game. The menu is "LoginActivity.this" and "MainActivity.class" is the actual game content
As for shared preferences, the usage is pretty straight-forward:
To obtain shared preferences, use the following method In your
activity:
SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
To read preferences:
String dateTimeKey = "com.example.app.datetime";
// use a default value using new Date()
long l = prefs.getLong(dateTimeKey, new Date().getTime());
To edit and save preferences
Date dt = getSomeDate();
prefs.edit().putLong(dateTimeKey, dt.getTime()).apply();
(Source, posted by naikus)
The internal mechanics aren't something you need to worry about - the thing you really need to know is that it's able to save your data in a way you can use that doesn't involve directly accessing files (which has become a maze since Android 10).
EDIT:
Based on what I saw at the tutorial, the entire thing is to check if the login information entered exists in the database. The getParams() method defines what goes into the form data

why package manager is always is null?

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.

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