Ich entwickle und Android-Anwendung, die einen Rundfunkempfänger hat eingehende SMS von einer bestimmten Anzahl zu übertragen. Wenn die Nachricht von dieser Nummer stammt und in einem bestimmten Format vorliegt, antwortet die Anwendung mit der aktuellen Länge und Breite des Benutzers. AberFusedLocationApi gibt null zurück
, wenn die Antwortnachricht erzeugt wird, die Lage wird null always.I ist mit GoogleApiClient Objekt und FusedLocationApi.
mLastLocation = LocationServices.FusedLocationApi.getLastLocation (googleAPIClient);
immer null zurück und meine Botschaft enthält an den Orten der Längen- und Breitengrad null-Werte. Hier ist der Dienst, der vom Broadcast-Empfänger angerufen wird, wenn eine Nachricht eingeht und nach dem Standort fragt.
public class ReplyWithLocationService erweitert Dienst implementiert GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedList, Location {
private BroadcastReceiver senderBroadcastReceiver, deliverBroadcastReceiver;
private String SENT = "SMS_SENT";
private String DELIVERED = "SMS_DELIVERED";
private String latitude, longitude;
private LocationRequest locationRequest;
private LocationManager locationManager;
private ConnectivityManager connectivityManager;
private GoogleApiClient googleAPIClient;
private boolean isConnected;
private Location mLastLocation;
private NetworkInfo activeNetwork;
private boolean isConnectedInternet;
private boolean connectivity = true;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
setUpGoogleAPIClient();
setUpLocationRequest();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
senderBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS Sent", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
break;
}
}
};
deliverBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS Delivered", Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
break;
}
}
};
registerReceiver(senderBroadcastReceiver, new IntentFilter(SENT));
registerReceiver(deliverBroadcastReceiver, new IntentFilter(DELIVERED));
//setUpGoogleAPIClient();
//setUpLocationRequest();
}
private void setUpGoogleAPIClient() {
Log.e("APIClient", String.valueOf(googleAPIClient));
if (googleAPIClient == null) {
this.googleAPIClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
}
Log.d("APIClient", String.valueOf(googleAPIClient));
}
private void setUpLocationRequest() {
locationRequest = LocationRequest.create().setInterval(1000).setFastestInterval(1000).setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//setUpGoogleAPIClient();
activeNetwork = connectivityManager.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnectedOrConnecting()) {
isConnectedInternet = true;
}
connectivity = checkConnectivity();
if (connectivity) {
if (!googleAPIClient.isConnected() || !googleAPIClient.isConnecting()) {
googleAPIClient.connect();
Log.e("APICLient", "Connect");
}
if (googleAPIClient.isConnected()) {
isConnected = true;
Log.e("APICLient", "Connected");
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
//return TODO;
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(googleAPIClient);
Log.d("LastLocation", String.valueOf(mLastLocation));
if (mLastLocation != null) {
latitude = String.valueOf(mLastLocation.getLatitude());
longitude = String.valueOf(mLastLocation.getLongitude());
Log.e("Latitude", latitude);
Log.e("Longitude", longitude);
mLastLocation = null;
}
} else {
TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
CellLocation cellLocation = tm.getCellLocation();
GsmCellLocation gsmLocation = (GsmCellLocation) cellLocation;
}
if (intent.getStringExtra("Pin1") != null && intent.getStringExtra("Pin2") != null) {
String pin1 = intent.getStringExtra("Pin1");
String pin2 = intent.getStringExtra("Pin2");
Log.e("PIN1", pin1);
Log.e("PIN2", pin2);
sendLocationSMS(pin1, pin2);
}
return START_STICKY;
}
private boolean checkConnectivity() {
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !isConnectedInternet) {
connectivity = false;
} else if (!isConnectedInternet) {
connectivity = false;
} else if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
connectivity = false;
}
return connectivity;
}
private void sendLocationSMS(String pin1, String pin2) {
String message = "FIND Location:" + pin1 + ":" + latitude + ":" + longitude + ":" + pin2;
Log.e("LocationMessage", message);
String number = "77131";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(number, null, message, sentPI, deliveredPI);
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(senderBroadcastReceiver);
unregisterReceiver(deliverBroadcastReceiver);
googleAPIClient.disconnect();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
locationRequest = LocationRequest.create().setInterval(1000).setFastestInterval(1000).setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
Log.e("LocationRequest", String.valueOf(locationRequest));
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}