Endless loop in onStart() - java

Why am I getting an endless loop between the main activity and login activity?
#Override
protected void onStart() {
super.onStart();
if (mAuthListener == null) {
//removeAuthSateListner is used in onStart function just for checking purposes, it helps in logging you out.
startActivity(new Intent(this, LoginActivity.class));
} else
startActivity(new Intent(this, EditProfileActivity.class));
}
#Override
protected void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
Is this code correct?

Related

Screenshot detection library does not detect screenshots

I want to detect screenshots inside a specific activity inside my app. I decided to use this ScreenShot Detector library but unfortunately this does not detect any screenshots for some reason.
I added some code to my activity to check if all the required permissions are grandent and it still doesn't work.
My Activity:
public class UserProfileActivity extends AppCompatActivity implements ScreenshotDetectionDelegate.ScreenshotDetectionListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
ActivityResultLauncher<String> requestPermissionLauncher =
registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
if (isGranted) {
Toast.makeText(this, "Permission ok", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission req", Toast.LENGTH_SHORT).show();
// Explain to the user that the feature is unavailable because the
// features requires a permission that the user has denied. At the
// same time, respect the user's decision. Don't link to system
// settings in an effort to convince the user to change their
// decision.
}
});
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
// You can use the API that requires the permission.
requestPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE);
}
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
CustomIntent.customType(UserProfileActivity.this, "right-to-left");
}
#Override
protected void onStart() {
super.onStart();
screenshotDetectionDelegate.startScreenshotDetection();
}
#Override
protected void onStop() {
super.onStop();
screenshotDetectionDelegate.stopScreenshotDetection();
}
#Override
protected void onResume() {
super.onResume();
screenshotDetectionDelegate.startScreenshotDetection();
}
#Override
public void onScreenCaptured(#NonNull String s) {
sendNotificationToUser("screenshot");
Toast.makeText(UserProfileActivity.this, "Profile Screenshotted", Toast.LENGTH_SHORT).show();
}
#Override
public void onScreenCapturedWithDeniedPermission() {
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
}
}

Async task inside a Service, when is the stopSelf method called? [duplicate]

Here I tried simple service program. Start service works fine and generates Toast but stop service does not. The code of this simple service is as below:
public class MailService extends Service {
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onCreate(){
super.onCreate();
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
}
public void onDestroyed(){
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
super.onDestroy();
}
}
The code of the Activity from where this Service is called is as below:
public class ServiceTest extends Activity{
private Button start,stop;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.service_test);
start=(Button)findViewById(R.id.btnStart);
stop=(Button)findViewById(R.id.btnStop);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startService(new Intent(ServiceTest.this,MailService.class));
}
});
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopService(new Intent(ServiceTest.this,MailService.class));
}
});
}
}
Help me to stop service with that stop button which generates toast in the onDestroy() method. I have already seen many posts regarding stop service problem here, but not satisfactory so posting new question. Hope for satisfactory answer.
onDestroyed()
is wrong name for
onDestroy()
Did you make a mistake only in this question or in your code too?
This code works for me: check this link
This is my code when i stop and start service in activity
case R.id.buttonStart:
Log.d(TAG, "onClick: starting srvice");
startService(new Intent(this, MyService.class));
break;
case R.id.buttonStop:
Log.d(TAG, "onClick: stopping srvice");
stopService(new Intent(this, MyService.class));
break;
}
}
}
And in service class:
#Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
player = MediaPlayer.create(this, R.raw.braincandy);
player.setLooping(false); // Set looping
}
#Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
player.stop();
}
HAPPY CODING!
To stop the service we must use the method stopService():
Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class);
//startService(myService);
stopService(myService);
then the method onDestroy() in the service is called:
#Override
public void onDestroy() {
Log.i(TAG, "onCreate() , service stopped...");
}
Here is a complete example including how to stop the service.

Camera does not finish with clicking onBackPressed

I want to call camera intent in on Start Method and it works fine, but when i click on back button it does not go back from camera because Uri is empty. I want to go to previous Activity with clicking back button.
#Override
protected void onStart() {
super.onStart();
if (uri==null){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
}
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
Create a helper class with a static boolean variable like below,
public class HelperUtil
{
public static boolean isCameraActivityBackPressed;
}
And change your code little bit,
#Override
protected void onStart()
{
super.onStart();
if (uri == null)
{
if (HelperUtil.isCameraActivityBackPressed)
{
// this block will execute when ActivityCamera back is pressed and uri is null
startActivity(new Intent(this, ReplaceWithYourActivity.class));
} else
{
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
}
}
}
#Override
public void onBackPressed()
{
super.onBackPressed();
HelperUtil.isCameraActivityBackPressed = true;
Toast.makeText(ActivityCamera.this, "onbackpressed", Toast.LENGTH_LONG).show();
startActivity(new Intent(this, ActivityCamera.class));
finish();
}

Android SDK how to OnClickListener

guys I need help with this code :
public View.OnClickListener btnGetLastLocationOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mGoogleApiClient != null) {
if (mGoogleApiClient.isConnected()) {
getMyLocation();
} else {
Toast.makeText(AddEditActivity.this,
"!mGoogleApiClient.isConnected()", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(AddEditActivity.this,
"mGoogleApiClient == null", Toast.LENGTH_LONG).show();
}
}
};
so what I want to do is when I click on btnGetLocation this code should work, but instead the code works automatically when I start the activity so how can I make works only when I click on the button ?
I hope you guys understand the problem, I'm new Android developer so I'm still learning forgive me for my stupid question.
thank you
*** Update :
here is the whole code :
TextView txtmylink;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
Button getlastlocation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtmylink = (TextView) findViewById(R.id.mylink);
getlastlocation = (Button) findViewById(R.id.getlastlocation);
getlastlocation.setOnClickListener(getlastlocationOnClickListener);
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}
View.OnClickListener getlastlocationOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mGoogleApiClient != null) {
if (mGoogleApiClient.isConnected()) {
getMyLocation();
} else {
Toast.makeText(MainActivity.this,
"!mGoogleApiClient.isConnected()", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(MainActivity.this,
"mGoogleApiClient == null", Toast.LENGTH_LONG).show();
}
}
};
private void getMyLocation() {
try {
/* code should explicitly check to see if permission is available
(with 'checkPermission') or explicitly handle a potential 'SecurityException'
*/
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
txtmylink.setText(String.valueOf("http://maps.google.com/maps?q=") +
String.valueOf(mLastLocation.getLatitude()) +
String.valueOf(",")+ String.valueOf(mLastLocation.getLongitude()));
} else {
Toast.makeText(MainActivity.this,
"mLastLocation == null",
Toast.LENGTH_LONG).show();
}
} catch (SecurityException e) {
Toast.makeText(MainActivity.this,
"SecurityException:\n" + e.toString(),
Toast.LENGTH_LONG).show();
}
}
#Override
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
#Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
#Override
public void onConnected(#Nullable Bundle bundle) {
getMyLocation();
}
#Override
public void onConnectionSuspended(int i) {
Toast.makeText(MainActivity.this,
"onConnectionSuspended: " + String.valueOf(i),
Toast.LENGTH_LONG).show();
}
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
Toast.makeText(MainActivity.this,
"onConnectionFailed: \n" + connectionResult.toString(),
Toast.LENGTH_LONG).show();
}
}
That's because you haven't associated the View.onClickListener object you've written to anything yet. Thus when the activity is started the listener's code gets executed. When you want a specific view say 'A' to respond to a click, you associate a View.onClickListener to it. So in your case add the following.
yourbutton.setOnClickListener(btnGetLastLocationOnClickListener)
Make sure you've bound the yourbutton to the Button view.
try this :
public void onClick(View v) {
if (view.getId() == R.id.btnGetLocation)
if (mGoogleApiClient != null) {
if (mGoogleApiClient.isConnected()) {
getMyLocation();
} else {
Toast.makeText(AddEditActivity.this,
"!mGoogleApiClient.isConnected()", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(AddEditActivity.this,
"mGoogleApiClient == null", Toast.LENGTH_LONG).show();
}
}
If still not works write your code like this :
your activity must implement OnClickListener and
btnGetLocation.setOnClickListener(this)
and you will have the onClick like this
public void onClick(View v) {
if (view.getId() == R.id.btnGetLocation)
if (mGoogleApiClient != null) {
if (mGoogleApiClient.isConnected()) {
getMyLocation();
} else {
Toast.makeText(AddEditActivity.this,
"!mGoogleApiClient.isConnected()", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(AddEditActivity.this,
"mGoogleApiClient == null", Toast.LENGTH_LONG).show();
}
}
You can accomplish it using the following code
btnGetLocation = (Button) findViewById(R.id.btnGetLocation); // or whatever you called the button
btnGetLocation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mGoogleApiClient != null) {
if (mGoogleApiClient.isConnected()) {
getMyLocation();
} else {
Toast.makeText(AddEditActivity.this,
"!mGoogleApiClient.isConnected()", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(AddEditActivity.this,
"mGoogleApiClient == null", Toast.LENGTH_LONG).show();
}
}
}
Note that you can add this code in the onCreate() method to make it work

cannot run android text to speech

My code is as follows
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
runBtn = (Button)findViewById(R.id.button_run_demo);
ttobj = new TextToSpeech(getApplicationContext(),
new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
ttobj.setLanguage(Locale.UK);
}
}
});
runBtn.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
}
public void onPause() {
if (ttobj != null) {
ttobj.stop();
ttobj.shutdown();
}
super.onPause();
}
public void speakText(String text) {
String toSpeak = text;
Toast.makeText(getApplicationContext(), toSpeak,
Toast.LENGTH_SHORT).show();
ttobj.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
//do my stuff
speakText("Hello");
}
}
Although I am able to do my stuff I am not able to run speech.
Am I missing something ??
Thanks All
When onActivityResult() is called the TextToSpeech is already shutdown since you shut it down in onPause(). You have to either set a flag to speak "Hello" when TextToSpeech is reinitialize or put your code in onPause() in onDestroy().

Categories