I'm creating a sports app, but when I request location the buttons stop functioning.
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.HashMap;
import java.util.Map;
public class HomeActivity extends AppCompatActivity {
Button btnLogout;
Button btnBair;
Button btnVoir;
Button btzFair;
FirebaseAuth mFirebaseAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;
private int MY_PERMISSIONS_REQUEST_READ_CONTACTS ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
btnLogout= findViewById(R.id.Logout);
btnBair= findViewById(R.id.button3);
btnVoir= findViewById(R.id.button4);
btzFair= findViewById(R.id.button5);
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(HomeActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
return;
}
btnLogout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent hom= new Intent(HomeActivity.this, MainActivity.class);
startActivity(hom);
}
});
btzFair.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent fair= new Intent(HomeActivity.this, FutbolActivity.class);
startActivity(fair);
}
});
btnBair.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent hom= new Intent(HomeActivity.this, BasketActivity.class);
startActivity(hom);
}
});
btnVoir.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent hom= new Intent(HomeActivity.this, VoleiActivity.class);
startActivity(hom);
}
});
}
}
You have a return in your
if(ActivityCompat.checkSelfPermission(){...}
I dont think it should be there. Delete it and see what happens
Related
I am getting an error for permission in the Bluetooth when I am using the command to disable.
please tell me why it's happening.
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
CheckBox enable;
private BluetoothAdapter BA;
private Set<BluetoothDevice> pairedDevices;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enable = findViewById(R.id.enable_bt);
BA = BluetoothAdapter.getDefaultAdapter();
if (BA == null) {
Toast.makeText(this, "bluetooth not supported", Toast.LENGTH_SHORT).show();
finish();
}
if (BA.isEnabled()) {
enable.setChecked(true);
}
enable.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
BA.disable();
}
}
}
I am new to android development, I think that something is wrong with the Uri built = Uri.parse("/storage/sdcard0/Download/");, but I don't know what. Any help would be appreciated.
package com.example.ecobinary;
import androidx.appcompat.app.AppCompatActivity;
import androidx.documentfile.provider.DocumentFile;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
private static final int CREATE_FILE = 1;
private void createFile(Uri pickerInitialUri) {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TITLE, "testing.txt");
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri);
startActivityForResult(intent, CREATE_FILE);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button execute_button = findViewById(R.id.execute_button);
execute_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri built = Uri.parse("/storage/sdcard0/Download/");
createFile(built);
}
});
}
}
I use Firebase Sms verification which sends OTP to the provided number. But the problem is it detects the number automatically if the number is within the phone.But i want to enter any number and when i put the number and i can access to the next activity by manually putting the otp. But in my case the the otp is received by any number(i mean outside of the device) but when i enter the code manually in the edittext, the app crashes.
Activity where the number is given and Otp send to the number
package com.example.bohon_final__001;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;
public class LoginActivity extends AppCompatActivity {
private EditText LoginPhone;
private Button LoginConfirmButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
LoginPhone = (EditText) findViewById(R.id.PhoneNumberLogin);
LoginConfirmButton = (Button) findViewById(R.id.Loginbutton);
Button registerbutton=(Button)findViewById(R.id.RegisterButton);
LoginConfirmButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String number=LoginPhone.getText().toString();
if(number.isEmpty() || number.length()<11)
{
LoginPhone.setError("Valied Number Required");
LoginPhone.requestFocus();
}
else {
String PhoneNumber = "+88" + number;
Intent firstintent = new Intent(LoginActivity.this, CodeConfirm.class);
firstintent.putExtra("PhoneNumber", PhoneNumber);
startActivity(firstintent);
}
}
});
registerbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(LoginActivity.this,User_Registration.class));
}
});
}
}
Activity that receives the OTP
package com.example.bohon_final__001;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskExecutors;
import com.google.firebase.FirebaseException;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;
import java.util.concurrent.TimeUnit;
public class CodeConfirm extends AppCompatActivity {
EditText Otpverify;
int s;
private String VerificationCode;
Button confirmbutton,test;
private FirebaseAuth mAuth;
String code,PhoneNumber;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_code_confirm);
Otpverify = (EditText) findViewById(R.id.ConfirmCode);
test=(Button)findViewById(R.id.testbutton);
mAuth= FirebaseAuth.getInstance();
PhoneNumber = getIntent().getStringExtra("PhoneNumber");
SendVerificationCode(PhoneNumber);
confirmbutton=(Button)findViewById(R.id.ConfirmButton);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent nactivity=new Intent(CodeConfirm.this,Current_Location.class);
nactivity.putExtra("Phone",PhoneNumber);
startActivity(nactivity);
}
});
confirmbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
code=Otpverify.getText().toString().trim();
if(code.isEmpty() || code.length()<6)
{
Otpverify.setError("Enter the OTP properly");
Otpverify.requestFocus();
}
Verifycode(code);
}
});
}
private void Verifycode(String code) {
PhoneAuthCredential credential=PhoneAuthProvider.getCredential(VerificationCode,code);
Signin(credential);
}
private void Signin(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
Intent WorkingSwitch=new Intent(CodeConfirm.this,Current_Location.class);
WorkingSwitch.putExtra("Phone",PhoneNumber);
WorkingSwitch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(WorkingSwitch);
}
}
});
}
private void SendVerificationCode(String Number) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
Number,
60,
TimeUnit.SECONDS,
TaskExecutors.MAIN_THREAD,
mCallBack
);
}
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallBack = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
#Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
String code=phoneAuthCredential.getSmsCode();
if(code!=null)
{
Verifycode(code);
}
}
#Override
public void onVerificationFailed(FirebaseException e) {
Toast.makeText(CodeConfirm.this,e.getMessage(),Toast.LENGTH_LONG).show();
}
#Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
VerificationCode=s;
}
};
}
I've seen similar question but the answer is not satishfying.
I have the code for the sound and the next activity. I do not know how to merge the two codes in order to execute them at the same time or in sequence.
package com.example.testmessages;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
ImageButton imageButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Add sound
final MediaPlayer gunSoundMP = MediaPlayer.create(this, R.raw.gunsound);
ImageButton GunSound=(ImageButton)this.findViewById(R.id.imageButton3);
GunSound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
gunSoundMP.start();
}
});
//Image button to execute new Activity
imageButton=(ImageButton) findViewById(R.id.imageButton3);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentLoadMain2Activity = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intentLoadMain2Activity);
gunSoundMP.start();
}
});
}
}
I have 2 screen on first i have Text field and when i pressed the button it takes me to second screen with data from the Text Field but while pressing the back button from screen 2 to screen 1.
I want that the text field(screen 1) will show the previously added data through variables.
////////////////////////////////////Screen 1 code://///////////////////////////
package com.example.abids.savingdataonbackbutton;
import android.content.Intent;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button button;
EditText name;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.buttonNext1);
name=(EditText)findViewById(R.id.editTextName);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String namevalue= name.getText().toString();
savedInstanceState.putString("MyString", "Welcome back to Android");
Intent intent=new Intent(MainActivity.this,Main3Activity.class);
intent.putExtra("Name",namevalue);
startActivity(intent);
}
});
}
}
////////////////**Screen 2 code:**////////////////////////////////////////////
package com.example.abids.savingdataonbackbutton;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;
public class Main3Activity extends AppCompatActivity {
TextView t1;
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
b1=(Button) findViewById(R.id.button);
t1=(TextView)findViewById(R.id.textView2);
t1=(TextView)findViewById(R.id.textView2);
getIntent().getStringExtra("Name");
t1.setText("Name :" +getIntent().getStringExtra("Name"));
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent= new Intent(Main3Activity.this,MainActivity.class);
startActivity(intent);
}
});
}
}
Screen 2
package com.example.abids.savingdataonbackbutton;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;
public class Main3Activity extends AppCompatActivity {
TextView t1;
Button b1;
String valueOfName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
b1=(Button) findViewById(R.id.button);
t1=(TextView)findViewById(R.id.textView2);
t1=(TextView)findViewById(R.id.textView2);
getIntent().getStringExtra("Name");
valueOfName = getIntent().getStringExtra("Name");
t1.setText("Name :" +getIntent().getStringExtra("Name"));
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
screen2Done();
}
});
}
public void screen2Done() {
Intent intent=new Intent();
intent.putExtra("RESULT_STRING", valueOfName);
setResult(RESULT_OK, intent);
finish();
}
#Override
public void onBackPressed() {
screen2Done();
}
On screen 1, capture the value in onActivityResult() method, similarly as in screen 2.