I have Three pages Splash_Activity , Login_Activity, Activity_One
The activity flow is Splash_Activity -> Login_Activity -> Activity_One .In that case i check the .db file is exist or not in my Splash_Activity which is always start activity of application.
I put the condition for if .db is exist then go to Activity_One
and if not then go to Login_Activity .
But the problem when i delete .db file on button click the .db file is deleted properly and display the login page.
When i exist the app without login and I again start the app first two three times run and check the .db file is not available then go to login page but after that it goes to Activity_One. Why this happen
This splash screen
public class Splash_Screen extends RootActivity
{
MyDbHelper dbhelper ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash_page);
Thread loading = new Thread() {
public void run()
{
try
{
File database=getApplicationContext().getDatabasePath("ClassNKK.db");
if (!database.exists()) {
// Database does not exist so copy it from assets here
Log.i("Database", "Not Found");
try
{
sleep(2000);
Intent main = new Intent(Splash_Screen.this, Login_Screen.class);
startActivity(main);
Log.e("DB "," is null !!!");
}
catch (Exception e) {
e.printStackTrace();
}
}
else {
Log.i("Database", "Found");
try {
sleep(2000);
Intent main = new Intent(Splash_Screen.this, AllPosts_Page.class);
startActivity(main);
Log.e("DB ", " is null !!!");
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (SQLiteException e) {
e.printStackTrace();
}
}
};
loading.start();
}
}
And this logout button on Activity_One page
imgBtn_LogOut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
File database = getApplicationContext().getDatabasePath(dbhelper.DATABASE_NAME);
if (!database.exists()) {
Log.e("Database", "Not Found");
} else {
Log.e("Database", "Found");
ctx.deleteDatabase(dbhelper.DATABASE_NAME);
Log.e("Database", " Deleted Completeley !!!");
File dir = new File(Environment.getExternalStorageDirectory() + "classnkk_images");
DeleteRecursive(dir);
Intent i = new Intent(Filter_Screen.this, Login_Screen.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
}
}
});
I think the problem is when you start application after delete db, it will automatically create new database without data.
So please check again database had created or not ?
I suggest you to save parameter in share preference and check it when in splash screen. It better!
If you want to store login credential you can use sharedPreference instead of sqlite. I think it will be most suitable method for this.
http://androidforbegineers.blogspot.in/2013/08/shared-preference-using-registration.html
this blog will help you.:)
Related
This is my first android app so I am quite new to it. I am able to read from a property file but facing problem on writing back to it on the save button in the same activity.
Here is my code
public class ConfigurationActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_configuration);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final EditText abc = (EditText) findViewById(R.id.abc);
...
...
...
//Reading into a activity and displaying it into the user
try {
Properties props = new Properties();;
InputStream inputStream = getApplicationContext().getAssets().open("app.properties");
props.load(inputStream);
abc.setText(props.getProperty("abc"));
...
...
...
inputStream.close();
} catch (FileNotFoundException ex) {
Log.e("Exception", "File donot exists");
} catch (IOException ex) {
Log.e("Exception","IO error");
}
//On the save button I am trying to write back to properties file
Button save = (Button) findViewById(R.id.SaveButton);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
Properties props = new Properties();
props.setProperty("abc", abc.getText().toString());
...
...
...
props.store(getApplicationContext().getAssets().openFd("app.properties").createOutputStream(), null);
Intent intent = new Intent(ConfigurationActivity.this, MainActivity.class);
startActivity(intent);
finish();
//inputStream.close();
} catch (FileNotFoundException ex) {
Log.e("Exception", "File donot exists");
} catch (IOException ex) {
Log.e("Exception","IO error");
}
}
});
}
}
Now I am getting the data from the property file and displaying it to user but on the save button its giving FileNotFoundException. I looked for other codes on stackoverflow but I wasn't able to solve my problem
Can someone guide my in this ?
Thanks in advance
You cannot modify assets or resources at runtime. They are read-only.
I have a problem that I am not able to check the value of AsyncTask result in order to start new activity if the result is desirable.
Here is my onPostExecute method:
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(LoginActivity.this, s, Toast.LENGTH_SHORT).show();
}
It toasts string value from php file and it has two possible values, "You are successfully logged in" or "Incorrect email or password". Everything is working fine until I want to check what the toasted message is, because if login is successfull I need to start a new activity.
This is how I tried to do that:
public void onClick(View v) {
AttemptLogin login = new AttemptLogin();
try {
String res = login.execute("http://10.0.2.2/loginApp.php").get();
if (res.equals("You are successfully logged in"))
startActivity(new Intent(LoginActivity.this,ConnectActivity.class));
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
I am totally confused because when I toast res, I get the desirable message, so the problem is with this line,if (res.equals("You are successfully logged in")). The app behaves like this line doesn't exist at all.
I also tried with if(AsyncTask.getStatus() == FINISHED) and then to check AsyncTask result but it didn't help.
I really don't have idea what is going on, can anyone please help me with this?
AsyncTask has OnPostExecute and OnPreExecute methods.
Both of them can call items, variables and methods as if they were normal methods.
So in your onPostExecute you can easily check the result and start your activity using this:
#Override
protected void onPostExecute(String s) {
super.onPostExecute();
try {
if (s.equals("You are successfully logged in")){
Intent i = new Intent(LoginActivity.this,ConnectActivity.class);
startActivity(i);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
Toast.makeText(LoginActivity.this, s, Toast.LENGTH_SHORT).show();
}
The first time my app runs it creates a database in which it loads 6,000 rows from a file in /res/raw. I can't do this asynchronously as the app depends on it entirely. It runs rapidly on my phone - a Moto X - but it's really slow in all my emulators and I'm concerned it could be a bit slower on slower devices thus making the user stare at a blank screen for a few seconds before the app does anything.
Is there a way to put a progress bar while running the overrided SQLiteOpenHelper's onCreate() methood and have it update the progress bar with how far along it is, with a message saying something like "Initializing data for first use!"?
I solved this problem by starting an AsyncTask in onCreate and then only loading the layout at the end of the 'AsyncTask` (or if the data had previously been loaded). It works beautifully as a loading screen. I followed this tutorial http://www.41post.com/4588/programming/android-coding-a-loading-screen-part-1 (which explains the details more) then changed it a bit for my needs (such as loading a raw resource).
I should say that although it does it asynchronously because the main layout hasn't loaded the user has to wait for the loading to complete before he or she can continue, so hopefully that means it doing it asynchronously won't be a problem for you with the app depending on the database.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedPref = getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
dataAddedToDB = (sharedPref.getBoolean(PXS_RXS_UPDATE, false));
if (!dataAddedToDB) {
new LoadViewTask(this).execute();
} else {
setContentView(R.layout.activity_main);
}
}
In the AsyncTask it loads the database showing how far it has got and showing your message and then only goes on to show the layout at the end. (BTW, it is helpful to lock the screen orientation while doing this to stop it messing it up).
EDIT: publishProgress(counter); passes the value of where the task has got to to onProgressUpdate().
private class LoadViewTask extends AsyncTask<Void, Integer, Void> {
private Context context;
public LoadViewTask(Context context) {
this.context = context.getApplicationContext();
}
#Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setTitle("Loading...");
progressDialog.setMessage("Initializing data for first use!");
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(false);
// this counts how many line to be added to the database so it can later tell how far it has got.
final Resources resources2 = context.getResources();
InputStream inputStream2 = resources2.openRawResource(R.raw.rawherbaldata);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream2));
int lineCount = 0;
try {
String line;
while ((line = reader.readLine()) != null) {
lineCount++;
}
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
progressDialog.setMax(lineCount);
progressDialog.setProgress(0);
progressDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
HerbalDatabaseOpenHelper mHerbalDbHelper = new HerbalDatabaseOpenHelper(MainActivity.this);
SQLiteDatabase db = mHerbalDbHelper.getWritableDatabase();
int counter = 0;
final Resources resources2 = context.getResources();
InputStream inputStream2 = resources2.openRawResource(R.raw.rawherbaldata);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream2));
db.beginTransaction();
try {
int lineNumber = 1;
String line;
while ((line = reader.readLine()) != null) {
// CODE FOR ENTERING LINE INTO DATABASE
// EDIT: the following keeps the task updated on where it has got to, passing the count to onProgressUpdate()
counter++;
publishProgress(counter);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
db.setTransactionSuccessful();
db.endTransaction();
db.close();
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
progressDialog.setProgress(values[0]);
}
#Override
protected void onPostExecute(Void result) {
progressDialog.dismiss();
SharedPreferences sharedPref = getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
pxsRxsUpdate = true;
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean(PXS_RXS_UPDATE, pxsRxsUpdate);
editor.commit();
// initialize the View
setContentView(R.layout.activity_main);
}
}
You could use another intermediate activity which would show the progress dialog and then send you back to the main activity when done.
First you'll need a static method that a boolean if the DB has already been create.
Then inside of your activity's onCreate call the middleman if necessary:
DbHelper mDbHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!DbHelper.isDbCreated()) {
Intent intent = new Intent(this, DbActivity.class);
startActivity(intent);
finish();
return;
}
// Do normal stuff like instantiating the helper and so on
mDbHelper = new DbHelper();
...
}
Then inside of this "middleman" activity show the ProgressDialog and create the database.
Once you're done, hide the dialog and go back to your main activity:
mProgress.dismiss();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
return;
If your static method isDbCreated() is created properly, you won't reveal the MainActivity's content until the database is created.
EDIT:
Here's the method I use to check for the database. Perhaps it will help you.
public boolean isDbCreated() {
String sDatabasePath = context.getDatabasePath(DB_NAME).getPath();
SQLiteDatabase tmpDb = null;
if (mContext.getDatabasePath(DB_NAME).exists()) {
try {
tmpDb = SQLiteDatabase.openDatabase(sDatabasePath, null,
SQLiteDatabase.OPEN_READONLY);
tmpDb.close();
} catch (SQLiteException e) {
e.printStackTrace();
}
} else {
Log.e(TAG, "DB file doesn't exist.");
// If the parent dir doesn't exist, create it
File parentDir = new File(mContext.getDatabasePath(DB_NAME).getParent());
if (!parentDir.exists()) {
if (parentDir.mkdirs()) {
Log.d(TAG, "Successfully created the parent dir:" + parentDir.getName());
} else {
Log.e(TAG, "Failed to create the parent dir:" + parentDir.getName());
}
}
}
return (tmpDb != null);
}
i am using check box for saving data in database .if it is checked then app redirecting to other screen by saving data after that if i click on device back button then app showing it is not checked.how can i fix this issue?
here i am placing code
public void joinLisn(){
String shareProfileType2=Constants.PROFILE_SHARE_ALL;
String accessToken = null;
DatabaseHelper helper = new DatabaseHelper(getApplicationContext());
DatabaseUtility dao = new DatabaseUtility(helper);
try {
accessToken = dao.getAccessToken();
} catch (Exception e1) {
handler.sendEmptyMessage(1);
return;
}
if(accessToken == null || accessToken.length() == 0){
handler.sendEmptyMessage(1);
return;
}
Map<String , String> params = new HashMap<String,String>();
params.put(Constants.ACCESS_TOKEN_PARAM, accessToken);
params.put(Constants.LISN_ID_PARAM, id);
params.put(Constants.PROFILE_TYPE_PARAM,shareProfileType2);
Status status = null;
try {
status = Utils.joinLisn(params, this);
} catch (NullPointerException e) {
handler.sendEmptyMessage(12);
return;
} catch (JSONException e) {
handler.sendEmptyMessage(11);
return;
}
if(status == null){
handler.sendEmptyMessage(1);
} else if(status.getStatus().equalsIgnoreCase(Constants.SUCCESS)){
try {
Intent lisnDetailIntent = new Intent(this, LisnDetailTabView.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle bundleObj = new Bundle();
bundleObj.putString("id", id);
bundleObj.putString("RSVP","In");
lisnDetailIntent.putExtras(bundleObj);
startActivityForResult(lisnDetailIntent,0);
overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
handler.sendEmptyMessage(8);
} catch(Exception ex) {}
} else{
handler.sendEmptyMessage(2);
return;
}
}
Override the onbackpressed to trigger your function to save the states.
public void onBackPressed() {
//Do your db saving here
super.onBackPressed();
}
If your app is redirecting to other app, your app will be put to background. Make use of onSaveInstanceState and onRestoreInstanceState to save and restore the checkbox state.
I am trying to implement an auto logout functionality. If the application is not used for a period of 1 hour i would want to automatically bring the user to the login screen. When the phone gets locked the thread which is monitoring if the application is in use does not seem to continue. My code is as below:
/**
* This Deamon checks if the application is idle and
*/
private class LogoutDeamon extends Thread {
public void run() {
while (!logoutDeamon) {
try {
System.out.println("Logout Counter:" + logoutConter);
if (logoutConter <= 0) {
logoutDeamon = true;
ApplicationManager.getInstance().setLoggedOut(true);
ApplicationManager.getInstance().Log(Level.INFO, "Auto Log out");
logout();
} else {
decreamentCounter();
}
sleep(60000 * 1);
} catch (ParserException ex) {
Log(Level.ERROR, " Par. Ex. in Logout-Deamon:" + ex.getMessage());
logout();
} catch (ServerException ex) {
Log(Level.ERROR, " Ser. Ex. in Logout-Deamon:" + ex.getErrorMessage());
logout();
} catch (InterruptedException ie) {
Log(Level.ERROR, "Int. Ex. in Logout-Deamon:" + ie.getMessage());
} catch (Exception ex) {
Log(Level.ERROR, "Erro in Logout-Deamon:" + ex.getMessage());
logout();
}
}
}
private void logout(){
Intent broadcastIntent = new Intent();
broadcastIntent
.setAction("com.package.ACTION_LOGOUT");
applicationContext.sendBroadcast(broadcastIntent);
Intent loginIntent = new Intent(applicationContext,
Login.class);
loginIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
applicationContext.startActivity(loginIntent);
}
}
Am i missing out on something? Can someone kindly help me with this. Thanks in advance.
I don't think that you need a thread. Register the time in onPause. In onResume test if 1 hour has passed. If not, reset the timer.