Я пытаюсь настроить самое основное оповещение о близости, но ничего не происходит. Я довольно новичок в программировании на Android, поэтому, пожалуйста, дайте мне знать, что я делаю неправильно или что мне не хватает. Меня вдохновили некоторые исходные коды и this one. Вот мой код:Android GPS близорукость не работает
package com.example.proximityalert;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
public class MainActivity extends Activity {
private static final String PROX_ALERT_INTENT = "com.example.proximityalert";
private IntentReceiver locationReminderReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(PROX_ALERT_INTENT);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationManager.addProximityAlert(55.586301,13.045417, 200, -1, pendingIntent);
this.locationReminderReceiver = new IntentReceiver();
final IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
this.registerReceiver(this.locationReminderReceiver, filter);
}
}
и приемник
package com.example.proximityalert;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.util.Log;
import android.widget.Toast;
public class IntentReceiver extends BroadcastReceiver{
// @SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering = intent.getBooleanExtra(key, false);
if (entering) {
Toast.makeText(context, "LocationReminderReceiver entering", Toast.LENGTH_SHORT).show();
Log.i("LocationReminderReceiver", "entering");
} else {
Toast.makeText(context, "LocationReminderReceiver exiting", Toast.LENGTH_SHORT).show();
Log.i("LocationReminderReceiver", "exiting");
}
}
}
пытаются переместить свое намерение и pendingintent к после того, как вы registerReciever – JRowan
я изменил его на это, и до сих пор ничего .. 'this.locationReminderReceiver = новый IntentReceiver(); \t \t \t \t Фильтр IntentFilter = новый IntentFilter (PROX_ALERT_INTENT); \t \t this.registerReceiver (this.locationReminderReceiver, filter); \t \t \t \t Цель намерения = новое намерение (PROX_ALERT_INTENT); \t \t PendingIntent pendingIntent = PendingIntent.getBroadcast (MainActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT); \t \t LocationManager locationManager = (LocationManager) this.getSystemService (Context.LOCATION_SERVICE); \t \t locationManager.addProximityAlert (55.586301,13.045417, 200, -1, pendingIntent); ' –
шахтных работы и только различие это я PendingIntent.FLAG_CANCEL_CURRENT вместо PendingIntent.FLAG_ONE_SHOT и им запустить его в службе – JRowan