我使用的是广播接收器,但我无法获得接近1公里的任何用户的位置。我无法理解这里有什么问题。 接收器的代码是。 。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;
import android.widget.Toast;
public class ProximityReciever extends BroadcastReceiver {
LocationManager locationManager;
double latitude, longitude;
Location location;
Context context;
private static final long MIN_DISTANCE = 1000;
private static final long MIN_TIME = 6000;
private static final int NOTIFICATION_ID = 1000;
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Proximity Reciever", "Inside Reciever");
String k = LocationManager.KEY_LOCATION_CHANGED;
// Key for determining whether user is leaving or entering
this.context = context;
boolean state = intent.getBooleanExtra(k, false);
//Gives whether the user is entering or leaving in boolean form
try {
Log.d("ProxyReceiver_State", "Got the state:" + state);
if (state) {
// Call the Notification Service or anything else that you would like to do here
Log.d("ProxyRe_State_true", "Someone Came in . . ");
Toast.makeText(context, "Welcome to my Area", Toast.LENGTH_SHORT).show();
} else {
//Other custom Notification
Toast.makeText(context, "Thank you for visiting my Area,come back again !!", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Log.d("Proxy_Receiver", String.valueOf(e));
}
}
}
使用此接收器的活动代码是:
import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
public class Proxy extends AppCompatActivity {
private GPS_Tracker gps_tracker;
private LocationManager locationManager;
private double lat = 0.0, long1 = 0.0; //Defining Latitude & Longitude
float radius = 1000.0f;//in metres
private ProximityReciever reciever;
@SuppressLint("MissingPermission")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_proxy);
//reciever = new ProximityReciever();
//GPS tracker to know your current location . . ..
//i have created another class for this
try {
gps_tracker = new GPS_Tracker(Proxy.this);
Log.d("Can Get Location:", String.valueOf(gps_tracker.canGetLocation()));
if (gps_tracker.canGetLocation()) {
lat = gps_tracker.getLatitude();
long1 = gps_tracker.getLongitude();
Log.d("Proxy_MyLocation", "Latitude: " + lat + "Longitude: " + long1);
Toast.makeText(this, "My Location: Latitude: " + lat + " Longitude: " + long1, Toast.LENGTH_LONG).show();
} else {
gps_tracker.showSettingAlert();
}
} catch (Exception e) {
Log.d("Proxy_Exception", String.valueOf(e));
}
//tryinh out things with Proxiity Reciever. . . .
//calling it and which detects the other users . . . .
try {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Log.d("Proxy_Manager", "Getting Location manager and sending intent");
//Intent intent = new Intent("com.example.hp.proximityalert");
Intent intent = new Intent("com.example.hp.proximityalert.ProximityAlert");
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 1, intent, 0);
//PendingIntent pi = PendingIntent.getService(getApplicationContext(), 1, intent, 0);
locationManager.addProximityAlert(lat, long1, radius, -1, pi);
Log.d("Proxy_Manager", "Added PRoxy Alert");
reciever = new ProximityReciever();
//Receive something from the other activity . . .
IntentFilter intentFilter = new IntentFilter("com.example.hp.proximityalert.ProximityAlert");
//
Log.d("Proxy_Manager", "Intent Filter . . . .");
//
intentFilter.setPriority(999);
//this.registerReceiver(reciever, intentFilter);
registerReceiver(reciever, intentFilter);
Log.d("Proxy_Manager", "Receiver registered . . .");
} catch (Exception e) {
Log.d("Proxy_Proxy_error", String.valueOf(e));
}
}
}
我使用了当前位置并在我的位置周围创建了1公里半径的距离,以检测是否有相同应用的任何用户进入我的附近。 。 。 但即使在同一个房间里我也找不到其他用户
可以理解如何检测如何检测也使用相同应用程序的附近用户。 。 。