2011-11-11 3 views
12

Android 애플리케이션에 문제가 있습니다. 나는 서버에 10 분 간격으로 배경 GPS 위치를 보낼 응용 프로그램이 필요하다. (여기에는 WCF 서비스가있다.) 응용 프로그램은 닫힌 경우에도 데이터를 보내야합니다. 그래서 나는 서버에 시간을 보내는 서비스 (테스트 용)를 만들었습니다. 그러나 내 서비스에서 GPS 위치를 가져 오는 코드를 삽입하면 모든 것이 실패합니다 (예기치 않게 중지됨).백그라운드 서비스가 서버에 GPS 위치를 전송해야합니다.

나는 어떤 수신기 나 무언가를 넣어야 할 리드가 있지만 지금까지는 아무 것도 작동하지 않습니다. 그래서 누군가가 나를 도울 수 있다면 나는 매우 기뻐할 것입니다.

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

    <uses-sdk android:minSdkVersion="4" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:label="@string/app_name" 
      android:name=".ETMGPSServiceActivity" > 
      <intent-filter > 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 
    <application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:name=".MyApplication" > 
    <service 
     android:enabled="true" 
     android:name=".MonitorService" > 
    </service> 
    <receiver 
     android:enabled="true" 
     android:name=".LocationReceiver" > 
     <intent-filter > 
      <action android:name="com.biromatik.intent.action.LOCATION" /> 
     </intent-filter> 
    </receiver> 
</application> 


    <uses-permission android:name="android.permission.INTERNET"></uses-permission> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

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




</manifest> 
+0

"내 서비스가 모두 실패합니다 (예기치 않게 중지됨)"-> 무엇이 오류입니까? – Hoff

+0

CatLog에서 11-11 15 : 25 : 49.541 : E/AndroidRuntime (229) : 잡히지 않은 핸들러 : 잡히지 않은 예외로 인해 스레드 메인이 종료 됨 –

답변

33

나는 당신이 요청한 것과 동일한 일을하기 위해이 작업을 응용 프로그램에서 수행했습니다.

@Override 
public void onStart(Intent intent, int startId) { 

    super.onStart(intent, startId); 
    addLocationListener(); 
} 

private void addLocationListener() 
{ 
    triggerService = new Thread(new Runnable(){ 
     public void run(){ 
      try{ 
       Looper.prepare();//Initialise the current thread as a looper. 
       lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

       Criteria c = new Criteria(); 
       c.setAccuracy(Criteria.ACCURACY_COARSE); 

       final String PROVIDER = lm.getBestProvider(c, true); 

       MyLocationListener myLocationListener = new MyLocationListener(); 
       lm.requestLocationUpdates(PROVIDER, 600000, 0, myLocationListener); 
       Log.d("LOC_SERVICE", "Service RUNNING!"); 
       Looper.loop(); 
      }catch(Exception ex){ 
       ex.printStackTrace(); 
      } 
     } 
    }, "LocationThread"); 
    triggerService.start(); 
} 

public static void updateLocation(Location location) 
{ 
    Context appCtx = MyApplication.getAppContext(); 

    double latitude, longitude; 

    latitude = location.getLatitude(); 
    longitude = location.getLongitude(); 

    Intent filterRes = new Intent(); 
    filterRes.setAction("xxx.yyy.intent.action.LOCATION"); 
    filterRes.putExtra("latitude", latitude); 
    filterRes.putExtra("longitude", longitude); 
    appCtx.sendBroadcast(filterRes); 
} 


class MyLocationListener implements LocationListener 
{ 

    @Override 
    public void onLocationChanged(Location location) 
    { 
     updateLocation(location); 
    } 
} 

참고 : 내 서비스에서

나는이 추가 내가) (MyApplication.getAppContext를 사용하는 것이 참조; 그건 내 자신의 Application 클래스를 사용하여 컨텍스트를 추가하기 때문입니다. 그리고 나는이와 브로드 캐스트 리시버를 가지고 :

public class LocationReceiver extends BroadcastReceiver { 

    double latitude, longitude; 

    @Override 
    public void onReceive(final Context context, final Intent calledIntent) 
    { 
     Log.d("LOC_RECEIVER", "Location RECEIVED!"); 

     latitude = calledIntent.getDoubleExtra("latitude", -1); 
     longitude = calledIntent.getDoubleExtra("longitude", -1); 

     updateRemote(latitude, longitude); 

    } 

    private void updateRemote(final double latitude, final double longitude) 
    { 
     //HERE YOU CAN PUT YOUR ASYNCTASK TO UPDATE THE LOCATION ON YOUR SERVER 
    } 
} 

Application 클래스 : 매니페스트에서 또한

public class MyApplication extends Application { 


    private static Context context; 

    /* (non-Javadoc) 
    * @see android.app.Application#onCreate() 
    */ 
    @Override 
    public void onCreate() {    
     MyApplication.context=getApplicationContext(); 
    } 

    public static Context getAppContext() { 
     return MyApplication.context; 
    } 
} 

당신이 모두를 추가해야하는 경우 감지 할 수

<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" 
    android:name=".MyApplication" > 

    //YOUR ACTIVITIES HERE... 

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

    <receiver 
     android:enabled="true" 
     android:name=".LocationReceiver" > 
     <intent-filter > 
      <action android:name="xxx.yyy.intent.action.LOCATION" /> 
     </intent-filter> 
    </receiver> 
</application> 

방법 서비스가 실행 중입니다. 첫 번째 작업에서이 작업을 수행합니다.

,

은 분명히 당신은 서비스 (ONSTART) 또는하지 (들의 OnDestroy)를 실행하는 경우 참/거짓에 locationService 변수를 설정 된 SharedPreferences (설정)를 제어 할 필요가있다.

+0

완벽 해 보이지만 여전히 문제가 있습니다.이 xxx.yyy.intent. .. 대신 무엇을 넣어야하고 그 MyApplication.getCOntext()에 문제가 있습니다, 다른 방법이 있습니까? –

+0

내가 말했듯이 xxx.yyy.intent.action.LOCATION은 원하는대로 애플리케이션 이름 (예 : 패키지 이름)을 추가하고 intent.action을 추가하여 Application에서 확장 한 클래스를 만들어야한다. .LOCATION에 대한 자세한 내용은 .. 응용 프로그램 클래스로 내 대답을 편집합니다. – SERPRO

+0

지금 확인 : W/ActivityManager (51) : 서비스를 시작할 수 없습니다. 의도 {act = com.biromatik.GPS.MonitorService} : 찾을 수 없습니다. 지금 그 서비스를 시작하는 방법을 말해 줄 수 있습니까? –

관련 문제