我从google maps api获取数据,最近的地方, 我如何制作一个可以在地点变化时收听的服务,当有新数据出现时,我可以在那里使用类似"检测到新地点的通知"等等。
主要活动:
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = "tag";
private GoogleApiClient mGoogleApiClient;
ArrayList<String> listForAd = new ArrayList<>();
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView);
adapter = new ArrayAdapter<String>(this, R.layout.list_item, listForAd);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
mGoogleApiClient = new GoogleApiClient
.Builder(this)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
List<String> filterType = new ArrayList<>();
filterType.add(Integer.toString(Place.TYPE_CAFE));
PlaceFilter filter = new PlaceFilter(false,filterType);
PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
.getCurrentPlace(mGoogleApiClient, null);
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
@Override
public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
for (PlaceLikelihood placeLikelihood : likelyPlaces) {
Log.i(TAG, String.format("Place '%s' has likelihood: %g",
placeLikelihood.getPlace().getName(),
placeLikelihood.getLikelihood()));
listForAd.add(String.valueOf(placeLikelihood.getPlace().getName()));
}
adapter.notifyDataSetChanged();
likelyPlaces.release();
}
});
}
我的服务:
public class MyService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
答案 0 :(得分:0)
在下面结帐时,您只需要创建一个服务并实现位置监听器
package app.test;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import java.util.ArrayList;
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
class TrackerService extends Service implements LocationListener {
private static final String LOGTAG = "TrackerService";
private LocationManager manager;
private ArrayList<Location> storedLocations;
private boolean isTracking = false;
@Override
public void onCreate() {
manager = (LocationManager)getSystemService(LOCATION_SERVICE);
storedLocations = new ArrayList<Location>();
Log.i(LOGTAG, "Tracking Service Running...");
}
public void startTracking() {
if(!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
return;
}
Toast.makeText(this, "Starting Tracker", Toast.LENGTH_SHORT).show();
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 0, this);
isTracking = true;
}
public void stopTracking() {
Toast.makeText(this, "Stopping Tracker", Toast.LENGTH_SHORT).show();
manager.removeUpdates(this);
isTracking = false;
}
public boolean isTracking() {
return isTracking;
}
@Override
public void onDestroy() {
manager.removeUpdates(this);
Log.i(LOGTAG, "Tracking Service Stopped...");
}
public class TrackerBinder extends Binder {
TrackerService getService() {
return TrackerService.this;
}
}
private final IBinder binder = new TrackerBinder();
@Override
public IBinder onBind(Intent intent) {
return binder;
}
public int getLocationsCount() {
return storedLocations.size();
}
public ArrayList<Location> getLocations() {
return storedLocations;
}
@Override
public void onLocationChanged(Location location) {
Log.i("TrackerService", "Adding new location");
storedLocations.add(location);
}
@Override
public void onProviderDisabled(String provider) { }
@Override
public void onProviderEnabled(String provider) { }
@Override
public void onStatusChanged(String provider, int status, Bundle extras) { }
}
public class Test extends Activity implements View.OnClickListener {
Button enableButton, disableButton;
TextView statusView;
TrackerService trackerService;
Intent serviceIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
enableButton = (Button)findViewById(R.id.enable);
enableButton.setOnClickListener(this);
disableButton = (Button)findViewById(R.id.disable);
disableButton.setOnClickListener(this);
statusView = (TextView)findViewById(R.id.status);
serviceIntent = new Intent(this, TrackerService.class);
}
@Override
public void onResume() {
super.onResume();
startService(serviceIntent);
bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}
@Override
public void onPause() {
super.onPause();
if(!trackerService.isTracking()) {
stopService(serviceIntent);
}
unbindService(serviceConnection);
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.enable:
trackerService.startTracking();
break;
case R.id.disable:
trackerService.stopTracking();
break;
default:
break;
}
updateStatus();
}
private void updateStatus() {
if(trackerService.isTracking()) {
statusView.setText(String.format("Tracking enabled. %d locations logged.",trackerService.getLocationsCount()));
} else {
statusView.setText("Tracking not currently enabled.");
}
}
private ServiceConnection serviceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
trackerService = ((TrackerService.TrackerBinder)service).getService();
updateStatus();
}
public void onServiceDisconnected(ComponentName className) {
trackerService = null;
}
};
}