0
我從谷歌地圖API,最近的地方, 我怎麼做一個服務,當地方正在改變,新的數據來時,因爲我可以使用通知,如「檢測到新的地方「或thx。聽衆服務的名單適配器
主要活動:
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();
}
});
}
我empry服務:
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");
}
}
下面
在那裏我必須使用你的公共類測試擴展活動實現View.OnClickListener,在我的主要活動? – Azarnoy
在哪裏我必須寫硝化?在onLocationChanged? – Azarnoy
你可以在任何活動中做到這一點,你想發起服務 – UMESH0492