使用广播接收器android设置多个Proximity Alerts

时间:2016-07-12 12:29:10

标签: android broadcastreceiver alert proximity

考虑家庭,办公室和游乐场等三个地点。每当我进入和退出该位置时,它都会显示消息提示。 这是我添加多个接近点的主要活动 的 MainActivity.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);

}

//这是我未决的意向接收器,我通知输入和现有警报 的 PendingIntentReceiver.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);

    }


}

1 个答案:

答案 0 :(得分:0)

试试这个:

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);