I experience some errors while trying to push data into my firebase realtime database. I checked a couple of tutorials and according to all of them i am doing nothing wrong. Unfortunately I can not update the database from my android studio emulator device.
I activated debug mode and stepped through every single process and saw that my code actually works because the database responses with the child id when pushing to it.
package com.example.fbconnect;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity {
Button btnsaves;
Member member;
DatabaseReference myRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnsaves=(Button)findViewById(R.id.button);
btnsaves.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myRef = FirebaseDatabase.getInstance().getReference("Member");
String id = myRef.push().getKey();
member = new Member("MSmith", 12, id);
myRef.child(id).setValue(member);
Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
}
});
}
}
When debugging to this line
String id = myRef.push().getKey();
I see, that the database answered with a code. But it will not set the new value or update the value of existing database Childs.
After activating the debug mode I recognised some rare lines in android studio that may lead to the problem:
E/FirebaseInstanceId: Topic sync or token retrieval failed on hard failure exceptions: AUTHENTICATION_FAILED. Won't retry the operation.
I am definitely sure that I did not activate any authentication in my firebase. Normally everybody should be able to read and write (I am aware of this but I am desperate to get this database connection working).
Can you please help me?
If anyone reads this and gets this error,
Firebase Realtime Database connection killed: Different Region
Make sure your google-services.json file has the right firebase_url.
If it doesn't work right away,
Clean Project
Invalidate Caches
After restart, it should be working if that was the case.
Go to firebase console > Database > Firebase Realtime Database > Rules tab Change read and write to true.
Also check if your device is connected to the internet.
By Default Rule in Firebase is for Authorized requests
you can change read and write to True
BUT IT IS NOT SECURE
any one with your Firebase Url Can Access And Change Data
Related
I am new to Android Studio. I successfully created a Hello World app from the example in Android website. Now, I want to play around by using some statement to print in the logcat but it doesn't works. Below is my ApplicationTest.java code:
package com.example.abc.myloggingapplication;
import android.app.Application;
import android.test.ApplicationTestCase;
import android.util.Log;
/**
* Testing Fundamentals
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
Log.d("MyTest", "Here goes the output!"); // THIS IS THE NEW STATEMENT INSERTED BUT PRINTS NOTHING IN CONSOLE LOG IN ANDROID STUDIO SDK.
}
}
Other files are same as provided by the Hello World example in Android website. In Android Studio sdk, I entered the logcat to debug but still no luck. Can anybody point me out where I am going wrong.
Check your logcat:
Check View -> Tool windows -> Android Monitor.
Or Alt + 6
If that doesnt help, make sure you have an instance of your class. Otherwise the constructor is never called and therefor the log.d is never called.
Edit:
As other's have stated: Check if you are running the Test Application, if you want to do so. Otherwise make sure you code in your actual application and not your test application.
I have two issues with this, my very first android app. The first is that I noticed after I installed the app the battery started draining about twice as fast as before. I read an article on this that stated sometimes programmers make an error which causes this. This being my first app, the probability is pretty high that this is the case.
The second issue is that I can save my variables when the app is turned off, but when the phone is turned off the data is lost.
As for the code, it's mostly bits and pieces from stuff I've found online and tried to incorporate. It's a simple calculator with three variables. Ideally, I'd like to store b and c, but allow the user to overwrite them. Variable a will change with each use, so no need to store that.
Here's my Main:
package com.kwagz.calc;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
String g, e;
String b; //changed(sorry)
String c; //changed
SharedPreferences sh_Pref;
Editor toEdit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadSavedPreferences();
}
public void calculateClickHandler(View view) {
if (view.getId() == R.id.button1) {
EditText a = (EditText)findViewById(R.id.a);
EditText b = (EditText)findViewById(R.id.b);
EditText c = (EditText)findViewById(R.id.c);
TextView output = (TextView)findViewById(R.id.textView9);
double gp = Double.parseDouble(a.getText().toString());
int ab = Integer.parseInt(b.getText().toString());
int ac = Integer.parseInt(c.getText().toString());
double t = ((gp / ab) * ac);
output.setText(String.format("%.2f", t));
}
}
public void sharedPreferences() {
SharedPreferences saved_values =//there's no linebreak here in my code
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor=saved_values.edit();
editor.putString("b", b);
editor.putString("c", c);
editor.commit();
}
private void loadSavedPreferences() {
SharedPreferences saved_values =//there's no linebreak here in my code
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
b = saved_values.getString("b", g);
}
}
From reading what you said, the only logical explanation forming in my head is: You are starting background service from your App, while background service is running it makes app run all the time which explains why variables are stored until phone is turned off (when background service gets killed).
Okay, let's take your questions one at a time. The battery problem could be a number of things, without knowing more details about your program I couldn't possibly diagnose it completely. Android does have some good power saving tips. The key item is to minimize the use of connectivity devices to no more than is required. The two biggest culprits are GPS and Internet connections. If your app isn't running, then the only way it could be running the batteries is if there's threads in the background, or maybe something like an AlarmService.
You are using SharedPreferences correctly, the value should be saved, but I think you should but the saving code in onSaveInstanceState(). If you do that, it should work fine.
EDIT
Upon closer inspection, you're fundementally saving the wrong thing. You should save the value of the EditText, not the EditText itself. In fact, I'd simply remove all references to String a,b,c, move the EditText definitions to the top to replace them, and do something like this:
editor.putString("b", b.getText().toString());
b.setText(saved_values.getString("b", g));
I've conducted several tests in an effort to recreate the issue, with no success. I'm still working on the sharedPreferences thing, but the battery drain (I suspect) was caused by another app which just happened to coincide with the installation of mine.
As I mentioned in my note to arleitiss, my Y!Mail was showing the highest amount of battery drain. A day or two after posting the note my account started acting up and then became "inactive". Once I reset it, the app no longer appears on the Battery list.
Thanks for all the input!
I did everything properly, by useingdebug.keystore I generated my MD% code and I got my Valid API key from google website and I gave permission for using internet connection in MasinFest.xmland used my mapping key value in main.xml.and in MapActivity File I simply used this below code
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import android.os.Bundle;
public class MapsActivity extends MapActivity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
I am using Android 4.0.3 version and Google API Level 15, I am Getting Error as 05-22 10:32:42.086: E/MapActivity(656): Couldn't get connection factory client in LogCat.. Help me regarding this please.. Thanks in Advance
well you are trying to import the classes of google maps. for that purpose you need to be sure that google maps app is already installed on the device you are trying to work on. unless it finds google maps application on your device your application wont work.
I've integrated OpenFeint into my Android game, and I'm wondering what i need to add in order to get the OpenFeint player id. I want the id or the username so I can allow users to transfer their game accounts between devices, authenticating with OpenFeint.
Here's my code for using OpenFeint:
OpenFeintSettings ofsettings = new OpenFeintSettings(gameName, gameKey, gameSecret, gameID);
OpenFeint.initialize(this, ofsettings, new OpenFeintDelegate() {});
#Andy hi i also needed the player id when i implemented openFeint in the game i developed
I used the following code
import com.openfeint.api.OpenFeint;
import com.openfeint.api.resource.User;
import android.app.Activity;
import android.os.Bundle;
public class ABC extends Activity {
User u;
String Name;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.score);
u = OpenFeint.getCurrentUser();
// user id received
Name = u.name;
System.out.println("** The User id is ** " + Name );
}
}
If you're not 100% sold on OpenFeint, check out Swarm, which provides a similar set of features (leaderboards, achievements, cloud data save, etc). Swarm's Cloud Data would do exactly what you're looking for, and allow you to save data to a user's account, so no matter what device they're on, they have all of their data available :)
I'm developing an application for the android OS, I'm just starting, but I can't get the GPS on the emulator to work.
I've read on the internet that you need to send a geo fix to the emulator in order to enable the gps locationProvider. I'm both using the DDMS and telnet to try to send it, but logcat never tells me the it recived a new fix, and my apolication still sees the gps as disabled
here's my code
package eu.mauriziopz.gps;
import java.util.Iterator;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
public class ggps extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager l =(LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> li = l.getAllProviders();
for (Iterator<String> iterator = li.iterator(); iterator.hasNext();) {
String string = iterator.next();
Log.d("gps", string);
}
if (l.getLastKnownLocation("gps")==null)
Log.d("gps", "null");
}
}
I've read that the DDMS may not work properly on a non english OS, but telnet should work!
update: the gps is enabled in the settings
Turns out, that since I was developing for Android 1.5 (and not Google API 1.5) the map (and looks like other features) were disabled.
As soon as I changed the target platform, my error disappeared.
btw thanks all
To test if the geofix is working you could use the Google Maps app with "My Location"
Make sure that the gps in enabled in the settings. If still the problem persists, you just go to the application named Navigation in the main menu, run it and exit. Now try your application.
You can enable location controls under emulator control in DDMS perspective by selecting a particular device. without selecting a device it will not work
I supose is fixed yet, but in the code you should use as provider a string returned by the LocationManager service, instead of "gps" as you put in l.getLastKnownLocation("gps").