2012-12-13 8 views
1

나는 내 앱용 GPS 서비스를 만들고 다른 목록 활동을 위해 GPS 서비스에서 얻은 데이터를 얻고 싶습니다. 문제는 목록에 표시 할 수 없다는 것입니다.android 서비스에서 데이터를 가져 오는 방법은 무엇입니까?

GPSservice을 시작하는 시작 코드 :

public class splash extends Activity 
{ 
    @Override 
    public void onCreate(Bundle icicle) 
    { 
     super.onCreate(icicle); 
     setContentView(R.layout.splash); 


     new Handler().postDelayed(new Runnable() { 
      public void run() { 
       startService(new Intent("com.example.geotask.gpsservice")); 
        Intent mainIntent = new Intent(splash.this, MainActivity.class); 
        splash.this.startActivity(mainIntent); 
        splash.this.finish(); 
      } 
     }, 1000); 
    } 

GPSservice 코드 :

public class GPSservice extends Service 
{ 
    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     return null; 
    } 
    public ArrayList<String> position = new ArrayList<String>(); 
    public ArrayList<Long> time=new ArrayList<Long>(); 
    public ArrayList<Integer> lat= new ArrayList<Integer>(); 
    public ArrayList<Integer> lon= new ArrayList<Integer>(); 
public void onCreate() 
{ 
    super.onCreate(); 


} 
public void onStart(Intent intent, int startID) 
{LocationManager locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE); 
String provider = locationManager.GPS_PROVIDER; 
Location location = locationManager.getLastKnownLocation(provider); 
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener); 
} 
private void additem(Location location){ 
    String latLongString = "Lat:" + location.getLatitude() + "\nLong:" + location.getLongitude(); 
    position.add(latLongString); 
    long t=location.getTime(); 
    time.add(t); 

} 

private void addgeo(Location location){ 
    int x=(int)location.getLatitude()*1000000; 
    int y=(int)location.getLongitude()*1000000; 
    lat.add(x); 
    lon.add(y); 


} 


LocationListener locationListener = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      additem(location); 
        addgeo(location); 
      Intent intent= new Intent("com.example.geotask.gpsdata"); 
      intent.putIntegerArrayListExtra("lat", (ArrayList<Integer>) lat); 
      intent.putIntegerArrayListExtra("lon", (ArrayList<Integer>) lon); 
      intent.putStringArrayListExtra("data", (ArrayList<String>) position); 
      sendBroadcast(intent); } 



     public void onProviderDisabled(String provider){ 


     } 

     public void onProviderEnabled(String provider) { 

     } 

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

목록 활동 코드 :

public class list extends Activity{ 
    ArrayList details = new ArrayList(); 
    private BroadcastReceiver gpsreceiver; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     IntentFilter intentFilter=new IntentFilter("com.example.geotask.gpsdata"); 
     intentFilter.addAction("com.example.geotask.gpsdata"); 
     gpsreceiver=new BroadcastReceiver() 
     { 
      @Override 
      public void onReceive(Context context, Intent intent) 
      { 
       List<String> ex=intent.getStringArrayListExtra("data"); 
       int i=0; 
       for(i=0; i<ex.size(); i++) 
       { 
        listdetails Detail = new listdetails(); 
        Detail.setIcon(R.drawable.ic_launcher); 
        Detail.setPlace(ex.get(i)); 
        Detail.setTime("2012.12.2"); 
        details.add(Detail); 
      } 
      } 
     }; 
     this.registerReceiver(gpsreceiver, intentFilter); 


     setContentView(R.layout.list); 
     ListView listView; 
     listView = (ListView)findViewById(R.id.listView1); 
     customlist Adapter = new customlist(details, this); 
     listView.setAdapter(Adapter); 


    } 
+0

처리기와 메신저를 사용하면 지연된 작업을 수행하고 인 텐트를 보내는 첫 번째 코드에서 내부 클래스가 필요하지 않습니다. –

+0

나를 위해 명확하게 설명해 줄 수있는 plz, 나는 안드로이드 programming.thx에 대한 신입생이다 :) – HeikiCyan

답변

0

당신은 당신의 수신기를 등록해야

registerReceiver(gpsreceiver, intentFilter); 

onDestroy의 등록을 잊지 마십시오.

+0

내가 manifest.xml에서 할 수 있습니까? 사촌 나는 BroadcastReceiver를 확장하는 새로운 클래스를 만들지 않았다. – HeikiCyan

+0

매니페스트에서 브로드 캐스트 리시버를 선언 할 수 있지만이 경우 BroadcastReceiver를 확장하는 새 클래스를 만들어야합니다. 리시버를 활동 라이프 사이클에 연결하려면 매니페스트가 아니라 registerReceiver를 사용해야합니다. –

+0

Thx 정보입니다. 코드를 수정하고 registerReceiver()를 추가했지만 문제는 여전히 존재합니다. 목록에는 아무것도 표시되지 않습니다. – HeikiCyan

관련 문제