Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending data to my (homescreen) widget

I am having some trouble sending data(strings) from my activity to my appWidgetProvide class.

I have a method called updateWidget. This gets called by the widget configure activity when the widget first gets placed in the home screen .This method is also called by the onReceive method when it receives data from one of my activities when the user enters data into that activity.

Here is my problem : the widget get placed in the home screen with all the data in the right place. But when my activity sends data the onReceive (using intents and extras) the data does not come through.I just get a empty string on the extras.getString .

I have used intents to send data between activities before , do I need to do something different when I send data to a widget provide class? Or am I just being stupid and missing something obvious ?

I have attached (what I think are the) relevant bits of code. Let me know if you need any clarification or any more of the code.

Thanks for taking the time to read this and for any help that you can give,

Cheers Rakshak

the onListItemClick in the widget configure class.

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        Cursor note = mDbHelper.fetchNote(id);
        startManagingCursor(note);
        String title = note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE));
        String text = note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY));
        loadData(title);


        Intent resultValue = new Intent();
        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);            
        setResult(RESULT_OK,resultValue);   
        finish();
 }



 void loadData(String title) {

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this); 

    SingleNote.updateWidget(this, appWidgetManager, mAppWidgetId, title);

}

The intent that sends data to the onReceive (this is in one of my activity classes)

private void updateWidget() { 
    Intent i = new Intent(this, SingleNote.class); 
    i.setAction(SingleNote.UPDATE_ACTION); 
    Toast.makeText(getApplicationContext(),mTitleText.getText()+"from the activity",
            Toast.LENGTH_SHORT).show();//This works just fine
    i.putExtra("title", mTitleText.getText());
    sendBroadcast(i); 

}

My widget provide class

public class SingleNote extends AppWidgetProvider {

public static String UPDATE_ACTION = "ActionUpdateSinglenoteWidget";
private static NotesDbAdapter mDbHelper;



public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int N = appWidgetIds.length;

    // Perform this loop procedure for each App Widget that belongs to this provider
    for (int i=0; i<N; i++) {
        int appWidgetId = appWidgetIds[i];



     // Tell the AppWidgetManager to perform an update on the current app widget
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.singlenote_widget);
        appWidgetManager.updateAppWidget(appWidgetId, views);




    }
}

@Override 
public void onReceive(Context context, Intent intent) { 

        String action = intent.getAction(); 
        Bundle extras = intent.getExtras(); 
        String title1 = extras.getString("title");//this value does not come through
        Toast.makeText(context, title1,Toast.LENGTH_LONG).show();//this gives an empty space

        if (action != null && action.equals(UPDATE_ACTION)) { 
                final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 
                ComponentName name = new ComponentName(context, SingleNote.class);
                int[] appWidgetId = AppWidgetManager.getInstance(context).getAppWidgetIds(name);
                final int N = appWidgetId.length;
                if (N < 1)
                {
                    return ;
                }
                else {
                    int id = appWidgetId[N-1];
                    updateWidget(context, appWidgetManager, id ,title1);
                }
        } 

        else { 
                super.onReceive(context, intent); 
        } 
} 



static void updateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, String title){

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.singlenote_widget);
    views.setTextViewText(R.id.single_note_title, title);
    appWidgetManager.updateAppWidget(appWidgetId, views);



}
like image 669
DrkStr Avatar asked Jul 02 '13 20:07

DrkStr


1 Answers

I suggest to try replacing i.putExtra("title", mTitleText.getText()); in updateWidget() with i.putExtra("title", mTitleText.getText().toString());

String title1 = extras.getString("title");

expects string, and mTitleText.getText() returns Editable - this is likely a mismatch

like image 190
amoiseyev Avatar answered Sep 20 '22 01:09

amoiseyev