Frage

hi friends enter link description here

this link to create list view widget for my app, but here i want to add a button below the list view ,which on click will open my app, using the below code to acheive this,because it worked for me on other app, but not with this example, how to do that .

class widgetprovider

public class WidgetProvider extends AppWidgetProvider {

    // String to be sent on Broadcast as soon as Data is Fetched
    // should be included on WidgetProvider manifest intent action
    // to be recognized by this WidgetProvider to receive broadcast
    public static final String DATA_FETCHED = "com.wordpress.laaptu.DATA_FETCHED";


    /*
     * this method is called every 30 mins as specified on widgetinfo.xml this
     * method is also called on every phone reboot from this method nothing is
     * updated right now but instead RetmoteFetchService class is called this
     * service will fetch data,and send broadcast to WidgetProvider this
     * broadcast will be received by WidgetProvider onReceive which in turn
     * updates the widget
     */
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {

        // which layout to show on widget
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.widget_layout);
        Intent informationIntent = new Intent(context,info.class);
        PendingIntent infoPendingIntent = PendingIntent.getActivity(context,0, informationIntent, 0);
        remoteViews.setOnClickPendingIntent(R.id.btnSeeMore,infoPendingIntent);

        final int N = appWidgetIds.length;
        for (int i = 0; i < N; i++) {
            Intent serviceIntent = new Intent(context, RemoteFetchService.class);
            serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    appWidgetIds[i]);
            context.startService(serviceIntent);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

manifest added a activity

<activity android:name=".info" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.INFO" />
            </intent-filter>
        </activity>

when on click it should open my app,how to do that please help, thank you

finaly i solved what i want to get, just wrote a button click code onReceive & it worked

@Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        if (intent.getAction().equals(DATA_FETCHED)) {
            Log.i("inside action", "DATA_FETCHED");
            int appWidgetId = intent.getIntExtra(
                    AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
            AppWidgetManager appWidgetManager = AppWidgetManager
                    .getInstance(context);
            RemoteViews remoteViews = updateWidgetListView(context, appWidgetId);

              Intent informationIntent = new Intent(context,MainActivity.class);
              PendingIntent infoPendingIntent = PendingIntent.getActivity(context,0, informationIntent, 0);
              remoteViews.setOnClickPendingIntent(R.id.txtSeeMore,infoPendingIntent);

            appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
        }   

}
War es hilfreich?

Lösung

You need to define how the PendingIntent you are setting will be handled, by overriding the onReceive method inside your provider :

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    if (intent.getAction().equals(ACTION_NAME)) {
        // Open your activity here.
    }
}

If every item in your ListView needs a different extra to help the onReceive create the proper Intent, you can use a FillInIntent for each item of the ListView, in getViewAt :

// Set the onClickFillInIntent
final Intent fillInIntent = new Intent();
final Bundle extras = new Bundle();
extras.putInt(YourWidgetProvider.ACTION_EXTRA_ITEM, event.getId()); // retrieve this in onReceive with intent.getIntExtra(ACTION_EXTRA_ITEM, -1)
fillInIntent.putExtras(extras);
rv.setOnClickFillInIntent(R.id.listview_main_layout, fillInIntent);
return rv;

Also, don't forget to add your Provider and your RemoteViewsService to your Manifest :

<receiver 
    android:name="com.example.YourWidgetProvider"
    android:label="@string/widget_name">
     <intent-filter>
         <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
     </intent-filter>
     <meta-data
         android:name="android.appwidget.provider"
         android:resource="@xml/provider_definition_in_xml" />
</receiver>

<service android:name="com.example.YourRemoteViewsService"
     android:permission="android.permission.BIND_REMOTEVIEWS"
     android:exported="false" />

EDIT :

To handle clicks on buttons in the main widget (not in the collection view), it's even easier :

rv.setOnClickPendingIntent(R.id.button_id, PendingIntent.getBroadcast(context, 0,
                        new Intent(context, YourWidgetProvider.class)
                        .setAction(ACTION_NAME),
                        PendingIntent.FLAG_UPDATE_CURRENT));

The ACTION_NAME needs to be different from the name you use for the clicks on the collection view, of course, since it needs to produce a different Intent.

I hope this helps !

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top