Adding activity/fragment when user opens any application on android - android

I want to add a particular activity (suppose ask password) when user opens any application. How can i do this in android?
-For example: Suppose user wants to open camera, it should be redirected first to that password screen, user will add password and if it is right he would be able to open camera.

For that you need to declare a background service to capture the event when camera application is opened by the user. Background service will have onReceive() method. In that method, you can get the list of running applications processes and match that if the camera is in the foreground. If the camera is clicked by the user, it will appear in the list of the running applications. Then you intercept that event and show the dialog fragment you intend to show to the user.
You can implement a custom DialogFragment class which has required field for asking user the password.
In the Custom dialog class you declare an interface, something like this:
public interface CustomAddressDialogListener{
public void onDialogPositiveClick(DialogFragment dialog, String enteredPassword);
public void onDialogNegativeClick(DialogFragment dialog);
}
Custom DialogFragment will have its own layout for entering the password and you can set Positive actions and negative actions.
Positive action: user enters password and hits submit.
Negative action: user doesn't want to enter password and clicks cancel.
Your parent activity can implement this interface to capture positive and negative click.
#Override
public void onDialogPositiveClick(DialogFragment dialog, String enteredPassword) {
// Here you can check if the password is correct or wrong and take the action accordingly to open the camera.
}
While handling the negative action you can kill the camera process using kill the camera process: android.os.Process.sendSignal(pid, android.os.Process.SIGNAL_KILL);
Here pid is the process id of the camera application. You can obtain pid when you obtains information of running application processes.
For details you can refer to: http://developer.android.com/reference/android/app/DialogFragment.html

Related

getting back to the home screen after sign up

I have a button
When the user clicks on it, it will be redirects to a registration page
I want user to be redirect to a loading page after clicking on the signup button
And then i want the user to be returns to the main page
And I want a message with the title of "your registration successfully completed" to be display to the user
And after a few seconds, this alert message to be hide
Follow this way to activity switch
Intent intent = new Intent(RegistrationActivity.this, DashBaord.class);
startActivity(intent);
finish();
Suppose your registration activity is RegistrationActivity where you will showing your sign up button and HomeActivity will be the activity which will be shown to user after registering to the app successfully.
You have to maintain some variable so you can know if that variable is registered or not.
boolean isUserRegistered ;
if a user is registered successfully then,
isUserRegistered = true;
and save this varibale using SharedPreferences.For more info about SharedPreferences :
Android Shared preferences example
In the splash activity or some other activity where you will decide which activity to open put this check,
if(isUserRegistered)
//start HomeActvity
else
//start RegistrationActivity
I think your requirement is to create a signup Activity and on completing signup, the user needs to be redirected to Homepage and display a dialog with content "your registration successfully completed".
First of all you need to create a signup Activity. Then when clicking signup, display a custom dialogfragment indicating Loading. Show this dialog when user clicks the signup button. Then make an api request to complete the signup. On response of this api request, dismiss the custom dialogfragment and go to Home activity.
Creating a dialog Fragment : link
On going to Home activity, you need to pass the response of the API request along with intent. In the Home Activity, you can get the value passed along with the intent to HomeScreen to get the message you want to display. You can use an Alert Dialog or custom DialogFragment for displaying the info.
To dismiss the dialog after x seconds, you can use postDelayed(Runnable, long).
Creating a Post Delayed : link

Displaying a AlertDialog on screen irrespective of Activity Screen

I want to show an Dialog Whatever the screen the User is in. Suppose if user opens application and in initial screen if I receive a server message I have to show it in a dialog.Meanwhile there is an option of autologin . So it could move to my next activity.If this is the case that dialog should not be closed.It should show on newly opened activity rather than the previous activity.And other thing is that even though the dialog is shown I should be able to control my buttons on the activity.
Here's what i am doing.
if(Activity1.mcontext!=null){
CommonMethods.showDialog(sliderMessageText,
LoginActivity.mcontext,"activity1");
}
if(Activity2.context!=null){
CommonMethods.showSliderMessageText(sliderMessageText,
Activity2.context,"activity2");
}
if(Activity3.mcontext!=null){
CommonMethods.showSliderMessageText(sliderMessageText,Activity3.context,"activity3");
}
Instead I am displaying the dialog in all the activities.
Thanks in advance.
You may want to consider using an Event driven model such as GreenRobot.
http://greenrobot.org/eventbus/
This would allow you to efficiently handle the scenario you describe.
Alternatively, you can use the LocalBroadcastManager to communicate between different parts of your app.
e.g Send a broadcast message when you want to display a dialog and handle that message in all of your Activities

Detecting 'back key' press from within a leaderboard? (Android)

I have a leaderboard set up in my Android game and it seems to work pretty well.
From the main menu the player can press the 'show scores' button to simply view the leaderboard and when the player's game is over (either through losing all their lives or completing the game), they are given the option of submitting their score. If this is pressed, the score is submitted and the leaderboard displayed to show the new score.
I have my app set up so that it will return to it's current scene (menu, game etc.) if it is temporarily interrupted.
However, I can't work out how to 'know' if the leaderboard is currently being shown to the user. (Well, I do know how to set this, rather I don't know how to 'unset' it when the leaderboard is exited)
I have a variable set up which determines which action my app should take when 'onSignedInSucceded()' is called like so:
#Override
public void onSignInSucceeded() {
//If the flag is set, then display the leaderboard
if (signInAction==SHOW_LEADERBOARD){
displayLeaderBoard();
}
//If the flag is set, then display the submit score
else if (signInAction==SUBMITSCORE){
submitScore();
}
//Otherwise, reset the flag and take no action
else {signInAction=NO_ACTION;}
}
However, what happens is that, lets say the user pressed the 'show scores' button, we set the signInAction to SHOW_LEADERBOARD and then connect. The leaderboard is displayed.
The user then presses the back key and returns to the app. If the app is now interrupted, onSignInSucceded() is called and the leaderboard is displayed again. (but it wasn't being displayed when the app was interrupted therefore this diminishes the user experience).
It would be great it, when the user presses the 'back' key to exit from the leaderboard, I could set the signInAction back to 'No_Action' - how can I do this?
Just for additional information, my app is a single activity and utilises a custom scene manager so in my Activity class, I can do things on back press like:
If (CurrentScene = mainMenu){
//Do Something Here
}
However, the leaderboard is not a custom scene. So I'm not sure how to go about this.
I guess this question could probably best be summed up as 'How can I detect a back key press from Google Play Leaderboard? (when the user exits it back into the app)' - any help would be appreciated.
You should be able to use onActivityResult() to clear signInAction. You'll need to check the request code and maybe the result codes depending on your application.
Some of the possible results will be:
Activity.RESULT_CANCELED (this is the one you're interested in)
GamesActivityResultCodes.RESULT_RECONNECT_REQUIRED
but you'll probably be best off handling them all the same way.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
//just returned from displaying the leaderboard
if(requestCode==REQUEST_LEADERBOARD)
{
signInAction=NO_ACTION;
return;
}
super.onActivityResult(requestCode, resultCode, data);
}

dialog activity control

What I want to do: When the application starts it reads an email address from a database. If the email address is NOT set, it starts another dialog activity for adding the email address.
If the user doesn't add the email, he cannot close that dialog activity, so clicking on close button doesn't close the dialog activity.
The problem that I have is that if the user clicks the back button, the dialog activity closes and the main activity starts without email address set. What I want to do is if the user clicks the back button in this case, to close the application.
I have to mention that I'm using the same dialog activity when I want to edit the email address.
Override onBackPressed, there check whether user has inserted any email address. If not just return without calling super.onBackPressed().
#Override
public void onBackPressed() {
if(yourEmailEditText.getText() == null
|| yourEmailEditText.getText().toString().trim().length() == 0){
return
}
super.onBackPressed();
}
Try Overriding the onBackPressed() method and do nothing into it.
You can set the Dialog as non-cancellable in one of the constructors second parameters:
Dialog(Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener)
Also take into the account what happens if the user by-pass the dialog by any means (any button by hardware, etc). You could simply interprete this as the user wants to exit the application.

Pause your application

I am developing an Android application. I want to quit my application but application should be in running state in background. Whenever I click on application application should start from last quit point. I dont want to login again.
The same thing is happening when I press home button. But I want to implement similar functionality like Home button on my own button event. How should I proceed with that??
Though I have finished all other activities, still I need to login again. When I finish the activity my session ends there. And on next app start with login screen.
Whereas in case of home button click, it keeps my session and on next app start my app check onResume() event where I am checking whether session exist or not. If session is there I can enter directly into my account.
So Anybody have any idea what exactly android does when we press home button.
The best way I know is something like in your scenario where you want to quit:
Save the login credentials on login and clear on log out.
When your app launches check for credentials and if it is there then you should login without asking user.
The above process will not bring your last activity but the activity which you show after login.
To bring back the last activity where you quit, you can use moveTasktoback(true) as it will put your app in the background the same when you press Home key but it's behaviour will not remain constant as android can kill your app whenever it will require the memory.
You mean login to a web app? WebView saves cookie information application-wide regardless of activity.
Otherwise use a static variable or singleton class to hold any session state and on activity start, check the static variable or singleton class for any state else redirect them to the login screen.
You can call moveTaskToBack on your Activity.
create a button where you want to pause(i mean in which activity) the application. And write the code like below. This will pause your application.
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
moveTaskToBack(true);
}
});

Categories

Resources