2013-10-08 3 views
0

내 응용 프로그램이 1 초에 1 회 GPS 업데이트를받습니다. 또한 1 초에 한 번 Wifi 연결의 RSSI 값을 가져옵니다. 내가 직면하고있는 문제는 BroadcastReceiver가 결코 호출되지 않는다는 것입니다. 누군가 내가 잘못하고있는 것을 지적 할 수 있습니까? 다른 사이트뿐만 아니라 많은 stackoverflow 질문을 보았지만 내가 뭘 잘못하고 있는지 알 수는 없습니다. 내 코드는 다음과 같습니다 :BraodcastReceiver가 호출되지 않았습니다.

final String locAction = "com.android.ping.STARTGPS"; 
PendingIntent pIntent; 

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Intent intent = new Intent(locAction); 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

    if (!isGPSEnabled) { 
     Log.i(TAG,"PING: GPS not enabled"); 
    } else { 
     Log.i(TAG,"PING: GPS enabled"); 
     pIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_time_BW_Updates, MIN_Distance_Change_For_Updates, pIntent); 
     registerReceiver(myWifiReceiver, new IntentFilter(locAction)); 
     Log.i(TAG,"PING: adding GPS status listener"); 
    } 
} 

브로드 캐스트 리시버 :

protected BroadcastReceiver myWifiReceiver = new BroadcastReceiver() 
{ 
    @Override 
    public void onReceive(Context context, Intent intent){ 
     Log.i(TAG, "PING: inside onReceive"); 
     if (intent.getAction().equals(locAction)){ 
      String locationKey = LocationManager.KEY_LOCATION_CHANGED; 
      Location location = (Location) intent.getExtras().get(locationKey); 
      Log.i(TAG, "PING: location is " + location.getLatitude() + ", " + location.getLongitude()); 
      setLocation(location.getLatitude(), location.getLongitude()); 

      try { 
       WifiInfo(); //Calls the function that gets the RSSI 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
}; 

의도가 등록되지 않은 여기에 있습니다 :

@Override 
protected void onStop() { 
    super.onStop(); 
    locationManager.removeUpdates(pIntent); 
    unregisterReceiver(myWifiReceiver); 
} 

내 매니페스트 파일, 나는 브로드 캐스트 리시버를 등록 여기서

<receiver android:name="PingActivity" > 
     <intent-filter> 
      <action android:name="com.android.ping.STARTGPS" /> 
     </intent-filter> 
</receiver> 

EDIT 1 : 또한 앱에 IP 주소를 하드 코드하여 2 개의 다른 장치에 배포해야한다고 덧붙여 야합니다. IP 주소에 따라 장치는 서버 또는 클라이언트 스레드를 시작합니다. 코드의이 부분은 당신이 myWifiReceiver이 행해져 Yout이 PingActivity로 이름을 변경해야한다라고 이름 PingActivity 및 구현 클래스와 매니페스트에 broadcast를 선언

+0

"내 응용 프로그램은 GPS 업데이트를 1 초에 한 번받습니다."- GPS는 GPS 수신기가 수정 사항을 얻을 수있는 속도를 보장하지 않습니다. – CommonsWare

+0

동적으로 처리하는 경우 Manifest에 등록 할 필요가 없습니다. BTW이 기술을 사용해보십시오. http://developer.android.com/guide/topics/location/strategies.html –

+0

@CommonsWare - 예, 독립형 GPS 앱을 이미 테스트했습니다. 괜찮은 순간에 가까운 업데이트가 나옵니다. – Sarvavyapi

답변

1

주를 브로드 캐스트 리시버를 등록하고, 또한 브로드 캐스트 a를 만들 직후 다음 별도의 수업.

+0

브로드 캐스트를 매니페스트에 추가하는 것은 내 문제를 해결하기위한 시도 중 하나 였지만 이제는 내 앱에 등록하기 때문에 제거했습니다. 나 또한 방송을 별도의 클래스로 만들려고했는데 작동하지 않았다. – Sarvavyapi

+0

당신이 선택해야하는 접근 방식에 따라 꼭해야 할 일이 있습니다. http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html에서 볼 수 있습니다. – xhamr

관련 문제