0

私のアプリケーションは次のように動作します。

  • すべてのアクティビティに対して1つのロケーションリスナーがいます
  • 私のロケーションリスナー(ロケーションリスナーを含む)は、変更されたロケーションでブロードキャストを送信します
  • すべてのアクティビティには放送受信機があり、地図上に場所を設定できます

そのように動作している必要がありますが、私のサービスでさえ開始されません。私はこのアプリを書くのに疲れているので、ここにコードを配置します。おそらく、いくつかの単純な問題を見落としていました。私はそのトピックについて助けが必要です:

GPSサービス:

// package and imports

public class GeoService extends Service {
    LocationListener GPSLocationListener, NetworkLocationListener;
    LocationManager locationManager;
    private IBinder mBinder;

    @Override
    public void onCreate() {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        getPosition();
        return Service.START_REDELIVER_INTENT;
    }

    @Override
    public IBinder onBind(Intent intent) {
        getPosition();
        if (mBinder == null) mBinder = new GeoBinder();
        return mBinder;
    }

    private void getPosition() {
        GPSLocationListener = new LocationListener() {

            public void onLocationChanged(Location location) {
                sendPosition(location);
            }

            public void onProviderDisabled(String provider) {
                Toast.makeText(getApplicationContext(), provider + " is now disabled. Turn it on to find better GPS position.", Toast.LENGTH_SHORT).show();
            }

            public void onProviderEnabled(String provider) {
                Toast.makeText(getApplicationContext(), provider.toUpperCase() + " is now enabled. Waiting for better position..", Toast.LENGTH_SHORT).show();
            }

            public void onStatusChanged(String provider, int status, Bundle extras) {
                switch (status) {
                    case 0:
                    case 1:
                        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, NetworkLocationListener);
                        break;
                    case 2:
                        locationManager.removeUpdates(NetworkLocationListener);
                        break;
                }
            }
        };

        NetworkLocationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                sendPosition(location);
            }

            public void onProviderDisabled(String provider) {}

            public void onProviderEnabled(String provider) {}

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

        };

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, GPSLocationListener);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, NetworkLocationListener);

        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) alert();
    }

    private void alert() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
        builder.setMessage("Internet connection and GPS are not avaiable!.").setCancelable(false).setTitle("No location provider!").setIcon(android.R.drawable.ic_dialog_alert).setPositiveButton("Change settings", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                startActivity(new Intent(getApplicationContext(), SettingsActivity.class));
            }
        }).create().show();
    }

    void sendPosition(Location location) {
        Intent gpsPosition = new Intent();
        gpsPosition.putExtra("location", location);
        gpsPosition.setAction(Context.LOCATION_SERVICE);
        sendBroadcast(gpsPosition);
    }

    public GeoPoint getGeoPoint(final Location loc) {
        int lat = (int) (loc.getLatitude() * 1e6);
        int lon = (int) (loc.getLongitude() * 1e6);
        return new GeoPoint(lat, lon);
    }

    public GeoPoint getLastLocation() {
        GeoPoint lastKnownPoint;

        Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (lastKnownLocation == null) lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if (lastKnownLocation != null) lastKnownPoint = getGeoPoint(lastKnownLocation);
        else lastKnownPoint = new GeoPoint((int) (51.110582 * 1E6), (int) (17.031509 * 1E6));
        return lastKnownPoint;
    }

    @Override
    public void onDestroy() {
        locationManager.removeUpdates(GPSLocationListener);
        locationManager.removeUpdates(NetworkLocationListener);
    }

    public class GeoBinder extends Binder {
        public GeoService getService() {
            return GeoService.this;
        }
    }
}

アクティビティの1つ:(サービスを開始またはバインドしようとしましたが、どのメソッドも機能していないようです)。

// package and imports

public class ShareLocationActivity extends MapActivity {
    private final int PICK_CONTACT = 123;

    MapView.LayoutParams mapMarkerParams;
    MapController mapController;
    Location lastLocation;
    ImageView mapMarker;
    GeoPoint current;
    MapView map;

    private ServiceConnection mConnection;
    GeoService mService;
    boolean mBound = false;

    BroadcastReceiver locationReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            setPosition((Location) intent.getParcelableExtra("location"));
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_location);

        mConnection = new ServiceConnection() {
            public void onServiceConnected(ComponentName name, IBinder service) {
                GeoBinder binder = (GeoBinder) service;
                mService = binder.getService();
                mBound = true;

                mapController = map.getController();
                mapController.setCenter(mService.getLastLocation());
                mapController.setZoom(17);
            }

            public void onServiceDisconnected(ComponentName name) {
                mBound = false;
            }
        };

        Intent intent = new Intent(this, GeoService.class);
        startService(intent);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

        map = (MapView) findViewById(R.id.shareMapView);
        map.setBuiltInZoomControls(true);
        map.setSaveEnabled(true);

        mapMarker = new ImageView(getApplicationContext());
        mapMarker.setImageResource(android.R.drawable.presence_online);

        Button share = (Button) findViewById(R.id.shareActivityButton);
        share.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent contacts_intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                startActivityForResult(contacts_intent, PICK_CONTACT);
            }
        });

        IntentFilter filter = new IntentFilter();
        filter.addAction(Context.ACTIVITY_SERVICE);
        registerReceiver(locationReceiver, filter);
    }

    @Override
    public void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);

        switch (reqCode) {
            case (PICK_CONTACT):
                if (resultCode == Activity.RESULT_OK) {
                    if (current != null) sendSms(data);
                    else Toast.makeText(getApplicationContext(), "No GPS data yet..", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!mBound) {
            Intent intent = new Intent(this, GeoService.class);
            bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        }
    };

    @Override
    protected void onPause() {
        map.removeAllViews();
        if (mBound) {
            unbindService(mConnection);
            mBound = false;
        }
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        map.removeAllViews();
    }

    void setPosition(Location location) {

        map.removeAllViews();

        current = mService.getGeoPoint(location);
        mapMarkerParams = new MapView.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, current, MapView.LayoutParams.TOP_LEFT);
        map.addView(mapMarker, mapMarkerParams);
        mapController.setCenter(current);
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}

そして確かにマニフェスト:

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

        <uses-feature android:name="android.hardware.location" />
        <uses-feature android:name="android.hardware.location.gps" />

        <uses-sdk
            android:minSdkVersion="10"
            android:targetSdkVersion="15" />

        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
        <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
        <uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.READ_CONTACTS" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_SETTINGS" />
        <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

        <application
            android:icon="@drawable/ico"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar" >
            <uses-library android:name="com.google.android.maps" />

            <activity android:name=".MainActivity" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".SaveLocationActivity" />
            <activity android:name=".ShareLocationActivity" />
            <activity android:name=".RecordTrackActivity" />
            <activity android:name=".SettingsActivity" />

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

    </manifest>
4

1 に答える 1

1

解決策は、それを使用するサービスとアクティビティを1つのパッケージに保持することです。これは小さな問題ですが、誰かにとって大きな問題になる可能性があります。

于 2012-08-22T10:23:33.107 に答える