1

I need to track user location continuously whether my application is running or not, for this i have a created a background Service and calculate latitude and longitude on Asyntask in this service , my code is running well in emulator but on device it got crashed after saying application is not responding. However log cat does not show any error. Please anybody help me. Thanks in Advance.

Here is MyService.java

public class MyService extends Service {
    // flag for GPS status
    public static LocationManager manager;
    public static boolean isGPSEnabled = false;
    public static double lattitude = 0;
    public static double longitude = 0;
    int count = 0;
    // flag for network status
    boolean isNetworkEnabled = false;

    // flag for GPS status
    boolean canGetLocation = false;
     MyFile myFile;
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    } // method ends

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        super.onStartCommand(intent, flags, startId);
        System.out.println("On Start Command is called ");
        return START_STICKY;
    } // method ends

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        System.out.println("Service is created//////////// ");
        // start asyntask to get locations
         Context mContext = MainActivity.getContextForService();
         if(mContext!=null)
         {
             myFile = new MyFile(mContext);

         }
        new GetLocations().execute();
    }// on create ends

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        System.out.println("Service is  Destroyed  //////////// ");
        if (manager != null && isGPSEnabled == true) {
            manager.removeUpdates(mylistener);
            System.out.println("Service is  Destroyed under if //////////// ");
        }

    } // method ends

    public class GetLocations extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            // getting GPS status
            isGPSEnabled = manager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);
            // getting network status
            isNetworkEnabled = manager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            if (isGPSEnabled) {
                /*Criteria criteria = new Criteria();
                String bestProvider = manager.getBestProvider(criteria, false);
                Location location = manager.getLastKnownLocation(bestProvider);
                double lat =location.getLatitude();
                double longi =location.getLongitude();
                  System.out.println("getting location continous ////// Lattti " +location.getLatitude()
                          );
                          System.out.println("getting location continous ////// LONGITU " +
                          location.getLongitude());*/
                manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                        0, mylistener);
                File root = new File(Environment.getExternalStorageDirectory(),
                        "Notes");
                if (!root.exists()) {
                    root.mkdirs();
                }
            } else {
                Toast.makeText(MainActivity.getContextForService(),
                        "Please oN Gps ", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
            // getting current lattitude and longitude

            return null;
        }// method ends
    }// asyntask class ends

    void handleLocationChanged(Location loc) {
        lattitude = loc.getLatitude();
        longitude = loc.getLongitude();
        if (count == 0) {
            Toast.makeText(MainActivity.getContextForService(),
                    "lat is " + lattitude + " long is  " + longitude,
                    Toast.LENGTH_LONG).show();
            count = count + 1;
        }

          System.out.println("getting location continous ////// Lattti " +
          lattitude);
          System.out.println("getting location continous ////// LONGITU " +
          longitude);

        generateNoteOnSD("GpsTesting", "lattitude is" + lattitude + "\n"
                + " longitude" + longitude);

    } // method ends

    public LocationListener mylistener = new LocationListener() {

        @Override
        public void onLocationChanged(Location loc) {
            handleLocationChanged(loc);
        }

        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub
            // Toast.makeText(mContext, "Gps is disable",
            // Toast.LENGTH_SHORT).show();
        }

        public void onProviderEnabled(String arg0) {
            // TODO Auto-generated method stub
            // Toast.makeText(mContext, "Gps is on", Toast.LENGTH_SHORT).show();
        }

        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub
            // Toast.makeText(appcontext, "Gps  status is chnged ",
            // Toast.LENGTH_SHORT).show();
        }
    };

    public void showSettingsAlert() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                MainActivity.getContextForService());

        // Setting Dialog Title
        alertDialog.setTitle("Alert");

        // Setting Dialog Message
        alertDialog
                .setMessage("GPS is not enabled. Do you want to go to settings menu?");

        // On pressing Settings button
        alertDialog.setPositiveButton("Settings",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(
                                Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        getApplicationContext().startActivity(intent);
                    }
                });

        // on pressing cancel button
        alertDialog.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

        // Showing Alert Message
        alertDialog.show();
    }// method ends
        // method for sending notiifcation

    public void sendNotification() {
        int MY_NOTIFICATION_ID = 1;
        PendingIntent contentIntent = PendingIntent.getActivity(
                getApplicationContext(), 0,
                new Intent(MainActivity.getContextForService(),
                        MainActivity.class), 0);
        Notification myNotification = new NotificationCompat.Builder(
                getApplicationContext())
                .setContentTitle("Notification for Lat and Long")
                .setContentText("hi testing notiifaction on lat and long")
                .setTicker(" ToDoList Notification")
                .setWhen(System.currentTimeMillis())
                .setContentIntent(contentIntent)
                .setDefaults(Notification.DEFAULT_SOUND).setAutoCancel(true)
                .setSmallIcon(R.drawable.ic_launcher).build();
        NotificationManager notificationManager = (NotificationManager) MainActivity
                .getContextForService().getSystemService(
                        Context.NOTIFICATION_SERVICE);
        notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
    } // method ends
        // writing file on

    public void generateNoteOnSD(String sFileName, String sBody) {
        try {
            File root = new File(Environment.getExternalStorageDirectory(),
                    "Gps_Data");
            if (!root.exists()) {
                root.mkdirs();
            }
            File gpxfile = new File(root, sFileName);
            FileWriter writer = new FileWriter(gpxfile);
            writer.append(sBody);
            writer.flush();
            writer.close();

        } catch (IOException e) {
            e.printStackTrace();

        }
    } // method ends
} // final class ends

Here is MainActivity.java

public class MainActivity extends Activity {
        Button btnStart, btnStop;
        public static Context mContext;
        Intent it;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mContext = this;
            btnStart = (Button) findViewById(R.id.btnStart);
            btnStop = (Button) findViewById(R.id.btnStop);
            btnStart.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    startService(getIntent());
                }
            });
            btnStop.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    stopService(getIntent());
                }
            });
        } // on create ends

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        public Intent getIntent() {
            it = new Intent(mContext, MyService.class);
            return it;
        } // method ends

        public static Context getContextForService() {
            return mContext;
        } // methods ends
    } // final class ends
Dhiraj Choudhary
  • 195
  • 5
  • 16

2 Answers2

0

Man you are doing very heavy task on your main thread which is

@Override
        public void onLocationChanged(Location loc) {
            handleLocationChanged(loc);
        }

your method will be called very frequently may be 3-4 times in sec (Did not count how many time). Actually it will be called when ever you location details changed. You are calling method handleLocationChanged(loc); in which you are calling

generateNoteOnSD("GpsTesting", "lattitude is" + lattitude + "\n"
            + " longitude" + longitude);

Method which is creating/opening file in sd card and writing data into it. This can create a problem and you can encounter problem of ANR in the application. Do not do that. Use a difference of time like 2-3 seconds to write location data into file. Also you are directly writing data not updating it so also keep that in mind.

Hope that helps.

Abhinav Singh Maurya
  • 3,313
  • 8
  • 33
  • 51
0

Finally i had got my answer, nothing is wrong in above code just using Myservice.this in place of MainActivity.getContextForService() and increasing time to 0 to 5000 milliseconds , Now its working like charm

Dhiraj Choudhary
  • 195
  • 5
  • 16