Follow up on Password protect launch of android application - android

Following up on https://stackoverflow.com/a/3448189, what is the best way to actually show the password screen?
My first try was starting a SubActivity with a LockActivity:
// MainActivity.java
public void onResume() {
super.onResume();
ApplicationState state = ((ApplicationState) getApplication());
if ((new Date().getTime() - state.mLastPause) > 5000) {
// Prompt for password if more than 5 seconds since last pause
Intent intent = new Intent(this, LockActivity.class);
startActivityForResult(intent, UNLOCKED);
}
}
However, this causes the MainActivity to be paused again after getting unlocked if the LockActivity is shown longer than 5 seconds.
So, I have some things in mind:
Use Fragments to show the Main screen or the Lock screen inside of MainActivity.
Show a Dialog as Lock screen (not preferred).
Using several if ... else branches to check whether a password has been set and the MainActivity has been paused longer than five seconds.
To give you an example, I would like to achieve the same behavior as in the Dropbox app (using the "Passcode lock" option).
What is the correct way to handle this?
P.S. I'm not sure whether I should have posted this as a question to the original question, thus digging out the old thread. I felt like posting a new question is a cleaner solution.

Since I was the one asking the other question, I might as well tell you how I solved it. I'm using a dialog for prompting for the password (which I do know you do not prefer, but it might help someone else) and makes sure that the only way to dismiss it is by entering the correct password.
MyApplication app = ((MyApplication)getApplication());
if (new Date().getTime() - app.mLastPause > 5000) {
// If more than 5 seconds since last pause, check if password is set and prompt if necessary
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
String password = pref.getString("password", "");
if (password.length() > 0) {
// Prompt for password
MyPasswordDialog dlg = new MyPasswordDialog(this, password);
dlg.setOwnerActivity(this);
dlg.show();
}
}
And in the OnCreate()-method of MyPasswordDialog i make sure that it is not cancelable
protected void onCreate(Bundle savedInstanceState) {
this.setCancelable(false);
// ...and some other initializations
}

Related

Android - Wifimanager handle wifi-connection states

I've got an app which connect itself programatically to a wifi connection. My problem is, I want to handle the case, that the password is wrong. I want to detect that the password is not correct in runtime. To be precise I've got a progressdialog running while the connection is established, so if the password is wrong the progressdialog is just shown all the time and can't be skipped. A further note: I handled a password which is less than 8 characters by using this code:
if(!m_wifiManager.enableNetwork(netId, true)) {
progressDialogConnecting.dismiss();
createInfoMessageDialog(CONST.WIFI_CON_FAILED_TITLE, CONST.WIFI_CON_FAILED_MSG_CONFAILURE);
m_wifiManager.reconnect();
return;
}
If the key for the wifi connection is less than 8 characters, this if-case gets triggered. But if it is longer than 8 characters and wrong I get an endless state of showing the progress dialog.
What I exactly want to ask: how do I handle 1. wrong password 2. connection states (just like Android system showing me the toasts "Connected to Wifi xyz") ? AND is it even possible to handel the first one (wrong password)?
Here is the code, that did not work for handling connection established event (this is just the wifirecevier, I also registered it in the activity):
public class WifiReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)){
if(wrongNetworkConnected)
progressDialogConnecting.dismiss();
}
}
} else {
}
}
}
}
Edit: What I am currently doing, is that I have a Handler which tells me to whom I am connected. That's useful because I can say that after the reconnect() I am reconnected to the old network (current network) and not the new one - so apparently the password could be wrong (or something else), because I could not connect to the new network.
The problem about this method is that first of all it takes too much time and secondly it is not reliable. I can lie and say that if you will get reconnected to your current network it is the fault of a wrong password, but actually it is not 100% sure that you cannot reconnect because of this - it may also have other reasons. So I am still searching for a simple feedback/handle from the suplicant that the password is wrong, just like the android api does in the wifi settings of each android device...
My problem is, I want to handle the case, that the password is wrong.
After some research I found this post which is not marked as answered but it still worked for me very well.
Here is the if-case in which the program jumps (already tested several times by me) if there is an authentication error --> e.g. wrong password:
int supl_error=intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1);
if(supl_error==WifiManager.ERROR_AUTHENTICATING){
// DO SOMETHING
}
NOTE: As seen in the linked post above this if-case should appear in a BroadcastReceiver adding the intent WifiManager.SUPPLICANT_STATE_CHANGED_ACTIONto the receiver-registration in your activity-class.

Welcome page for Android App

I am new to android development, and developing a small app for practice purposes.
I want the following:
When the user installs the app, he/she is greeted with a set of welcome pages, that give details about the app, and then proceeds to the actual app.
But I don't want that set of pages to appear anytime after the first time the app is opened.
So how do I go about implementing that?
PS: This is my first question posted, excuse typos or brevity if any.
Edit: It seems like there is another question of the same context, but I also want to know how to make such an activity that will load only once after the installation.
You can achieve this result in different way, one of them may be to
store on SharedPreference
isFirstLoad = true
after the user read you intro page then
isFirstLoad = false
in your main activity check for first load to redirect user to correct activity
Intent i = ...// normal activity
if(isFirstLoad){
i = ...// intro activity
}
startActivity(i);
you should use SharedPrefrences for store that it is the first time or not.
you can creat a class for store information like this.
public class prefrence
{
SharedPreferences sharedPreferences;
public prefrence(Context context)
{
sharedPreferences = context.getSharedPreferences("myAppData", 0);
}
public boolean isFirstTime()
{
return sharedPreferences.getBoolean("first", true);
}
public void setFirstTime(boolean b)
{
sharedPreferences.edit().putBoolean("first", b).commit();
}
}
and check that it is first time or not like this:
if (new preference(context).isFirstTime()) {
showSplash();
new preference(context).setFirstTime(false);
}

Get Thread sleep duration from App Preferences

I am trying to allow users of my app to change the length of the splash screen. I created an EditTextPreference in my preferences.xml and gave it a default value of 5. The key is "duration". I figured that I could use SharedPreferences and use the getLong method to get the value of the field, and then use it as the parameter for the Thread's sleep method.
Here is my code:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
long dur = prefs.getLong("duration", 5);
final long duration = dur*1000; //convert from seconds to milliseconds
Thread timer = new Thread() {
public void run() {
try{
sleep(duration);
}
catch (InterruptedException e) {
e.printStackTrace();
}
finally {
Intent openMenu = new Intent("com.heh.blah.MENU");
startActivity(openMenu);
}
}
};
timer.start();
This code works perfectly fine as long as the preference isn't changed. However, if I go into the preferences and change the Duration preference (even if I don't change it and just hit "ok" with the default value of 5 in the box OR hit cancel) and close the app, the next time it opens, the screen goes all white for a few seconds, then all black, and then it crashes and I get the "Unfortunately, App has stopped." popup box. Clearing app data allows the app to start up normally again (but with a 5 second splash screen).
UPDATE: Just opening the preferences EVEN WITHOUT CHANGING CLICKING ON OR CHANGING ANYTHING causes it to crash during the next start up.
Any help with this issue would be much appreciated!!!
Thanks,
Max
You would be better off sending a delayed Message to a Handler on the UI thread.

Implementing a login screen with tabs

I am working on an application which requires me to create a log in screen. The way I have planned to do this is have 2 tabs log in, sign up and what I wanted to know is I want to be able to have a user sign up and if they choose remember password next time they load the app it should go straight to the main menu. Although selecting sign out in the main menu should load up the tabs with the log in information when they start the app again.
The question I have is how do I implement the remember me button so next time it skips the log in and how do I implement the sign out so next time the app loads the log in screen.
Thank you in advance! (",)
Sri
Throw the remembered answer into the SharedPreferences and read it when your activity starts and process it accordingly.
I would probably use Internal Storage to store the username/password for the remember me button.
When the app loads, first check if the user/pass is already saved. If so then direct to the tabs, if not then direct to the log in screen.
First Log In :
SharedPreferences sSession = PreferenceManager.getDefaultSharedPreferences(context);
Editor ePrefrences = sSession.edit();
ePrefrences.putString("id", "user id");
ePrefrences.putString("password", "user password");
ePrefrences.putBoolean("successfullylogin", true);
ePrefrences.commit();
Second Log In :
SharedPreferences sSession = PreferenceManager.getDefaultSharedPreferences(this);
if (sSession .getBoolean("successfullylogin", false)) {
//get user name and password
sUser = sSession.getString("id", "");
sPassword = sSession.getString("password", "");
//start activity
}
else {
//prepare for normal login
}

how to set themes continuously in android

My problem is make a function to set up themes in my application every 00:00 AM if there are new themes. As I know, to do this problem we must use a loop.
Here is my code:
private void updateThemes() {
Thread time = new Thread() {
public void run() {
int time = 0;
while(time > 86400000) {
//invoke method or start new activity
}
}
};
}
Please help me - Thanks.
Running a thread and waiting for a full day is not going to work. What if the phone is shutdown? What if the user switches to another app and your app is closed by Android because it needed the resources? Besides, it's not very battery friendly either.
You'd better use the Android AlarmManager to set the times at which you would like to check for updates. Also specify a BroadcastReceiver in your app that will receive and process the alarms. There's an example application that does this here or check this post for more info.

Categories

Resources