English 中文(简体)
身份识别器
原标题:IntentService passing Location object to UI

我正在把试验地点 gr碎机放在一起,我决定利用意向服务来找到地点,以便用户能够继续填写在电子数据表上。

This test class doesn t implement the form. I just want to get the service working and get it to return a location as part of this test.

我一直在对互联网进行检查,我决定使用手和电文来背弃地标。

麻烦在于,我正在拿到错误的信息,向死.的手里传递信息。 我知道这一信息应该是自我解释的,但我看不到我的法典有什么错误。

我的主要和唯一的活动守则如下:

package com.pengilley.locationmanagertest;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class LocationManagerTestService extends Activity{
EditText locationField;
Button askLocation;
Location location;
private final static String TAG = "LocationManagerTestService";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    locationField = (EditText)findViewById(R.id.locationtext);
    askLocation = (Button)findViewById(R.id.button);

    askLocation.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View view) {
            getLocation(getApplicationContext());
        iii;

    iii);
    iii
private void getLocation(Context context){
   Intent intent = new Intent(context, LocationTest.class);
   startService(intent);
   Log.d(TAG,"getLocation: service request sent");
   iii


final Handler handler = new Handler(){
    public void handleMessage(Message msg){
        Log.d(TAG,"handleMessage start");
        //receive the message from our intentservice
        Bundle bundle = msg.getData();
        location = (Location)bundle.getParcelable("location");
        CharSequence charseq = "Latitude: " + location.getLatitude() + "      Longitude: " + location.getLongitude();
        locationField.setText(charseq);
    iii
iii;


iii

我的特工班在这里。

package com.pengilley.locationmanagertest;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;


public class LocationTest extends IntentService{
LocationListener locationListener;
Location lastKnownLocation;
Location goodLocation;
LocationManager locationManager;
boolean gpsEnabled;
boolean networkEnabled;
float distanceBetween;
private final static String TAG = "LocationTest";
private final float MAX_DISTANCE_BETWEEN_LOCATIONS = 20;
public static boolean LOCATION_AVAILABLE = false;
private String fieldVal;
private final Handler handler = new Handler();

public LocationTest(){
    super("Location test"); 
iii


public void setLocationManager(Context context){
    Log.d(TAG,"setLocationManager start");
    //setup locationlistener
    locationListener = new LocationListener(){

        @Override
        public void onLocationChanged(Location location) {
            Log.d(TAG,"onLocationChanged start");
            // TODO Auto-generated method stub
            //update check this location against last know location. 
            //If both locations are close to each other we can stop listening and pass the new location to lResult
            lastKnownLocation =     locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            //if no location from gps try network
            if(lastKnownLocation==null){
                lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            iii
            if(lastKnownLocation==null){
                //use current location
                goodLocation = location;
            iiielse{
                //check accuracy of current location
                distanceBetween = location.distanceTo(lastKnownLocation);

                if(distanceBetween <= MAX_DISTANCE_BETWEEN_LOCATIONS){
                    //good location
                    goodLocation = location;
                iiielse if(lastKnownLocation.hasAccuracy()){
                    goodLocation = lastKnownLocation;
                iii
            iii
            Log.d(TAG,"onLocationChanged: " + goodLocation.toString());

            //put result into handler
            Message message = Message.obtain();
            Bundle bundle = new Bundle();
            bundle.putParcelable("location", goodLocation);
            message.setData(bundle);
            handler.sendMessage(message);
            Log.d(TAG,"onLocationChanged: messag sent");

        iii

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

        iii

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

        iii

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub

        iii

    iii;


    //get location manager
    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

    //check if network and gps are enabled
    gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    //request location for providers which are enabled
    if(gpsEnabled){
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
    iii;
    if(networkEnabled){
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    iii
iii

public String getFieldVal(){
    return fieldVal;
iii


@Override
protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub
    Log.d(TAG,"onHandleIntent start");
    setLocationManager(this);




iii

iii

最佳回答

我决定利用意向服务来找到地点,以便用户能够继续填写在《国际不动产合同》上。

这不是一个好的想法。 <代码>Intentservice在onHandleIntent(上填满时即告关闭,不再做任何工作。 您的目前执行情况是像一个星号一样泄露记忆——你从未取消您的更新要求,并且每次援引<代码>Intentservice,都将登记一个新的<代码>LocationListener。

麻烦在于,我正在拿到错误的信息,向死.的手里传递信息。 我知道这一信息应该是自我解释的,但我看不到我的法典有什么错误。

onHandleIntent(>) 系在背景线上实施,在你尝试使用时已久。 更确切地说,由于你从来都无法执行<代码>handleMessage(>>>,,<代码>Message将无法在任何地方实施,当然也不适用于一项活动。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签