Android - Communications between a widget and its app

点点圈 提交于 2019-12-11 15:09:22

问题


I have a widget that shows various images with text below, and the same UI set up in the app itself. I want the widget to not only be able to open the app, but to open the app based on which picture is showing in the widget and then show that same image in the app. However, I am having a tough time getting this to work.

Thanks.


回答1:


Let's say there are several images in the ui, you can set different Intent for each image, and these intents each will target a different activity.

This post: http://rxwen.blogspot.com/2012/10/communication-between-android-widget.html may give you some hints.




回答2:


Add this code in widget.java file to launch your application. instead of MainActivity.class you can call any activity

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                    int appWidgetId) {

        RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.mywidget_provider);
        Intent openApp = new Intent(context,MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(context,0,openApp,0);

        views.setOnClickPendingIntent(R.id.btnOpenApp,pIntent);
        appWidgetManager.updateAppWidget(appWidgetId,views);
}


来源:https://stackoverflow.com/questions/8176137/android-communications-between-a-widget-and-its-app

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