我正在嘗試設置最基本的接近警報,但似乎沒有發生。我對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移動到您註冊後收件人 – JRowan 2013-05-07 21:47:55
我已將其更改爲此,並且仍然沒有任何內容。 'this.locationReminderReceiver = new IntentReceiver(); \t \t \t \t IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT); \t \t this.registerReceiver(this.locationReminderReceiver,filter); \t \t \t \t Intent intent = new Intent(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);' – 2013-05-08 06:22:39
礦山工程和唯一性差異是我PendingIntent.FLAG_CANCEL_CURRENT代替PendingIntent.FLAG_ONE_SHOT和IM運行它的服務 – JRowan 2013-05-08 11:39:15