简体   繁体   中英

gps monitoring service in android

I'm trying to create a program for android that constantly (every minute) gets the gps location and then sends it to my server at home (so that i always know where my phone is). I created a gui with a start-button which starts the service:

start.setOnClickListener(new View.OnClickListener() {
        synchronized public void onClick(View v) {
            startService(new Intent(GpsTest2.this, GTService.class));
        }
});

Then my service is declared like this:

public class GTService extends Service implements LocationListener {
}

This GTService has a method for retrieving the data:

public void onLocationChanged(Location location) {
}

@Override
public void onCreate() {
    super.onCreate();
    LocationManager locMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, interval * 1000, minDist, this);
}

In AndroidManifest.xml I have:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <service android:name=".GTService">
    </service>

This doesn't seem to work: no data is logged. What am I doing wrong here?

Your code seems to be working fine except this,

You are using LocationManager.NETWORK_PROVIDER

locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, interval * 1000, minDist, this);

Where as it should be LocationManager.GPS_PROVIDER

locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2 * 1000, 10, locationListener);

Hope this will surely do for you. Thanks.

The following should work fine,

Manifest :

    <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.smartconcept.locationmonitor.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".GTService" />
</application>

GTService :

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;

public class GTService extends Service implements LocationListener {
LocationManager locMgr;
@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
 public void onCreate(){
    super.onCreate();
    locMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1 * 1000, 0, this);
}

@Override
 public void onDestroy(){
    super.onDestroy();
    locMgr.removeUpdates(this);
 }

@Override
public void onLocationChanged(Location loc) {
    Toast.makeText(getBaseContext(), String.valueOf(loc.getLatitude()) + "\n" + String.valueOf(loc.getLongitude()), Toast.LENGTH_SHORT).show();

}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}

}

MainActivity:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btnStart = (Button)findViewById(R.id.btnStart);
    btnStart.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            startService(new Intent(MainActivity.this,GTService.class));
        }

    });
}

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

}

Hope this helps.

The location from Network provider is not accurate. PLease use GPS instead. If you really want to user Network only then I would recomond to set minDistance param as 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM