我不是母语为英语的人,我正在和翻译一起写作。我想让你了解上下文是否很奇怪。
我希望定期在后台使用Google Place API。
MainActivity.java
private static final String LOG_TAG = "PlacesAPIActivity";
private static final int GOOGLE_API_CLIENT_ID = 0;
private GoogleApiClient mGoogleApiClient;
private static final int PERMISSION_REQUEST_CODE = 100;
private TextView txtView,txtView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_places_api);
txtView = (TextView)findViewById(R.id.txtview);
txtView2 = (TextView)findViewById(R.id.txtview2);
Button currentButton = (Button) findViewById(R.id.currentButton);
mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
.addApi(Places.PLACE_DETECTION_API)
.enableAutoManage(this, GOOGLE_API_CLIENT_ID, this)
.build();
currentButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mGoogleApiClient.isConnected()) {
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSION_REQUEST_CODE);
} else {
//callPlaceDetectionApi();
Intent intent = new Intent(getApplicationContext(), MyService.class);
startService(intent);
}
}
}
});
}
public void callPlaceDetectionApi() throws SecurityException {
PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
.getCurrentPlace(mGoogleApiClient, null);
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
@Override
public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
for (PlaceLikelihood placeLikelihood : likelyPlaces) {
float temp = placeLikelihood.getLikelihood();
String tempName = placeLikelihood.getPlace().getName().toString();
Log.i(LOG_TAG, String.format("Place '%s' with " +
"likelihood: %g",
placeLikelihood.getPlace().getName(),
placeLikelihood.getLikelihood()));
if (temp <= a)
{
rightplace = placeLikelihood.getPlace().getName().toString();
a = temp;
rightplace = tempName;
}
}
txtView.setText(rightplace);
txtView2.setText(Float.toString(a));
likelyPlaces.release();
}
});
}
MyService.java
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
myServiceHandler handler = new myServiceHandler();
thread = new ServiceThread(handler);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread.start();
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
myActivity = new MyActivity();
}
@Override
public void onDestroy() {
super.onDestroy();
thread.stopForever();
thread = null;
}
class myServiceHandler extends Handler {
@Override
public void handleMessage(android.os.Message msg) {
Toast.makeText(MyService.this, "testest", Toast.LENGTH_LONG).show();
myActivity.getInstance().callPlaceDetectionApi();
}
};
MyActivity.java
公共类MyActivity扩展了Application {
private static volatile MainActivity mainActivity;
public static MainActivity getInstance()
{
if(mainActivity == null)
{
synchronized (MyActivity.class)
{
if (mainActivity == null)
{
mainActivity = new MainActivity();
}
}
}
return mainActivity;
}
@Override
public void onCreate() {
super.onCreate();
}
我不知道为什么应用程序在运行Service时会挂起。 我需要你的帮助。