调用方法从服务更新活动中的TextView

时间:2012-04-20 23:27:10

标签: android service android-activity

我有一个项目应该从位置管理器接收当前位置,然后在向用户显示这些坐标的活动中更新文本视图。我们希望位置管理器在后台作为服务运行。目前,即使我打开了活动,每当位置更新并且从服务中调用更新textViews的方法时,我们都会关闭一个强制关闭。我也无法将方法更改为静态方法,因为静态与textViews不兼容。下面是服务,被调用的方法和堆栈跟踪。

在位置更改时调用的服务方法

        public void onLocationChanged(Location location) {
            currentLocation = location;
            SplashActivity.historyList.add(currentLocation);
            Toast.makeText(getApplicationContext(), "Point added",
                    Toast.LENGTH_SHORT).show();
            //postpones the update until NavigatorActivity starts
            if (NavigatorStarted) {
            new NavigatorActivity().doActionFine(currentLocation);
            }
            // these lines are controversial...
            if (location.getAccuracy() > 1000 && location.hasAccuracy())
                locationManager.removeUpdates(this);                          

        }

活动中的方法,用于通过准确的GPS信号或粗略信号更新具有新位置的文本视图。使用do action方法从LocationService

中使用当前位置的参数调用服务
public void updateLocationTextViewsFine(Location currentLocation) {
    currentLatView = (TextView) findViewById(R.id.currentLat);
    currentLonView = (TextView) findViewById(R.id.currentLon);
    // update the textviews (labels)
    String lat = currentLocation.getLatitude() + " (accurate)";
    String lon = currentLocation.getLongitude() + " (accurate)";
    currentLatView.setText(lat);
    currentLonView.setText(lon);
}
public void updateLocationTextViewsCoarse(Location currentLocation) {
    currentLatView = (TextView) findViewById(R.id.currentLat);
    currentLonView = (TextView) findViewById(R.id.currentLon);
    // update the textviews (labels)
    String lat = currentLocation.getLatitude() + " (low accuracy)";
    String lon = currentLocation.getLongitude() + " (low accuracy)";
    currentLatView.setText(lat);
    currentLonView.setText(lon);
}

public  void doActionFine(Location currentLocation) {
    updateLocationTextViewsFine(currentLocation);
}


public void doActionCoarse(Location currentLocation) {
    updateLocationTextViewsCoarse(currentLocation);
}

收到新错误。

04-20 20:15:33.894: E/AndroidRuntime(4389): FATAL EXCEPTION: main
04-20 20:15:33.894: E/AndroidRuntime(4389): java.lang.NullPointerException
04-20 20:15:33.894: E/AndroidRuntime(4389):     at android.app.Activity.findViewById(Activity.java:1637)
04-20 20:15:33.894: E/AndroidRuntime(4389):     at com.example.UVAMapsProject.NavigatorActivity.updateLocationTextViewsFine(NavigatorActivity.java:341)
04-20 20:15:33.894: E/AndroidRuntime(4389):     at com.example.UVAMapsProject.NavigatorActivity.doActionFine(NavigatorActivity.java:360)
04-20 20:15:33.894: E/AndroidRuntime(4389):     at com.example.UVAMapsProject.LocationService$2.onLocationChanged(LocationService.java:116)
04-20 20:15:33.894: E/AndroidRuntime(4389):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:191)
04-20 20:15:33.894: E/AndroidRuntime(4389):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:124)
04-20 20:15:33.894: E/AndroidRuntime(4389):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:140)
04-20 20:15:33.894: E/AndroidRuntime(4389):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 20:15:33.894: E/AndroidRuntime(4389):     at android.os.Looper.loop(Looper.java:123)
04-20 20:15:33.894: E/AndroidRuntime(4389):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-20 20:15:33.894: E/AndroidRuntime(4389):     at java.lang.reflect.Method.invokeNative(Native Method)
04-20 20:15:33.894: E/AndroidRuntime(4389):     at java.lang.reflect.Method.invoke(Method.java:521)
04-20 20:15:33.894: E/AndroidRuntime(4389):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-20 20:15:33.894: E/AndroidRuntime(4389):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-20 20:15:33.894: E/AndroidRuntime(4389):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

这可以使用Binder完成:

http://developer.android.com/guide/topics/fundamentals/bound-services.html

然后在活动中按照上面文章中的示例进行操作。连接到服务后,请询问当前位置(保存在服务或首选项中)。然后你给一个将被调用的服务的监听器。

这里有一些示例代码。缺少Listener和LocalService类的实现。但只是为了表明我的意思。

public class BindingActivity extends Activity {
    LocalService mService;
    boolean mBound = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Bind to LocalService
        Intent intent = new Intent(this, LocalService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        // Unbind from the service
        if (mBound) {
            mService.removeListener(this);
            unbindService(mConnection);
            mBound = false;
        }
    }

    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();
            mBound = true;

            handleLocation(mService.getCurrentLocation());
            mService.addListener(BindingActivity.this);
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
    };
}

活动完成后,在取消绑定之前从服务中删除侦听器。

答案 1 :(得分:1)

new NavigatorActivity().doActionFine(currentLocation);

刚刚看到这个....你正在创建一个活动的新实例并在其上调用方法。这个新实例与为打开锁创建的活动不同,这是一个没有调用生命周期方法的新活动。我肯定会像其他答案中所示的那样转向Binder计划

相关问题