在后台服务中运行Gps

时间:2013-12-09 06:51:11

标签: android

我需要不断跟踪用户位置我的应用程序是否正在运行,为此我已经创建了一个后台服务并在此服务中计算Asyntask的纬度和经度,我的代码在模拟器中运行良好但在设备上运行得很好在说申请没有回复后崩溃了。但是log cat没有显示任何错误。请有人帮助我。在此先感谢。

这是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

这是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

2 个答案:

答案 0 :(得分:0)

你在主线程上执行非常繁重的任务

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

你的方法会被频繁调用,可能是每秒3-4次(不计算多少次)。实际上,当您更改位置详细信息时,它将被调用。您正在调用要调用的方法handleLocationChanged(loc);

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

在SD卡中创建/打开文件并将数据写入其中的方法。这可能会产生问题,您可能会在应用程序中遇到ANR问题。 不要那样做。使用2-3秒的时间差将位置数据写入文件。此外,您直接编写数据而不是更新数据,因此请记住这一点。

希望有所帮助。

答案 1 :(得分:0)

最后我得到了答案,上面的代码没有错,只是使用Myservice.this代替MainActivity.getContextForService()并将时间增加到0到5000毫秒,现在它的工作就像魅力一样