2013-09-28 64 views
0

我想在用戶進入特定區域時收到通知,但它不工作,但我沒有收到任何錯誤!我在過去的一個星期裏呆在這裏,請和我合作!接近警報沒有通知

public class MainActivity extends FragmentActivity{ 


protected GoogleMap gMap; 
private LocationManager locationManager; 
TextView txt; 
double clat,clong; 

private static final String PROX_ALERT_INTENT ="com.example.map.ProximityAlert"; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    gMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 
    if (gMap == null) { 
      Toast.makeText(this, "Google Maps not available",   
     Toast.LENGTH_LONG).show();} 


    txt=(TextView) findViewById(R.id.text1); 



    locate(); 



    gMap.setMyLocationEnabled(true); 
    }  

private void locate() { 


    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    LocationListener locationListener = new LocationListener() { 
     public void onLocationChanged(Location location) { 

      clat=location.getLatitude(); 
      clong=location.getLongitude();   
     } 

     public void onStatusChanged(String provider, int status, Bundle extras) {} 
     public void onProviderEnabled(String provider) {} 
     public void onProviderDisabled(String provider) {} 
     }; 

     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 10, locationListener); 
     addProximityAlert(clat,clong); 

    } 
     private void addProximityAlert(double latitude, double longitude) { 

      Intent intent = new Intent(PROX_ALERT_INTENT); 

        PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT); 

        float POINT_RADIUS=10000; 
       long PROX_ALERT_EXPIRATION=10000; 
       locationManager.addProximityAlert(

         latitude, // the latitude of the central point of the alert region 

         longitude, // the longitude of the central point of the alert region 

         POINT_RADIUS, // the radius of the central point of the alert region, in meters 

         PROX_ALERT_EXPIRATION, // time for this proximity alert, in milliseconds, or -1 to indicate no expiration 

         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(PROX_ALERT_INTENT); 
       registerReceiver(new ProximityIntentReceiver(), filter); 
       } 
    } 

這裏是接近的接收機類,

public class ProximityIntentReceiver extends BroadcastReceiver { 

private static final int NOTIFICATION_ID = 1; 

    @SuppressWarnings("deprecation") 
    public void onReceive(Context context, Intent intent) { 

     String key = LocationManager.KEY_PROXIMITY_ENTERING; 

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



     if (entering) { 

      Log.d(getClass().getSimpleName(), "entering"); 

     } 

     else { 

      Log.d(getClass().getSimpleName(), "exiting"); 

     } 

     NotificationManager notificationManager = 
      (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0);  
     Notification notification = createNotification(); 
     notification.setLatestEventInfo(context,"Proximity Alert!", "You are near your point of interest.", pendingIntent); 
     notificationManager.notify(NOTIFICATION_ID, notification); 
    } 
    private Notification createNotification() { 
     Notification notification = new Notification(); 
     //notification.icon = R.drawable.image_1.jpg; 
     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.map" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="18" /> 

    <permission 
    android:name="com.example.map.permission.MAPS_RECEIVE" 
    android:protectionLevel="signature" /> 

    <uses-permission android:name="com.example.map.permission.MAPS_RECEIVE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission   android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
    <!-- 
    The following two permissions are not required to use 
    Google Maps Android API v2, but are recommended. 

    --> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 

    <uses-feature 
    android:glEsVersion="0x00020000" 
    android:required="true" /> 

    <application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 

    <activity 
     android:name="com.example.map.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> 




    <activity 
     android:name="com.example.map.Locate" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.Locate" /> 

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

    <receiver android:name="ProximityIntentReceiver" > 
    <intent-filter> 
     <action android:name="com.example.map" /> 
    </intent-filter> 
    </receiver> 

     <meta-data 
     android:name="com.google.android.maps.v2.API_KEY" 
     android:value="AIzaSyBeJoGKnhxYps-youI1xfMZn6S2G05l0PM" /> 

    </application> 

    </manifest> 

回答