1

Hi Everyone, I am having a problem with launching an activity from my home screen widget.And before some one marked this as a duplicate questions , please know that I have looked around and tried the solutions mentioned in other similar questions.Especially this one Launching activity from widget

My problem is simple enough , I want to launch an activity from my home screen widget , and it is not working. Note : The Activity launches from the widget once(for the first time) after a fresh install of the application.

Here is the code for the widget provide, the configure activity and the relevant manifest entry.let me know if you need to see any more bits of code.

the configure activity:

    public class SingleNoteConfigure extends ListActivity  {


private NotesDbAdapter mDbHelper;
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
public static String ACTION_WIDGET_LIST = "ActionReceiverList";
public static String ACTION_WIDGET_NEW = "ActionReceiverNew";



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //set the result to canceled to allow the user
    //to change their mind mid widget configure
    setResult(RESULT_CANCELED);
    Log.d("MYTAG", "in the onCreate of of the widget configure");

    //set the layout file for the widget configure
    setContentView(R.layout.notes_list_config);

    //using the action bar title for user instruction
    setTitle("Select note to display on the widget");

    // Find the widget id from the intent. 
    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    if (extras != null) {
        mAppWidgetId = extras.getInt(
                AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
    }
    Log.d("MYTAG", "in the onCreate");
    //stuff to get the database to open
    mDbHelper = new NotesDbAdapter(this);
    mDbHelper.open();

    //call the method that fills the list view
    fillData();

}

 @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));



        RemoteViews views = new RemoteViews(this.getPackageName(), R.layout.singlenote_widget);
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this); 
        appWidgetManager.updateAppWidget(mAppWidgetId, views);    



        Log.d("MYTAG", "in the onListItemClick....");

        loadData(title, text, id);//here id is the row id of the selection;it is returned by the onListItemSelect 

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



 void loadData(String title, String text, Long Id) {

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this); 
    Log.d("MYTAG", "in the load data....");
    SingleNote.updateWidget(this, appWidgetManager, mAppWidgetId, title, text);
    NotesDbAdapter.updateWidgetId(mAppWidgetId,Id);

}

private void fillData() {
    Cursor notesCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(notesCursor);
    Log.d("MYTAG", "in the fill data  of the widget configure");
    // Create an array to specify the fields we want to display in the list (TITLE and DATE)
    String[] from = new String[]{NotesDbAdapter.KEY_TITLE,NotesDbAdapter.KEY_DATE};


    // and an array of the fields we want to bind those fields to (title and date)
    int[] to = new int[]{R.id.title,R.id.date};


    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.notes_row1, notesCursor, from, to);

    setListAdapter(notes);


}  
}

The widget provider:

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

        Log.d("MYTAG", "in the onUpdate");

        Intent intent = new Intent(context, Notepadv3.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.singlenote_widget);
        views.setOnClickPendingIntent(R.id.single_note_text, pendingIntent);  

        // Push update for this widget to the home screen

        ComponentName thisWidget = new ComponentName(context, SingleNote.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        manager.updateAppWidget(thisWidget, views);

    }
}

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

    try
    {

    Log.d("MYTAG", "in the onReceive....");
        String action = intent.getAction(); 
        Log.d("MYTAG", "in the onReceive....line 2");
        Bundle extras = intent.getExtras(); 
        Log.d("MYTAG", "in the onReceive....after bundle");
        String title = extras.getString("title");
        Log.d("MYTAG", "in the onReceive....after title");
        String text = extras.getString("body");
        Log.d("MYTAG", "in the onReceive....after text");
        Log.d("MYTAG", "@ the point of int");
        int id = extras.getInt("widget_id");

        Log.d("MYTAG", action+ title + text + id);

        if (action != "rakshak" && action.equals(UPDATE_ACTION)) { 
            final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 

            if (id > 0)
            {
                updateWidget(context, appWidgetManager, id ,title ,text);
            }
            else {
                return;

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

    }
    catch (Exception e)
    {
        Log.e("STACKTRACE_TAG", "STACKTRACE");
        Log.e("STACKTRACE_TAG", Log.getStackTraceString(e));
    }


}

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

    Log.d("MYTAG", "in the updatewidget method in the siglenote widget....");
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.singlenote_widget);
    views.setTextViewText(R.id.single_note_title, title);
    views.setTextViewText(R.id.single_note_text, text);
    appWidgetManager.updateAppWidget(appWidgetId, views);



}
    }

The stuff from the android manifest:

  <receiver android:name=".SingleNote" 
                android:label="Y.A.N (single note widget)"
                android:enabled="@bool/is_GB">
        <intent-filter> 
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> 
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
        <meta-data android:name="android.appwidget.provider" 
                   android:resource="@xml/singlenote_widget_provider" />
      </receiver>  

Here are my 2 main questions:

  1. how do I get the activity to launch
  2. And how do I get the widget Id of the widget that is being taped?

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

Community
  • 1
  • 1
DrkStr
  • 1,752
  • 5
  • 38
  • 90
  • Did you add the `android:configure` item in singlenote_widget_provider.xml? – Karakuri Jul 12 '13 at 19:55
  • you want to launch the `activity` which is `configure activity` from the widget home screen? i.e. clicking home screen widget to open `configure activity` – Rohan Kandwal Jul 12 '13 at 20:28
  • yes, I have the configure activity listed in the widget_provider.xml – DrkStr Jul 13 '13 at 05:56
  • no I do not want to launch the configure activity of the widget , I want to launch an activity form inside the app – DrkStr Jul 13 '13 at 05:57

1 Answers1

0

I don't see your activity listed in the manifest, but you didn't paste the whole file. Make sure the activity exists there.

Here is a working snippet of the pending intent that I use in many of my widgets to launch activities:

Intent configIntent = new Intent(context, ActivityToBeLaunched.class);
        configIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        // Make the pending intent unique...
        configIntent.setData(Uri.parse(configIntent.toUri(Intent.URI_INTENT_SCHEME)));
        PendingIntent pendIntent = PendingIntent.getActivity(context, 0, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.widgetBack, pendIntent);
    }

One other thing; I don't see a return views; anywhere. Are you handling that elsewhere?

suomi35
  • 1,069
  • 12
  • 11
  • hey , this is a stupid question . But what are returne views ? When I launch an activity from a widget , the activity just launches and does its thing .right ? And adding the configIntent.setData(Uri.parse(configIntent.toUri(Intent.URI_INTENT_SCHEME))); has note made a difference. – DrkStr Jul 13 '13 at 08:10