Aby stworzyć widget w aplikacji opartej na cordovie musimy część aplikacji napisać w Javie. Po pierwsze, musimy skonfigurować plik AndroidManifest.xml. Znaleźć go można w katalogu: platforms/android/app/src/main/AndroidManifest.xml. Powinien on wyglądać mniej więcej tak:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?xml version='1.0' encoding='utf-8'?> <manifest android:hardwareAccelerated="true" android:versionCode="10000" android:versionName="1.0.0" package="com.fxteam.widget" xmlns:android="http://schemas.android.com/apk/res/android"> <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:hardwareAccelerated="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true"> <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize"> <intent-filter android:label="@string/launcher_name"> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- WIDGET --> <receiver android:name=".SimpleAppWidget"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/simple_app_widget_info" /> </receiver> <!-- WIDGET ^ --> </application> </manifest> |
tag receiver odpowiada za widget.
Następnie tworzymy plik o nazwie np SimpleAppWidget.java w katalogu platforms/android/app/src/main/java/com/fxteam/widget/ obok pliku MainActivity.java .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | package com.fxteam.widget; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.widget.RemoteViews; import android.util.Log; /** * Implementation of App Widget functionality. */ public class SimpleAppWidget extends AppWidgetProvider { @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // There may be multiple widgets active, so update all of them for (int appWidgetId : appWidgetIds) { updateAppWidget(context, appWidgetManager, appWidgetId); } } private void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { // Construct the RemoteViews object RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.simple_app_widget); // Construct an Intent object includes web adresss. Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://fx-team.fulara.com")); // In widget we are not allowing to use intents as usually. We have to use PendingIntent instead of 'startActivity' PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); // Here the basic operations the remote view can do. views.setOnClickPendingIntent(R.id.tvWidget, pendingIntent); // Instruct the widget manager to update the widget appWidgetManager.updateAppWidget(appWidgetId, views); } } |
W katalogu platforms/android/app/src/main/res/layout tworzymy plik simple_app_widget.xml (jeżeli nie ma katalogu layout należy go utworzyć)
1 2 3 4 5 6 7 8 9 10 11 12 13 | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tvWidget" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff6200" android:gravity="center" android:textColor="#ffffff" android:textSize="24sp" android:textStyle="bold|italic"/> |
Następnie w katalogu res/xml tworzymy plik simple_app_widget_info.xml
1 2 3 4 5 6 7 8 9 10 11 12 | <?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialKeyguardLayout="@layout/simple_app_widget" android:initialLayout="@layout/simple_app_widget" android:minHeight="60dp" android:minWidth="60dp" android:previewImage="@android:drawable/ic_menu_add" android:resizeMode="horizontal|vertical" android:updatePeriodMillis="0" android:widgetCategory="home_screen"> </appwidget-provider> |
W tym momencie po skompilowaniu aplikacji powinna wyświetlać się opcja utworzenia widgetu na ekranie głównym telefonu.