How to start an activity by the click on an image in widget?

非 Y 不嫁゛ 提交于 2019-12-08 10:08:35

问题


Intent intent = new Intent( context, ColorConfigure.class);
Intent.putExtras(AppWidgetManager.EXTRA_APPWIDGET_ID, appwidgetId );
PendingIntent pi = PendingIntent.getActivity( context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews r = new RemoteViews( context.getPackageName(), R.layout. activity_widget);
r.setOnClickPendingIntent(R.I'd.pic, pic);

The above code isn't working for the time i.e., when I start the widget for the first time then widget gets loaded easily but when I go onto start a new activity from the widget then nothing happens. But whenever I re-run my app from the eclipse without removing the widget then my widget starts running successfully without any glitch. I don't really know what sort of problem is this one?? Or if anyone is able to help me by sending me the code of a widget that start a fresh activity from a button on that activity.


回答1:


even i have faced similar problem for my app homescreen widget .which is very similar to fb/twitter homescreen widget(show updates if not signed show as "your not signedin").achieved using android service. even you don't need a service. in your case you are not calling manager.updateAppWidget(ids, views);

public class NFWidgetProvider extends AppWidgetProvider {

            @Override
            public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                    int[] appWidgetIds) {
    final Intent intent = new Intent(context, UpdateService.class);

            context.startService(intent);

                 }
}

Updateservice.class //my app code..mention service class app manifest

 public void onStart(Intent intent, int startId) {

AppWidgetManager manager = AppWidgetManager.getInstance(this);
        ComponentName thisWidget = new ComponentName(this,
                NFWidgetProvider.class);
        int[] ids = manager.getAppWidgetIds(thisWidget);
        final int N = ids.length;
        for (int i = 0; i < N; i++) {
            int awID = ids[i];

    RemoteViews    views = new RemoteViews(getPackageName(),
                        R.layout.widget_layout);

                Intent intent = new Intent(this, your intented class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                //views.setTextViewText(R.id.widgetcount, visitstext);
                PendingIntent pendingIntent = PendingIntent.getActivity(this,
                        0, intent, 0);

                views.setOnClickPendingIntent(R.id.widgetimage, pendingIntent);
manager.updateAppWidget(awID, views);
    }


来源:https://stackoverflow.com/questions/26108355/how-to-start-an-activity-by-the-click-on-an-image-in-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!