- <? xml version = "1.0" encoding = "utf-8" ?>
- <appwidget-provider xmlns: android = "schemas.android.com/apk/res/android"
- android: minWidth = "146dip"
- android: minHeight = "72dip"
- android: updatePeriodMillis = "86400000"
- android: initialLayout = "@ layout / main" />
- package ru.example.android.widget ;
- import android.appwidget.AppWidgetProvider ;
- public class HelloWidget extends AppWidgetProvider {
- }
- <? xml version = "1.0" encoding = "utf-8" ?>
- <LinearLayout xmlns: android = " schemas.android.com/apk/res/android"
- android: layout_width = "fill_parent"
- android: orientation = "vertical"
- android: background = "@android: color / white"
- android: layout_gravity = "center"
- android: layout_height = "wrap_content" >
- <TextView android: id = "@ + id / widget_textview"
- android: text = "Hello Widget"
- android: layout_height = "wrap_content"
- android: layout_width = "wrap_content"
- android: layout_gravity = "center_horizontal | center"
- android: textColor = "@android: color / black" />
- </ LinearLayout >
- <receiver android: name = ".widget.HelloWidget" android: label = "@ string / app_name" >
- <intent-filter >
- <action android: name = "android.appwidget.action.APPWIDGET_UPDATE" />
- </ intent-filter >
- <meta-data android: name = "android.appwidget.provider"
- android: resource = "@ xml / hello_widget_provider" />
- </ receiver >
- <? xml version = "1.0" encoding = "utf-8" ?>
- <LinearLayout xmlns: android = " schemas.android.com/apk/res/android"
- android: layout_width = "fill_parent"
- android: orientation = "vertical"
- android: background = "@android: color / white"
- android: layout_gravity = "center"
- android: layout_height = "wrap_content" >
- <TextView android: id = "@ + id / widget_textview"
- android: text = "Hello Widget"
- android: layout_height = "wrap_content"
- android: layout_width = "wrap_content"
- android: layout_gravity = "center_horizontal | center"
- android: textColor = "@android: color / black" />
- <Button android: id = "@ + id / widget_button"
- android: text = "click me"
- android: layout_height = "wrap_content"
- android: layout_width = "wrap_content" />
- </ LinearLayout >
- public class HelloWidget extends AppWidgetProvider {
- public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget" ;
- @ Override
- public void onUpdate ( Context context, AppWidgetManager appWidgetManager, int [ ] appWidgetIds ) {
- // Create a new RemoteViews
- RemoteViews remoteViews = new RemoteViews ( context. GetPackageName ( ) , R. layout . Main ) ;
- // Prepare Intent for Broadcast
- Intent active = new Intent ( context, HelloWidget. Class ) ;
- active. setAction ( ACTION_WIDGET_RECEIVER ) ;
- active. putExtra ( "msg" , "Hello Habrahabr" ) ;
- // create our event
- PendingIntent actionPendingIntent = PendingIntent. getBroadcast ( context, 0 , active, 0 ) ;
- // register our event
- remoteViews. setOnClickPendingIntent ( R. id . widget_button , actionPendingIntent ) ;
- // update widget
- appWidgetManager. updateAppWidget ( appWidgetIds, remoteViews ) ;
- }
- @ Override
- public void onReceive ( Context context, Intent intent ) {
- // We catch our Broadcast, check and display the message
- final String action = intent. getAction ( ) ;
- if ( ACTION_WIDGET_RECEIVER. equals ( action ) ) {
- String msg = "null" ;
- try {
- msg = intent. getStringExtra ( "msg" ) ;
- } catch ( NullPointerException e ) {
- Log e ( "Error" , "msg = null" ) ;
- }
- Toast. makeText ( context, msg, Toast. LENGTH_SHORT ) . show ( ) ;
- }
- super . onReceive ( context, intent ) ;
- }
- }
- <receiver android: name = ".widget.HelloWidget" android: label = "@ string / app_name" >
- <intent-filter >
- <action android: name = "android.appwidget.action.APPWIDGET_UPDATE" />
- <action android: name = "ru.example.android.widget.ACTION_WIDGET_RECEIVER" />
- </ intent-filter >
- <meta-data android: name = "android.appwidget.provider"
- android: resource = "@ xml / hello_widget_provider" />
- </ receiver >
Source: https://habr.com/ru/post/114515/
All Articles