"if'" condition never Go to the Else in android - android

ive been trying to run this code, but the condition result never going to the else ,even when the value of my slat is 0.0
public void keLogin(){
if(slat.equals("0.0")) {
Toast.makeText(getBaseContext(),"pls wait until we know your location",Toast.LENGTH_LONG).show();
} else {
Intent keLogin = new Intent(getApplicationContext(), Login_Activity.class);
keLogin.putExtra("slat", slat);
keLogin.putExtra("slong", slong);
startActivity(keLogin);
}
}

public void keLogin(){
if(!slat.equals("0.0")) {
Toast.makeText(getBaseContext(),"pls wait until we know your location",Toast.LENGTH_LONG).show();
} else {
Intent keLogin = new Intent(getApplicationContext(), Login_Activity.class);
keLogin.putExtra("slat", slat);
keLogin.putExtra("slong", slong);
startActivity(keLogin);
}
}

If the salt is NOT a String variable: so you should convert it
and If it's a String variable:
Maybe your app don't change that variable
or that variable didn't achieve to "0.0"
In 1st option you must add some code for changing that value
In 2nd option you can must change "0.0" to an accessible value

Try using
if(slat.equalsIgnoreCase("0.0") { }
Might work.

Related

Android executing both IF and ELSE statement together

I am working with FireBase Notifications and I can send a notification which will send the user to the webview page I input on the console.
The problem is that when it matches the IF statement is fires the else statement too, what could be the cause of this?
if(getIntent().getExtras()!=null) {
for (String key : getIntent().getExtras().keySet()) {
if (key.equals("url")){
mwebView.loadUrl("http://example.com/" + getIntent().getExtras().getString(key));
}else {
mwebView.loadUrl("http://example.com");
}
}
}
Because it executes both at the same time the app crashes.
Also when I load the app the usual way it matches the with:
if(getIntent().getExtras()!=null)
and then loads the else statement. Shouldnt getExtras be null?
When I first install a new instance of the app it uses the following statement:
if(getIntent().getExtras()==null) {
if (haveNetworkConnection()) {
mwebView.loadUrl("http://example.com");
} else {
mwebView.loadUrl("file:///android_asset/myerrorpage.html");
}
}
Update:
As I cannot find out why this it happening I am trying another approach, How would I get the variable outside of the loop to use like the following:
if(getIntent().getExtras()!=null) {
for (String key : getIntent().getExtras().keySet()) {
String valuex = getIntent().getExtras().getString(key);
}
}
if (haveNetworkConnection()) {
mwebView.loadUrl("http://example.com/" + valuex);
} else {
mwebView.loadUrl("file:///android_asset/myerrorpage.html");
}
If and Else can not both be executed in one go.
You should check your other code and ensure, that this code section is not executed twice for some reason (once with TRUE and once with FALSE).

How to know when Activity is finished in android?

In my android app I want to change the input method. So I start a new Activity which shows the language settings in the device. Then user can change it. However then I want to know that if the user has changed it. So I wrote a function for that also. My code so far is...
Intent enableIME = new Intent(android.provider.Settings.ACTION_INPUT_METHOD_SETTINGS);
startActivityForResult(enableIME,0);
if(isInputMethodEnabled()){
activateshadow.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_pressed));
activateshadow.setText("Deactivate Shadow");
prefs.edit().putBoolean("Activate", false).commit();
}else{
Toast.makeText(MainActivity.this,"You haven't change the input method to simpleIME.In order to activate you must change it.",Toast.LENGTH_SHORT).show();
}
my is inputMethodEnabled function is....
public boolean isInputMethodEnabled() {
boolean isIME ;
String id = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
String [] name = id.split("/.");
// Toast.makeText(MainActivity.this,"s:"+name[1]+":s",Toast.LENGTH_SHORT).show();
if(name[1].contains("SimpleIME") ){
isIME = true ;
}else{
isIME = false;
}
// Toast.makeText(MainActivity.this,"Returning..."+isIME,Toast.LENGTH_SHORT).show();
return isIME;
}
if(isInputMethodEnabled()) always fails because when the new intent(settings) opens and it take some time to change the input method to simpleIME . How to fix this problem?
You catch when a launched Activity returns in onActivityResult. The requestCode you supplied to startActivityForResult will be a parameter, as will the Activity's result. The Activity may also set other data which you didn't ask about.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 555) {//555 is the intent ID you gave in startActivityForResult(enableIME,555);
if (resultCode == /*Result1*/)
//Do something
else {
//Do something else
}
}
}
You need a unique id when calling startActivityForResult(enableIME,0);
startActivityForResult(enableIME, 555);
Better still replace 555 with a named variable.
if u look at android life cycle, when activity is finished whole android call onDestroy() method.
so u can call and override this method.
just need write:
#override
protected void onDestroy(){
// code
super.onDestroy();
}
u can manage and override all of life cycle's parts in android
e.g: onResume to get current activity
i hope this help u

unable to make call in KitKat

I am working on csipsimple version 1915. It work fine for android devices below 4.4.2 . To call somebody in local network I have this method
private void broadCastAndroidCallState(String state, String number) {
//Android normalized event
Intent intent = new Intent(ACTION_PHONE_STATE_CHANGED);
intent.putExtra(TelephonyManager.EXTRA_STATE, state);
if (number != null) {
intent.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER, number);
}
intent.putExtra(pjService.service.getString(R.string.app_name), true);
pjService.service.sendBroadcast(intent,android.Manifest.permission.READ_PHONE_STATE);
}
But it does not work on android version 4.4.2. When I want to make a call it crashes. I have modified my code as follows
private void broadCastAndroidCallState(String state, String number) {
// Android normalized event
if(!Compatibility.isCompatible(19)) {
// Not allowed to do that from kitkat
Intent intent = new Intent(ACTION_PHONE_STATE_CHANGED);
intent.putExtra(TelephonyManager.EXTRA_STATE, state);
if (number != null) {
intent.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER, number);
}
intent.putExtra(pjService.service.getString(R.string.app_name), true);
pjService.service.sendBroadcast(intent, android.Manifest.permission.READ_PHONE_STATE);
}
}
By modifying this my application does not crash but cant hear anything. Please help me with this.
There are two way, first way you can set phone number in textview and set autoLink in textview. So, it's handle everything.
Second thing is that,
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:"+phonenumber));
startActivity(callIntent);

How to set my username and password in the code.?

Button loginbuttonbutton = (Button) findViewById(R.id.btnLogin);
loginbuttonbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(inputEmail.getText().toString() == "EdEffort#ncat.edu" &&
inputPassword.getText().toString() == "Steelers") {
Intent myIntent = new Intent(view.getContext(),
Host_Setting_PageActivity.class);
startActivityForResult(myIntent, 0);
} else {
System.out.println("Username or password is incorrect");
}
}
});
That is my code and the application actually start but whenever I hit the login button the application closes.
first use .equals() to compare strings.
== compares string refrences.Not value.
.equals() = compare strings character equality
if((inputEmail.getText().toString().equals("EdEffort#ncat.edu")&&inputPassword.getText().toString().equals("Steelers"))
And if force close than put logcat here..
use equals insead of == for String comparison
loginbuttonbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(inputEmail.getText().toString().equals("EdEffort#ncat.edu") && inputPassword.getText().toString().equals("Steelers") ){
Intent myIntent = new Intent(YOUR_CURRENT_ACTIVITY.this, Host_Setting_PageActivity.class);
startActivityForResult(myIntent, 0);
}
else{
System.out.println("Username or password is incorrect");
}
}
});
and make sure you are registering YOUR_CURRENT_ACTIVITY.this and Host_Setting_PageActivity.class both in manifest.xml
You should add
Host_Setting_PageActivity.class to your AndroidManifest
And also, you should compare strings always using .equals and not with "==" as "==" will really compare the instance of the string object and .equals will check the value of the string
dont use == method in if Condition but used .equals() method
like as
if(inputEmail.getText().toString().equals("abc") && inputPassword.getText().toString().equals("abc") ){
----------your code here-----------
}

Android: getSharedPreferences for session

Okay, check this source code:
public void checkSession() {
SharedPreferences session = getSharedPreferences(PREFS_NAME, 1);
String getSession = session.getString("SESSION", null);
Toast.makeText(this, getSession, Toast.LENGTH_SHORT).show();
if(getSession.length() > 30) {
Intent menu = new Intent(this, menu.class);
startActivity(menu);
finish();
}
else
{
}
}
The problem is that "first time users" get crush.
When I disable the function, run the app and login the code creates session. After that when I logout, and restart the app - there's no problem. Any ideas?
First time the app runs and you don't have a value stored in the SharedPreference "SESSION" you return null as your default value. The getSession().length will then result in a NullPointerException.
You should do like this instead:
String getSession = session.getString("SESSION", "");
If getSession is null, I think Toast.makeText will fall over.* You might want to change the default return to "" from null. getSession.length() won't work if getSession is null either.
*Apparently that isn't true -- see TofferJ's comment.

Categories

Resources