0

this is my first question I got stucked in an app development..

I have developed an app. This app has a registration activity in the first stage, here the user's name, password and mobile number is collected and saved in the server. after that, the user's registration process is completed.

once this is done, the app will work to give daily horoscope predictions. This means the app will have to notify, or predict, daily. So after once the registrtion process is over the app should continue to run in the background. Also, when the user re-opens the app, it should not display the registration page, but the prediction page. I would also like a button that closes the app, but keeps it running in background.

How would I go about doing this?

Here is the code to my mainActivity,

public class MainActivity extends Activity 
{
    Button btn1,btn2;
    LoginDataBaseAdapter loginDataBaseAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        loginDataBaseAdapter=new LoginDataBaseAdapter(this);
        loginDataBaseAdapter=loginDataBaseAdapter.open();

        btn2 = (Button) findViewById (R.id.button2);
        btn1 = (Button) findViewById (R.id.button1);



         if(loginDataBaseAdapter.getUsercount() >= 50)

            {
             btn2.setVisibility(View.INVISIBLE);                
            }           

            else
            {       
                btn2.setOnClickListener(new View.OnClickListener()      
                {

            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub
                Intent i = new Intent(MainActivity.this,SignUpActivity.class);
                startActivity(i);
            }
        });

    }

         btn1.setOnClickListener(new View.OnClickListener() 
         {

            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub              
                Intent inte = new Intent(MainActivity.this,SignInActivity.class);
                startActivity(inte);

            }
        });
    }








@Override
protected void onDestroy() 
     {
    super.onDestroy();
    // Close The Database
    loginDataBaseAdapter.close();
      }

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    finish();
}
}
Epicblood
  • 1,167
  • 2
  • 10
  • 29
Jocheved
  • 1,125
  • 3
  • 15
  • 32

3 Answers3

1

Create a Service class which runs in the background. Access the horoscope details in the service class in a daily manner, which is up to your requirements. Don't forget to register your service in your Activity.

Also once the registration is successful save username and password in Sharedpreferences and each time application starts you can check if the data is available or not and skip the login page. If it's online you can further validate the username and password with the server.

Since you are writing a Service class you can close the application by clicking a button or something. But once you login makesure you finish() the login activity and start the prediction activity.

SKT
  • 1,821
  • 1
  • 20
  • 32
1

1.Use Shared preference to set a flag once user has done a successful login. NEVER STORE USERNAME OR PASSWORD.

2.Create a Service which will run in Background for getting horoscope details.

3.Next time when user opens app, check the flag value from the Shared Preferences and display appropriate activity

PsyGik
  • 3,535
  • 1
  • 27
  • 44
-1

You will have to inherit from Thread Object. This will allow You to overwrite the run method where You can, for example, run an infinite loop depending on some passed arguments to the inherited thread class. NOT UI threads ( and this is going to become one, as i understood) must not create and/or modify any ui elements. Therefore You must also create an interface, which will be implemented by the inherited thread class. You also will have to pass the thread's callback object as listener to Your thread class in order to invoke a method call in it, where itself You then can marshall thread execution back to ui thread. Basically this means, the thread runs in background and if some conditions occurr, it announces the main thread like an event, where You for Yourself can Do whatever You like. But read You must, first: ( especially, how variable access among n threads should be ensured ):

Here for example:

Threading Example in Android

Community
  • 1
  • 1
icbytes
  • 1,831
  • 1
  • 17
  • 27