我可以在android应用程序的单个服务中编写两个回调函数吗
我可以在android API的单一后台服务中添加两个回调函数吗..就像我知道LocationManager并不能在所有android手机上运行一样,所以一些人建议在android API中使用FusedLocationProviderApi库。所以我可以编写一个后台服务,其中使用两种方法。这样两种方法中的任何一种都可以工作,但不知道当已经在使用listener时,哪一个会起作用。基本的问题是,我可以添加额外的正常或回调函数和回调函数,它会起作用吗 我的代码如下:我可以在android应用程序的单个服务中编写两个回调函数吗,android,Android,我可以在android API的单一后台服务中添加两个回调函数吗..就像我知道LocationManager并不能在所有android手机上运行一样,所以一些人建议在android API中使用FusedLocationProviderApi库。所以我可以编写一个后台服务,其中使用两种方法。这样两种方法中的任何一种都可以工作,但不知道当已经在使用listener时,哪一个会起作用。基本的问题是,我可以添加额外的正常或回调函数和回调函数,它会起作用吗 我的代码如下: import android.
import android.app.PendingIntent;
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.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
//changes - start for fused location
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.api.GoogleApiClient;
//import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
//changes end for fused location with one comment above library
public class LocationService extends Service implements ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
public static final String BROADCAST_ACTION = "Hello World";
public LocationManager locationManager;
public MyLocationListener listener;
public Location previousBestLocation = null;
Intent intent;
int counter = 0;
//fused changes -- start
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
com.google.android.gms.location.LocationListener mGoogleListener;
//fused chhanges - end
@Override
public void onCreate()
{
super.onCreate();
intent = new Intent(BROADCAST_ACTION);
//fused changes --start
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
//fused changes -- end
}
@Override
public int onStartCommand(Intent intent,int flag, int startId)
{
//first call LocationManager
try {
int time = 2 * 60 * 1000;
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, time, 0, listener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, time, 0, listener);
} catch (Exception e) {
String error = e.getMessage();
}
//second call --fused class - start
try {
mGoogleApiClient.connect();
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(1 * 1000); // 1 second, in milliseconds
//LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, listener);
//fused location --end
} catch (Exception e) {
String error = e.getMessage();
}
return Service.START_STICKY;
} catch (Exception e){
stopService(new Intent(LocationService.this,LocationService.class));
return Service.START_STICKY;
}
}
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onDestroy() {
// handler.removeCallbacks(sendUpdatesToUI);
super.onDestroy();
Log.v("STOP_SERVICE", "DONE");
locationManager.removeUpdates(listener);
}
public static Thread performOnBackgroundThread(final Runnable runnable) {
final Thread t = new Thread() {
@Override
public void run() {
try {
runnable.run();
} finally {
}
}
};
t.start();
return t;
}
@Override
public void onConnected(Bundle bundle) {
try {
//tried with pending intent but not working
//LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, PendingIntent.getService(this, 1, new Intent(this, MyLocationListener.class), 0));
//tried with mGooogleListener
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest, mGoogleListener);
} catch (Exception e) {
String error = e.getMessage();
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onDisconnected() {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onLocationChanged(Location location) {
//when fused library detect location change - gets here do whatever want to do
double locLat = location.getLatitude();
double locLong = location.getLongitude();
}
public class MyLocationListener implements LocationListener
{
class RequestTask extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... uri) {
String responseString = null;
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
public void onLocationChanged(final Location loc)
{
//when locationManager detect location change, then gets here , now do whatever want to do
}
public void onProviderDisabled(String provider)
{
//Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
}
public void onProviderEnabled(String provider)
{
//Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
}
导入android.app.pendingent;
导入android.app.Service;
导入android.content.Context;
导入android.content.Intent;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.net.ConnectivityManager;
导入android.net.NetworkInfo;
导入android.net.Uri;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.os.IBinder;
导入android.util.Log;
导入org.apache.http.HttpResponse;
导入org.apache.http.HttpStatus;
导入org.apache.http.StatusLine;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.json.JSONObject;
导入java.io.BufferedInputStream;
导入java.io.BufferedReader;
导入java.io.ByteArrayOutputStream;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.math.biginger;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.security.MessageDigest;
导入java.security.NoSuchAlgorithmException;
//更改-保险丝位置的启动
导入com.google.android.gms.common.ConnectionResult;
导入com.google.android.gms.common.GooglePlayServicesClient;
导入com.google.android.gms.common.api.GoogleAppClient;
//导入com.google.android.gms.location.LocationListener;
导入com.google.android.gms.location.LocationRequest;
导入com.google.android.gms.location.LocationServices;
导入com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
//更改融合位置的结尾,在库上方添加一条注释
公共类LocationService扩展服务实现ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener、com.google.android.gms.location.LocationListener、GoogleAppClient.ConnectionCallbacks、GoogleAppClient.OnConnectionFailedListener{
公共静态最终字符串广播\u ACTION=“Hello World”;
公共场所经理;
公共MyLocationListener;
公共位置previousBestLocation=null;
意图;
int计数器=0;
//融合的变化——开始
私人GoogleapClient MGoogleapClient;
私人位置请求mLocationRequest;
com.google.android.gms.location.LocationListener mGoogleListener;
//熔合换热器-结束
@凌驾
public void onCreate()
{
super.onCreate();
意图=新意图(广播行动);
//融合的变化——开始
mgoogleapclient=新的Googleapclient.Builder(此)
.addApi(LocationServices.API)
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(此)
.build();
//融合变化——结束
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId)
{
//第一次呼叫位置管理器
试一试{
整数时间=2*60*1000;
locationManager=(locationManager)getSystemService(Context.LOCATION\u服务);
listener=新的MyLocationListener();
locationManager.RequestLocationUpdate(locationManager.NETWORK\u提供程序,时间,0,侦听器);
locationManager.RequestLocationUpdate(locationManager.GPS\提供程序,时间,0,侦听器);
}捕获(例外e){
字符串错误=e.getMessage();
}
//第二个调用--融合类-启动
试一试{
mGoogleApiClient.connect();
mLocationRequest=LocationRequest.create()
.setPriority(定位请求。优先级高精度)
.setInterval(10*1000)//10秒,以毫秒为单位
.setFastTestInterval(1*1000);//1秒,以毫秒为单位
//LocationServices.FusedLocationApi.RequestLocationUpdate(mgoogleAppClient、mlLocationRequest、listener);
//熔合位置-结束
}捕获(例外e){
字符串错误=e.getMessage();
}
return Service.START\u STICKY;
}捕获(例外e){
stopService(新意图(LocationService.this,LocationService.class));
return Service.START\u STICKY;
}
}
@凌驾
公共IBinder onBind(意向)
{
返回null;
}
@凌驾
公共空间{
//handler.removeCallbacks(sendUpdatesToUI);
super.ondestory();
Log.v(“停止服务”,“完成”);
locationManager.RemoveUpdate(侦听器);
}
公共静态线程性能BackgroundThread(最终可运行可运行){
最终螺纹t=新螺纹(){
@凌驾
公开募捐{
试一试{
runnable.run();
}最后{
}
}
};
t、 start();
返回t;
}
@凌驾
未连接的公共空间(捆绑包){
试一试{
//以待定的意图尝试,但不起作用
//LocationServices.FusedLocationApi.RequestLocationUpdate(mgoogleAppClient、mlLocationRequest、PendingEvent.getService(thi