0

我正在尝试编写一个简单的应用程序,用服务返回的 Lat/Lng 值更新 MainActivity。但它总是返回空值。我已添加权限并将 TheService.java 作为服务添加到 AndroidManifest.xml 文件中...请告诉我哪里出错了。

MainActivity.java

public class MainActivity extends Activity {

TextView tv1, tv2;
IntentFilter filter;
receive rec;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv1 =(TextView)findViewById(R.id.textView1);
    tv2 = (TextView)findViewById(R.id.textView2);
    filter = new IntentFilter("Updated");
    rec = new receive();
    Intent gps_int = new Intent(this,TheService.class);
    startService(gps_int);      

}

@Override
public void onPause()
{
    super.onPause();
    unregisterReceiver(rec);
}

@Override
public void onResume()
{
    super.onResume();
    rec = new receive();
    registerReceiver(rec, filter);      
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public class receive extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        tv1.setText(intent.getExtras().getString("lat"));
        tv2.setText(intent.getExtras().getString("lon"));
        Toast.makeText(getApplicationContext(), "Toast Executed", Toast.LENGTH_SHORT).show();
        Toast.makeText(getApplicationContext(), "BR Latitude "+intent.getExtras().getString("lat"),Toast.LENGTH_SHORT).show();
    }

}}

服务.java

public class TheService extends Service implements LocationListener {
LocationManager lm;
Location loc;
double lat = 0;
double lon = 0;
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    lm=(LocationManager)getSystemService(LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
    loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    onLocationChanged(loc);
     return START_STICKY;
}
@Override
public void onCreate()
{


}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    updateui(location);
}

private void updateui(Location location) {
    // TODO Auto-generated method stub
    lat = location.getLatitude();
    lon = location.getLongitude();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Intent gps_intent = new Intent("Updated");
    gps_intent.putExtra("lat", lat);
    gps_intent.putExtra("lon", lon);
    sendBroadcast(gps_intent);
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}}
4

1 回答 1

1
  1. 您应该为该位置使用Google Play 服务,这样更容易处理。
  2. 你还需要什么服务?在你的 Activity 中有一个位置监听器是完全可以的。
  3. 如果您想保留服务,则将活动绑定到它,而不是使用广播。
于 2013-07-16T16:50:56.597 回答