2012-08-13 156 views
0

我的應用程序的工作原理如下:我的位置服務無法正常工作

  • 我有一個位置監聽器來的每一次活動
  • 我的位置監聽器(它包含位置監聽器)上的位置發送廣播改變
  • 所有活動有廣播接收器,允許他們在地圖上設置位置

它必須這樣工作,但即使我的服務沒有啓動。我已經厭倦了編寫這個應用程序,所以我把我的代碼放在這裏,也許我忽略了一些簡單的問題。我需要與該主題幫助:

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; 
     } 
    } 
} 

一個活動:(我試圖啓動或綁定服務,但沒有方法似乎是工作)。

// 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> 

回答

1

的解決辦法是保持業務和活動,它使用它在一個封裝中。 這是一個小問題,但可能是一個人的大問題。