广播接收器的onReceive()被调用所有接近警报。我究竟做错了什么?

时间:2014-09-01 12:28:40

标签: android broadcastreceiver android-geofence

我是一个Android新手,学习android,我正在开发一个简单的Geofencing应用程序。 插入单个GeoFence时,一切正常。但是当我使用超过2个Geofences时,所有围栏的onReceive都会被触发,并且所有Geofences的通知都会在彼此之上创建。我尝试在网上搜索,但没有提出任何解决方案。任何人都可以帮我解决我做错的事吗? 非常感谢。 :)

以下是我编写的Service类的代码:

package com.example.multigeofence;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.sqlite.SQLiteDatabase;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;

public class GeoFenceService extends Service {

    LocationManager lm;
    BroadcastReceiver br;
    SQLiteDatabase myDatabase;
    BufferedReader bur;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @SuppressLint("SdCardPath")
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1,
                new MyLocationListener());


        addProximityAlert(12.955299,77.679977,"Tata Elxsi",50);
        addProximityAlert(12.187497,76.852970,"Mall",50);
        addProximityAlert(12.552929,77.079977,"Dummy place",50);

        return START_NOT_STICKY;
    }

    private void addProximityAlert(double latitude, double longitude,
            String name, int rad) {

        Intent intent = new Intent("idontknowyvusethis");
        PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0,
                intent, 0);

        lm.addProximityAlert(latitude, longitude, rad, -1, proximityIntent);
        // will be used to generate an Intent to fire when entry to or exit from
        // the alert region is detected

        IntentFilter filter = new IntentFilter("idontknowyvusethis");
        registerReceiver(new proximityIntentReceiver(name, rad), filter);

    }

    public class MyLocationListener implements LocationListener {
        public void onLocationChanged(Location location) {
        }

        public void onStatusChanged(String s, int i, Bundle b) {
        }

        public void onProviderDisabled(String s) {
        }

        public void onProviderEnabled(String s) {
        }
    }

}

以下是Receiver类的代码:

package com.example.multigeofence;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.LocationManager;
import android.util.Log;
import android.widget.Toast;

public class proximityIntentReceiver extends BroadcastReceiver {
    private static final int NOTIFICATION_ID = 1000;
    private String name;
    private int rad;

    public proximityIntentReceiver(String name, int rad) {
        this.name = name;
        this.rad = rad;
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onReceive(Context context, Intent intent) {
            String key = LocationManager.KEY_PROXIMITY_ENTERING;
            String abc = "";

            Boolean entering = intent.getBooleanExtra(key, false);

            if (entering) {
                abc = "You are entering " + name + "! Beware!";
                Toast.makeText(context, "inside if : " + name, Toast.LENGTH_SHORT).show();
                Log.i("GeoGeo", "inside if : " + name);
            } else
                abc = "You are exiting " + name + "! Plz com bak :(";

            NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);

            Intent i = new Intent();
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                    i, 0);

            Notification notification = createNotification();
            notification.setLatestEventInfo(context, abc, "The radius is: "
                    + rad, pendingIntent);

            notificationManager.notify(NOTIFICATION_ID, notification);
        }


    private Notification createNotification() {
        Notification notification = new Notification();

        notification.icon = R.drawable.ic_launcher;
        notification.when = System.currentTimeMillis();

        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;

        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_LIGHTS;

        notification.ledARGB = Color.WHITE;
        notification.ledOnMS = 1500;
        notification.ledOffMS = 1500;

        return notification;
    }
}

这是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.multigeofence"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".GeoFenceService"
            android:enabled="true" />

    </application>

</manifest>

任何提示或帮助将不胜感激。谢谢:))

1 个答案:

答案 0 :(得分:0)

您应该只注册一个接收器而不是三个接收器。要找出触发通知的位置,您应该将一些数据添加到传递给getBroadcast的意图中,如下所示:

Intent intent = new Intent("idontknowyvusethis");
intent.putExtra( "fenceName", name );
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

此意图将在onReceive中返回给您:

String locationName = intent.getStringExtra( "fenceName" );