2016-03-14 41 views
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"); 
} 

}

下面

回答

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; 
    } 
}; 
} 
+0

在那裏我必須使用你的公共類測試擴展活動實現View.OnClickListener,在我的主要活動? – Azarnoy

+0

在哪裏我必須寫硝化?在onLocationChanged? – Azarnoy

+0

你可以在任何活動中做到這一點,你想發起服務 – UMESH0492