문제

AddProxImityAlert 메서드를 사용하는 Android 프로젝트에서 작업하고 있습니다. 이미이 방법을 알고 있으므로 위치 (위도, 경도) 및 주어진 반경에 의해 주어진 위치에 대한 근접 경고를 설정할 수 있으며 알림당신이 너무 가깝다면 당신은 당신에게.

그래서 나는 3 일 전에 그 일을하고 있었고 나는 다시 같은 문제를 다시 얻고있었습니다.

Bref : 이것은 나의 간단한 코드입니다.

# mainactivity.java

package com.example.proximityalert;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity implements LocationListener {

    LocationManager lm;

    //Defining Latitude & Longitude
    double lat=37.422006 ,long1=-122.084095;
    //Defining Radius
    float radius=1000;               

    //Intent Action 
    String ACTION_FILTER = "com.example.proximityalert";


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

       //i'm registering my Receiver First
       registerReceiver(new ProximityReciever(), new IntentFilter(ACTION_FILTER));

       //i'm calling ther service Location Manager
       lm=(LocationManager) getSystemService(LOCATION_SERVICE);

       //for debugging...
       lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this);

       //Setting up My Broadcast Intent
       Intent i= new Intent(ACTION_FILTER);
       PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), -1, i, 0);

       //setting up proximituMethod
       lm.addProximityAlert(lat, long1, radius, -1, pi);

   }

@Override
//just For debugging to See the distance between my actual position and the aproximit point  
public void onLocationChanged(Location newLocation) {

    Location old = new Location("OLD");
    old.setLatitude(lat);
    old.setLongitude(long1);

    double distance = newLocation.distanceTo(old);

    Log.i("MyTag", "Distance: " + distance);
}

@Override
public void onProviderDisabled(String arg0) {}

@Override
public void onProviderEnabled(String arg0) {}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}

}
.

# proximityReceiver.java

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 ProximityReciever extends BroadcastReceiver {

     @Override
     public void onReceive(Context context, Intent intent) {

         // Key for determining whether user is leaving or entering 
         String key = LocationManager.KEY_PROXIMITY_ENTERING;

         //Gives whether the user is entering or leaving in boolean form  
         boolean state = intent.getBooleanExtra(key, false);

          if(state){
                // Call the Notification Service or anything else that you would like to do here
                Log.i("MyTag", "Welcome to my Area");
                Toast.makeText(context, "Welcome to my Area", Toast.LENGTH_SHORT).show();
          }else{
              //Other custom Notification 
                Log.i("MyTag", "Thank you for visiting my Area,come back again !!");
                Toast.makeText(context, "Thank you for visiting my Area,come back again !!", Toast.LENGTH_SHORT).show();
          }
     }
}
.

* # 문제가 프로그램을 실행할 때, Broadcastreceiver (proximityReciever)가 근접 지점에 가까운 매우 중요한 경우에도 Broadcastreceiver (proximityReciever)가 시스템에 의해 호출되지 않아도 디버거가 두 위치 사이의 거리를 보여 주더라도<1000m : /

입니다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top