⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jsr179locationprovider.java

📁 关于J4ME J2ME实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			hasBeenAvailable = true;
			
			// Put the latest location information into a cubby hole
			// for JSR179LocationProvider.run() method to forward.  If
			// an older location event is still in the cubby hole it will
			// be replaced by this one.
			update.set( location );
		}
	}

	/**
	 * A background thread that posts location events to the registered
	 * <code>LocationListener</code>.  This keeps the main UI thread free
	 * for JSR 179 implementations that use it, like the BlackBerry.
	 * Implementations therefore can take their time handling the events
	 * without causing the application to become unresponsive or crash.
	 * 
	 * @see Runnable#run()
	 */
	public void run ()
	{
		try
		{
			while ( true )
			{
				// Block until a new event has been raised.
				Object o = update.get();
				
				// Check what kind of update it is.
				if ( o instanceof Integer )
				{
					// The provider is now unavailable.
					Integer i = (Integer)o;
					int newState = i.intValue();
					
					// Forward the state change event to the user's location listener.
					raiseStateChangeEvent( newState );
					
					// Should we reset the provider?
					//  BlackBerry implementations actually become completely unavailable when they
					//  say they are temporarily unavailable.  It is actually a signal saying the
					//  user must try resetting the provider later because it may become available
					//  again.  (To a BlackBerry "Out of Service" means the device does not have LBS.)
					//  This is covered in more detail in the BlackBerry Knowledge Base article at
					//  http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800703/How_To_-_Detect_when_GPS_is_no_longer_available_and_when_to_reset_the_LocationProvider.html?nodeid=1357467&vernum=0
					if ( hasBeenAvailable && (newState == javax.microedition.location.LocationProvider.TEMPORARILY_UNAVAILABLE) )
					{
						// Pause for a bit so we don't immediately try to get a location.
						//  Since this is happening on our one and only worker thread any events
						//  that may occur by the JSR 179 implementation, such as going back in
						//  service, will still happen.  They will not block either.  This just
						//  means the user's location implementation will not get updated for
						//  at least this long.
						Thread.sleep( interval * 1000 );
						
						// Make sure the provider is still unavailable after the sleep.
						if ( original.getState() == javax.microedition.location.LocationProvider.TEMPORARILY_UNAVAILABLE )
						{
							Log.info("Resetting the location provider to get another fix");
							
							// Reset the location provider so it tries to get another fix.
							reset();
						}
					}
				}
				else // ( o instanceof javax.microedition.location.Location )
				{
					// A new location event has been received;
					javax.microedition.location.Location l = (javax.microedition.location.Location)o;
					
					// Was the provider unavailable and is now available?
					if ( (lastState != javax.microedition.location.LocationProvider.AVAILABLE) && l.isValid() )
					{
						// Now the provider is available again.
						raiseStateChangeEvent( javax.microedition.location.LocationProvider.AVAILABLE );
					}
					
					// Forward the location event to the user's location listener.
					raiseLocationEvent( l );
				}
			}
		}
		catch (InterruptedException e)
		{
			// The application is exiting.
		}
	}
	
	/**
	 * Call when the location provider gives us a new state.
	 * 
	 * @param newState is the <code>javax.microedition.location.LocationProvider</code>
	 *  state code.
	 */
	private void raiseStateChangeEvent (int newState)
	{
		// Record the unavailable state.
		lastState = newState;
		
		// Forward to the application's listener.
		if ( locationListener != null )
		{
			int state = convertAvailabilityStatusCode( newState );
			
			try
			{
				locationListener.providerStateChanged( this, state );
			}
			catch (Throwable t)
			{
				// This is a programming error in the user's application.
				Log.warn("Unhandled exception in LocationProvider.providerStateChanged to " + state, t);
			}
		}
	}
	
	/**
	 * Call when the location provider gives us a new location.
	 * 
	 * @param location is the new location.
	 */
	private void raiseLocationEvent (javax.microedition.location.Location location)
	{
		// Forward to the application's listener.
		if ( locationListener != null )
		{
			org.j4me.bluetoothgps.Location l = convertLocation( location );
			
			try
			{
				locationListener.locationUpdated( this, l );
			}
			catch (Throwable t)
			{
				// This is a programming error in the user's application.
				Log.warn("Unhandled exception in LocationProvider.locationUpdated\n" + l, t);
			}
		}
	}
	
	/**
	 * Converts a JSR 179 <code>LocationException</code> object into a J4ME one.
	 * 
	 * @param jsr179 is the object to convert.
	 * @return The J4ME version of the object.
	 */
	protected static org.j4me.bluetoothgps.LocationException convertLocationException (javax.microedition.location.LocationException jsr179)
	{
		org.j4me.bluetoothgps.LocationException j4me = new org.j4me.bluetoothgps.LocationException( jsr179.getMessage() );
		return j4me;
	}

	/**
	 * Converts a J4ME <code>Criteria</code> object into a JSR 179 one.
	 * 
	 * @param j4me is the object to convert.
	 * @return The JSR 179 version of the object.
	 */
	protected static javax.microedition.location.Criteria convertCriteria (org.j4me.bluetoothgps.Criteria j4me)
	{
		javax.microedition.location.Criteria jsr179 = new javax.microedition.location.Criteria();

		jsr179.setAddressInfoRequired( j4me.isAddressInfoRequired() );
		jsr179.setAltitudeRequired( j4me.isAltitudeRequired() );
		jsr179.setCostAllowed( j4me.isAllowedToCost() );
		jsr179.setHorizontalAccuracy( j4me.getHorizontalAccuracy() );
		jsr179.setPreferredResponseTime( j4me.getPreferredResponseTime() );
		jsr179.setSpeedAndCourseRequired( j4me.isSpeedAndCourseRequired() );
		jsr179.setVerticalAccuracy( j4me.getVerticalAccuracy() );

		// Translate the power level constant.
		int power = j4me.getPreferredPowerConsumption();

		switch ( power )
		{
		case org.j4me.bluetoothgps.Criteria.NO_REQUIREMENT:
			power = javax.microedition.location.Criteria.NO_REQUIREMENT;
			break;
		case org.j4me.bluetoothgps.Criteria.POWER_USAGE_LOW:
			power = javax.microedition.location.Criteria.POWER_USAGE_LOW;
			break;
		case org.j4me.bluetoothgps.Criteria.POWER_USAGE_MEDIUM:
			power = javax.microedition.location.Criteria.POWER_USAGE_MEDIUM;
			break;
		case org.j4me.bluetoothgps.Criteria.POWER_USAGE_HIGH:
			power = javax.microedition.location.Criteria.POWER_USAGE_HIGH;
			break;
		}

		jsr179.setPreferredPowerConsumption( power );

		return jsr179;
	}

	/**
	 * Converts a JSR 179 <code>QualifiedCoordinates</code> object into a J4ME one.
	 * 
	 * @param jsr179 is the object to convert.
	 * @return The J4ME version of the object.
	 */
	protected static org.j4me.bluetoothgps.QualifiedCoordinates convertQualifiedCoordinates (javax.microedition.location.QualifiedCoordinates jsr179)
	{
		if ( jsr179 == null )
		{
			return null;
		}
		
		double latitude = jsr179.getLatitude();
		double longitude = jsr179.getLongitude();
		float altitude = jsr179.getAltitude();
		float horizontalAccuracy = jsr179.getHorizontalAccuracy();
		float verticalAccuracy = jsr179.getVerticalAccuracy();

		org.j4me.bluetoothgps.QualifiedCoordinates j4me = new org.j4me.bluetoothgps.QualifiedCoordinates( latitude, longitude, altitude, horizontalAccuracy, verticalAccuracy );
		return j4me;
	}

	/**
	 * Converts a JSR 179 <code>Location</code> object into a J4ME one.
	 * 
	 * @param jsr179 is the object to convert.
	 * @return The J4ME version of the object.
	 */
	protected static org.j4me.bluetoothgps.Location convertLocation (javax.microedition.location.Location jsr179)
	{
		LocationImpl j4me;
		
		if ( jsr179.isValid() )
		{
			org.j4me.bluetoothgps.QualifiedCoordinates j4meCoordinates = convertQualifiedCoordinates( jsr179.getQualifiedCoordinates() );
			j4me = new LocationImpl( j4meCoordinates, jsr179.getSpeed(), jsr179.getCourse(), jsr179.getTimestamp() );
		}
		else
		{
			j4me = new LocationImpl();
		}
		
		return j4me;
	}

	/**
	 * Converts a JSR 179 availability status code constant into a J4ME one.
	 * The status codes are contstants defined on the <code>LocationProvider</code>
	 * classes.
	 * 
	 * @param jsr179 is the availability status code to convert.
	 * @return The J4ME availability status code.
	 */
	protected static int convertAvailabilityStatusCode (int jsr179)
	{
		int j4me = -1;

		switch (jsr179)
		{
		case javax.microedition.location.LocationProvider.AVAILABLE:
			j4me = org.j4me.bluetoothgps.LocationProvider.AVAILABLE;
			break;
		case javax.microedition.location.LocationProvider.OUT_OF_SERVICE:
			j4me = org.j4me.bluetoothgps.LocationProvider.OUT_OF_SERVICE;
			break;
		case javax.microedition.location.LocationProvider.TEMPORARILY_UNAVAILABLE:
			j4me = org.j4me.bluetoothgps.LocationProvider.TEMPORARILY_UNAVAILABLE;
			break;
		}

		return j4me;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -