Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8559239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:59:40+00:00 2026-06-11T15:59:40+00:00

Possible Duplicate: how to run application in background in android? i am doing project

  • 0

Possible Duplicate:
how to run application in background in android?

i am doing project using location manager if my location latitude and longitude equal to the same which is in database mobile profile should go to silent mode.so i need to keep on update my location using location manager.i did it .but if i close my app it’s not working.
but my app should be working even though i close my app.i tried with async task its working when i close the app but after i switch to loud mode manually it’s not changing to silent.i should also get notified when my app runs n background..please help.tks in advance

here is my code

public class ShowLocationActivity extends Activity implements LocationListener {
      private TextView latituteField;
      private TextView longitudeField;
      private LocationManager locationManager;
      private String provider;
      double lat,lng;
      Location location;
      AudioManager mobilemode ;
        private Boolean flag = false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.location);
        latituteField = (TextView) findViewById(R.id.TextView02);
        longitudeField = (TextView) findViewById(R.id.TextView04);
        mobilemode = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
        // Get the location manager
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // Define the criteria how to select the locatioin provider -> use
        // default
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        location = locationManager.getLastKnownLocation(provider);

        // Initialize the location fields

        flag = displayGpsStatus();
        if (flag) {

        if (location != null) {
          System.out.println("Provider " + provider + " has been selected.");
          onLocationChanged(location);


          if((lat==12.905478)&&(lng==80.093358))
        {
            mobilemode.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

            Toast.makeText(getBaseContext(),"VIBRATE profile activated ",Toast.LENGTH_SHORT).show();



        }
        else if ((lat==12.90080625)&&(lng==80.09210655))
        {
            mobilemode.setRingerMode(AudioManager.RINGER_MODE_SILENT);





            Toast.makeText(getBaseContext(),"SILENT profile activated !",Toast.LENGTH_SHORT).show();
            //notification.setLatestEventInfo(ShowLocationActivity.this, "changed to silent mode", "because u r in office", pendingIntent);
              //  notificationManager.notify(i, notification);
        }
        else
        {
           mobilemode.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

            Toast.makeText(getBaseContext(),"LOUD profile activated !",Toast.LENGTH_SHORT).show();
        }



        }




        else {
          latituteField.setText("Location not available");
          longitudeField.setText("Location not available");
        }
    }
        else {
            alertbox("Gps Status!!", "Your GPS is: OFF");
        }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.location, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.menu_settings:
                 Intent intent = new Intent( ShowLocationActivity.this,SetPreference.class);
                 startActivityForResult(intent, 0);
                 checkPref();
                 return true;

            default:
                return super.onOptionsItemSelected(item);
             }








        }

      private void checkPref(){

          SharedPreferences myPref

          = PreferenceManager.getDefaultSharedPreferences(ShowLocationActivity.this);

          String _pref =

                  "Option 1: " +  myPref.getBoolean("pref_opt1", true);


      }



      @Override

      protected void onActivityResult(int requestCode, int resultCode, Intent data) {

          // TODO Auto-generated method stub

          super.onActivityResult(requestCode, resultCode, data);

          checkPref();

      }
    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        lat = (double) (location.getLatitude());
         lng = (double) (location.getLongitude());
        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));


    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
         Toast.makeText(this, "Disabled provider " + provider,
                    Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Enabled new provider " + provider,
                Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
     @Override
    protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 400, 1, this);
      }


      @Override
      protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
      }

      public void rrr(View v)
      {

          DownloadTask n= new DownloadTask();
             n.doInBackground();
      }
      public void onDestroy()
      {
          super.onDestroy();
          Log.v("hhh","on destroy");
             DownloadTask n= new DownloadTask();
             n.doInBackground();
      }

      public Boolean displayGpsStatus() {
            ContentResolver contentResolver = getBaseContext().getContentResolver();
            boolean gpsStatus = Settings.Secure.isLocationProviderEnabled(
                    contentResolver, LocationManager.GPS_PROVIDER);
            if (gpsStatus) {
                return true;

            } else {
                return false;
            }
        }

        /*----------Method to create an AlertBox ------------- */
        protected void alertbox(String title, String mymessage) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Your Device's GPS is Disable")
                    .setCancelable(false)
                    .setTitle("** Gps Status **")
                    .setPositiveButton("Gps On",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    // finish the current activity
                                    // AlertBoxAdvance.this.finish();
                                    Intent myIntent = new Intent(
                                            Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                    startActivity(myIntent);
                                    dialog.cancel();
                                }
                            })
                    .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    // cancel the dialog box
                                    dialog.cancel();
                                }
                            });
            AlertDialog alert = builder.create();
            alert.show();
        }


    public class DownloadTask extends AsyncTask<Integer, Integer, Void> {



        @Override
        protected Void doInBackground(Integer... params) {


            Log.v("hhh","back");
            onCreate(null);
            // onLocationChanged(location);
            /*
            lat = (double) (location.getLatitude());
             lng = (double) (location.getLongitude());
            latituteField.setText(String.valueOf(lat));
            longitudeField.setText(String.valueOf(lng));

            if (location != null) {
                  System.out.println("Provider " + provider + " has been selected.");
                  onLocationChanged(location);


                  if((lat==12.905478)&&(lng==80.093358))
                {
                    mobilemode.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

                    Toast.makeText(getBaseContext(),"VIBRATE profile activated ",Toast.LENGTH_SHORT).show();



                }
                else if ((lat==12.90080625)&&(lng==80.09210655))
                {
                    mobilemode.setRingerMode(AudioManager.RINGER_MODE_SILENT);





                    Toast.makeText(getBaseContext(),"SILENT profile activated !",Toast.LENGTH_SHORT).show();
                    //notification.setLatestEventInfo(ShowLocationActivity.this, "changed to silent mode", "because u r in office", pendingIntent);
                      //  notificationManager.notify(i, notification);
                }
                else
                {
                   mobilemode.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

                    Toast.makeText(getBaseContext(),"LOUD profile activated !",Toast.LENGTH_SHORT).show();
                }



                }




                else {
                  latituteField.setText("Location not available");
                  longitudeField.setText("Location not available");
                }






            // TODO Auto-generated method stub


        */
            return null;
        }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:59:41+00:00Added an answer on June 11, 2026 at 3:59 pm

    You have coded in Activity, I suggest you to changed it to Android Service. A Service is a component which runs in the background, without interacting with the user.

    Have a look at Vogella’s Service Example.

    Just Convert your Activity code into Android Service and it will work in background without any GUI.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: how to run my application in background in iphone? Hi friends. Am
Possible duplicate: Run single instance of an application using Mutex I am using VS
Possible Duplicate: Slow Android emulator I am trying to run an android application in
Possible Duplicate: Run code when Android app is closed/sent to background In my android
Possible Duplicate: automatically run an application on Android phone startup I have developed an
Possible Duplicate: How to switch an Application between Themed and not Themed at run-time?
Possible Duplicate: Why does Android app run in small window on tablet emulator? i
Possible Duplicate: Run one instance from the application let's suppose i make an application
Possible Duplicate: Start android application without activity I want to do a download tool
Possible Duplicate: C#: Run external console program as hidden I am using a Windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.