0

I am trying to make a dashclock extension for my app. Basically my app's main activity reads data from the sensor and shows it. Now the dashclock extends DashClockExntension which extends Service. My doubt is, how should i make this service to ask data from the app's main class. If i am using an intent with startService(), then I am getting null values in the widget. How to get that data right before the widget displays it.

package com.thePaze.Ambiance;

import android.content.Intent;
import com.google.android.apps.dashclock.api.DashClockExtension;
import com.google.android.apps.dashclock.api.ExtensionData;

public class DashMain extends DashClockExtension {

    String temp, humidity;

    @Override
    protected void onUpdateData(int reason) {
        // TODO Auto-generated method stub



        publishUpdate(new ExtensionData().visible(true).status("Ambient").icon(R.drawable.ic_launcher).expandedTitle("Temp & Humidity").expandedBody("Temperature: " + temp+ "\nHumidity: " +humidity)
                .clickIntent(new Intent(DashMain.this, SensorMain.class))

        );

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        super.onStartCommand(intent, flags, startId);

        temp = intent.getStringExtra(temp);
        humidity = intent.getStringExtra(humidity); 


        return START_STICKY;

    }

}   

1 Answers1

2

From the documentation:

onUpdateData(int) will by default be called roughly once per hour, but extensions can use methods such as setUpdateWhenScreenOn(boolean) and addWatchContentUris(String[]) to request more frequent updates.

If you click that link, you'll see this documentation for addWatchContentUris(String[])

Requests that the main DashClock app watch the given content URIs (using ContentResolver.registerContentObserver) and call this extension's onUpdateData(int) method when changes are observed. This should generally be called in the onInitialize(boolean) method.

Are you familiar with content providers? It is a method of sharing data between activities. Activities who watch a content provider get notified of updates. It is the only way that I am aware of to increase the frequency of dashclock updates.


EDIT: adding more specifics

Add a content provider entry to your androidmanifest.xml. Note that the authority should start with your package name, but it doesn't have to, it's really just an arbitrary identifier.

<provider android:authorities="com.mypackage.myapp.myprovider"
    android:name=".MyProviderClassName"
    android:exported="true"/>

Now create a class called MyProviderClassName that extends ContentProvider. You do not even need to implement any of the methods, just return the default values.

class MyProviderClassName extends ContentProvider {
    @Override
    public boolean onCreate() {
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        return null;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }
}

Now, in your extension class in onInitialize(), add the following:

addWatchContentUris(new String[] { "content://" + YOUR_AUTHORITY_FROM_MANIFEST }

Whenever you want the extension to update from within your activity, simply call the following method:

getContentResolver().notifyChange(Uri.parse("content://" + YOUR_AUTHORITY_FROM_MANIFEST), null);
Jacob Tabak
  • 8,044
  • 3
  • 24
  • 32