Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- download the google-services.json file and add it to the app folder
- build.gradle <project>
- ============================
- add to dependencies
- classpath 'com.google.gms:google-services:3.0.0'
- build.gradle <app>
- ====================
- add to dependencies
- compile 'com.google.firebase:firebase-core:9.0.2'
- compile 'com.google.firebase:firebase-database:9.0.2'
- add to the bottom
- apply plugin: 'com.google.gms.google-services'
- activity_main.xml
- ======================
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Enter message....."
- android:id="@+id/helloMsg"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Send message..."
- android:onClick="sendMsg"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Read Message...."
- android:onClick="readMsg"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Enter User Name....."
- android:id="@+id/uName"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/uPass"
- android:hint="Enter User Pass"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Create User"
- android:onClick="createUser"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Get all"
- android:onClick="displayUsers"/>
- </LinearLayout>
- Users.java (for holding the structure of Json and db)
- ===========================================================
- import android.content.Context;
- import android.net.wifi.WifiInfo;
- import android.net.wifi.WifiManager;
- import android.widget.Toast;
- import com.google.firebase.database.DataSnapshot;
- import com.google.firebase.database.DatabaseError;
- import com.google.firebase.database.DatabaseReference;
- import com.google.firebase.database.FirebaseDatabase;
- import com.google.firebase.database.ValueEventListener;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Created by app0811 on 05/06/2016.
- */
- public class Users {
- public String userName; //hold userName
- public String userPass; //hold userPass
- public String userId; //hold userId
- public Users(){} //empty constructor, must have
- public Users(String userName, String userPass, String uid)
- {
- this.userName=userName;
- this.userPass=userPass;
- this.userId=uid; //represent the UUID
- }
- public void saveUser()
- {
- //create an instance of User class
- Users user=new Users(userName,userPass,userId,context);
- //creating a connection to fire base
- FirebaseDatabase database=FirebaseDatabase.getInstance();
- //creating a reference to Users object
- DatabaseReference myRef=database.getReference("Users");
- //saving the user under the UUID
- myRef.child(userId).setValue(user);
- }
- }
- MainActivity.java
- ==================================
- public class MainActivity extends AppCompatActivity {
- EditText myMsg;
- EditText uName;
- EditText uPass;
- Context context;
- final List<Users> myData=new ArrayList<>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- myMsg=(EditText)findViewById(R.id.helloMsg);
- uName=(EditText)findViewById(R.id.uName);
- uPass=(EditText)findViewById(R.id.uPass);
- context=this;
- }
- private String getMac()
- {
- //seeting WifiManager
- WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
- //getting the info for the manager
- WifiInfo info = manager.getConnectionInfo();
- //getting mac address , physical address of the wifi card
- String address = info.getMacAddress();
- //returning the address
- return address;
- }
- private String getUUID()
- {
- //create a unique UUID
- UUID idOne = UUID.randomUUID();
- //returning the UUID
- return idOne.toString();
- }
- public void sendMsg(View v) // Write a message to the database
- {
- //creating an instance to the database
- FirebaseDatabase database = FirebaseDatabase.getInstance();
- //creating a reference to message object
- DatabaseReference myRef = database.getReference("message");
- //getting the message from the activity
- String myString = myMsg.getText().toString();
- //setting the value
- myRef.setValue(myString);
- }
- public void readMsg(View v)
- {
- //creating a connection to the database
- FirebaseDatabase database = FirebaseDatabase.getInstance();
- //creating a reference to the database
- DatabaseReference myRef = database.getReference("message");
- //creating an event listener to the reference
- myRef.addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- // This method is called once with the initial value and again
- // whenever data at this location is updated.
- String value = dataSnapshot.getValue(String.class);
- Toast.makeText(MainActivity.this, "Read:"+value, Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onCancelled(DatabaseError error) {
- // Failed to read value
- Toast.makeText(MainActivity.this, "Failed to read value.", Toast.LENGTH_SHORT).show();
- }
- });
- }
- public void createUser(View v)
- {
- //getting the info from the activity
- final String userName=uName.getText().toString();
- final String userPass=uPass.getText().toString();
- //creating new instance of the project
- Users user=new Users(userName,userPass,getUUID(),this);
- //calling inside method from the class to save the data
- user.saveUser();
- }
- public void displayUsers(View v)
- {
- //creating an instance to the database
- FirebaseDatabase database = FirebaseDatabase.getInstance();
- //creating a reference to the database
- final DatabaseReference myRef = database.getReference("Users");
- //adding an event listener to the Event
- myRef.addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- //method for data change
- public void onDataChange(DataSnapshot dataSnapshot) {
- //moving on entire data that we received from the database
- for (DataSnapshot item:dataSnapshot.getChildren()) {
- //get a single value, and push it to User instance by the Users class build
- Users recivedUser = item.getValue(Users.class);
- //add the value to our list
- myData.add(recivedUser);
- }
- //display the message to the screen
- Toast.makeText(context, "we got:"+myData.size(), Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- //handle in error case
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement