public boolean isUserAGoat() can be used to determine whether the user making this call is goat or not. This method always returns a false value as this is done to protect goat privacy. In other words, it can be said that it is used to determine whether the user making this call is subject to teleportation.
What are we going to build in this article?
We will be building a simple application in which we will be implementing this function. We will be using it to modify the text to prove that it always returns a false value. After clicking on the text “Click”, it will update the text (A sample video is given below to get an idea about what we are going to create in this article)
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Project just refer to this article on How to Create New Project in Android Studio. The code has been given in Java.
Step 2: Working with the activity_main.xml File
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout android:layout_width = "match_parent" android:gravity = "center" android:layout_height = "match_parent" tools:context = ".MainActivity" android:orientation = "vertical" > < TextView android:id = "@+id/actionEvent" android:textSize = "40sp" android:layout_marginTop = "30dp" android:layout_width = "wrap_content" android:layout_height = "match_parent" /> </ LinearLayout > |
Step 3: Working with the MainActivity File
Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail.
Java
package com.example.gfgisuseragoat; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.view.View; import android.widget.TextView; import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { TextView actionEvent; @RequiresApi (api = Build.VERSION_CODES.LOLLIPOP) @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); actionEvent = findViewById(R.id.actionEvent); actionEvent.setText( "Click" ); // Setting on click listener on the text Click actionEvent.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { // Implementing the isUserAGoat() function in order to change the text // '!' is used to invert the false returned by the function if (!isUserAGoat()) { singleToneClass singleToneClass = com.example.gfgisuseragoat.singleToneClass.getInstance(); // Changing the text when this function is called singleToneClass.setData( "geeksforgeeks.org" ); actionEvent.setText(singleToneClass.getData()); } } }); } public boolean isUserAGoat() { PackageManager pm = getPackageManager(); boolean app_installed; // Exception-handling part try { pm.getPackageInfo( "com.example.myapplication" ,PackageManager.GET_ACTIVITIES); app_installed = true ; } catch (PackageManager.NameNotFoundException e) { app_installed = false ; } return app_installed; } } |
Step 4: Creating a new SingletoneClass file
Create a new Java class and name it SingletoneClass and add the following code to it.
Java
package com.example.gfgisuseragoat; public class singleToneClass { String s; private static final singleToneClass ourInstance = new singleToneClass(); public static singleToneClass getInstance() { return ourInstance; } private singleToneClass() { } public void setData(String s) { this .s = s; } public String getData() { return s; } } |
Output: