0

I am trying to update longitude and latitude on low frequency live card that supports contextual voice commands(Which is possible from XE 21.0 update).

Since my service renders the live card and listens to location updates, I am not sure how to add a contextual menu from a Service itself.The example from the official documentation shows them added to an Activity.

Here is my code,

public class Start_service extends Service {

    private static final String LIVE_CARD_TAG = "LiveCardDemo";
    public static double lati;
    public static double longi;
    public static float speed;
    private LiveCard mLiveCard;
    private RemoteViews mLiveCardView;
    private final Handler mHandler = new Handler();
    private final UpdateLiveCardRunnable mUpdateLiveCardRunnable =
        new UpdateLiveCardRunnable();
    private static final long DELAY_MILLIS = 3000;
    @Override
    public void onCreate() {
        super.onCreate();
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        String provider = mLocationManager.getBestProvider(criteria, true);
        boolean isEnabled = mLocationManager.isProviderEnabled(provider);
        if (isEnabled) {
        // Define a listener that responds to location updates
        LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
        // Called when a new location is found by the network location provider.
        if (location != null){
        Geocoder geocoder = new Geocoder(Start_service.this.getBaseContext(), Locale.getDefault());

        try {
            lati= location.getLatitude();
            longi=location.getLongitude();
            speed=location.getSpeed();
        }
        catch (IOException e) {
        System.out.println(e);
        e.printStackTrace();
        }
        }

    }
        }


        // Register the listener with the Location Manager to receive location updates
        mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
        }
    }

This is my code for rendering low frequency live cards,this is also in the above service class, Now how do i add a contextual voice menu to it?

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        if (mLiveCard == null) {

            // Get an instance of a live card
            mLiveCard = new LiveCard(this, LIVE_CARD_TAG);

            // Inflate a layout into a remote view
            mLiveCardView = new RemoteViews(getPackageName(),
                R.layout.main_layout);
            // Set up the live card's action with a pending intent
            // to show a menu when tapped
            Intent menuIntent = new Intent(this, Sample.class);
            menuIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_CLEAR_TASK);
            mLiveCard.setAction(PendingIntent.getActivity(
                this, 0, menuIntent, 0));
            // Set up initial RemoteViews values        
            mLiveCardView.setTextViewText(R.id.latitude_value,
                    getString(R. id.latitude_value));
            mLiveCardView.setTextViewText(R.id.longitude_value,
                    getString(R.id.longitude_value));
            mLiveCardView.setTextViewText(R.id.speed_value,
                    getString(R.id.speed_value));
            // Publish the live card
            mLiveCard.setVoiceActionEnabled(true);
            mLiveCard.publish(PublishMode.REVEAL);

            // Queue the update text runnable
            mHandler.post(mUpdateLiveCardRunnable);
        }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        if (mLiveCard != null && mLiveCard.isPublished()) {
          //Stop the handler from queuing more Runnable jobs
            mUpdateLiveCardRunnable.setStop(true);

            mLiveCard.unpublish();
            mLiveCard = null;
        }
       /// mlocManager.removeUpdates(mlocListener);
        super.onDestroy();
    }

    /**
     * Runnable that updates live card contents
     */
    private class UpdateLiveCardRunnable implements Runnable{

        private boolean mIsStopped = false;

        /*
         * Updates the card with a fake score every 30 seconds as a demonstration.
         * You also probably want to display something useful in your live card.
         *
         * If you are executing a long running task to get data to update a
         * live card(e.g, making a web call), do this in another thread or
         * AsyncTask.
         */
        public void run(){
            if(!isStopped()){


                mLiveCardView.setTextViewText(R.id.latitude_value,
                    String.valueOf(lati));
                Log.e("updated", "updated latitude...............");
                mLiveCardView.setTextViewText(R.id.longitude_value,
                    String.valueOf(longi));
                mLiveCardView.setTextViewText(R.id.speed_value,
                        String.valueOf(speed));
                // Always call setViews() to update the live card's RemoteViews.
                mLiveCard.setViews(mLiveCardView);
                // Queue another score update in 30 seconds.
                mHandler.postDelayed(mUpdateLiveCardRunnable, DELAY_MILLIS);
            }
        }
ErstwhileIII
  • 4,829
  • 2
  • 23
  • 37
Prasanna Aarthi
  • 3,439
  • 4
  • 26
  • 48

1 Answers1

1

You can model this similarly to how the Compass sample does it:

  1. Implement an IBinder in your service that has a method to update the live card.
  2. Have your menu activity (which handles the contextual voice commands on the live card) bind to the service and get a reference to that binder.
  3. Call the appropriate binder method when the menu item is selected to ask the service to update the live card.
Tony Allevato
  • 6,429
  • 1
  • 29
  • 34