考虑三个位置,如家庭,办公室和游乐场。每当我从该位置进入和退出时,它都会显示消息警报。这是我的主要活动,我添加了多个接近点主要活动.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
latitudeEditText = (EditText) findViewById(R.id.point_latitude);
longitudeEditText = (EditText) findViewById(R.id.point_longitude);
addAlertButton = (Button) findViewById(R.id.add_alert_button);
double latitude = 43.039, latitude1 = 43.039;
double longitude = 60.238, longitude1 = 60.239;
addProximityAlert(latitude, longitude);
addProximityAlert(latitude1, longitude1);
Toast.makeText(getApplicationContext(), "Alert Added", Toast.LENGTH_SHORT).show();
}
private void addProximityAlert(double latitude,double longitude) {
Intent intent = new Intent(PROX_ALERT_INTENT);
intent.putExtra("lat",latitude);
intent.putExtra("long",longitude);
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, (int) System.currentTimeMillis(), intent,PendingIntent.FLAG_CANCEL_CURRENT);
Toast.makeText(this,Long.toString(System.currentTimeMillis()),Toast.LENGTH_LONG).show();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager.addProximityAlert(latitude,
longitude,
POINT_RADIUS,
PROX_ALERT_EXPIRATION,
proximityIntent
);
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
registerReceiver(new ProximityIntentReceiver(), filter);
}
这是我待处理的意图接收器,我在其中通知输入和现有警报挂起的意图接收器.java
public class ProximityIntentReceiver extends BroadcastReceiver {
private static final int NOTIFICATION_ID = 1000;
double lat,lng;
String d="";
@Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering = intent.getBooleanExtra(key, false);
lat=intent.getDoubleExtra("lat",0.00);
lng=intent.getDoubleExtra("long",0.00);
if (entering) {
Log.d(getClass().getSimpleName(), "entering");
d="Entering region"+Double.toString(lat)+Double.toString(lng);
Toast.makeText(context, d, Toast.LENGTH_SHORT).show();
}else {
Log.d(getClass().getSimpleName(), "exiting");
d= "Exiting region"+Double.toString(lat)+Double.toString(lng);
Toast.makeText(context,d, Toast.LENGTH_SHORT).show();
}
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), notificationIntent, 0);
Notification n = new Notification.Builder(context)
.setContentTitle("Proximity")
.setContentText("Subject: "+d)
.setSmallIcon(R.drawable.hell)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
notificationManager.notify((int) System.currentTimeMillis(), n);
}
}
试试这个:
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setAction(Long.toString(System.currentTimeMillis()));
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Notification n = new Notification.Builder(context)
.setContentTitle("Proximity")
.setContentText("Subject: "+d)
.setSmallIcon(R.drawable.hell)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
notificationManager.notify((int) System.currentTimeMillis(), n);