AppWidget for Android-Launcher development (2)
AppWidget communicates data through Memory sharing. The schematic diagram is as follows:
1. Create a BroadcastReceiver that inherits the AppWidgetProvider.
2. Configure AndroidManifest. xml as follows: the following official website example is required.
3. Provide The xml file of the resource in the tag, which defines the style attribute of the appwidget (the configuration parameter is optional)
4. Define the style of the appwidget. Define a layout file as the layout file and configure it in the above intialLayout attribute.
5. When you click to add a widget, first obtain the appWidgetId and open an Activity that selects the widget.
6. After the selection, obtain appWidgetInfo through appWidgetId, convert it to the LauncherAppWidgetInfo object, and add it to workspace.
Of course, this period includes the location of the appwidget. The source code to be added is as follows:
private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) { Bundle extras = data.getExtras(); int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); if (LOGD) Log.d(TAG, dumping extras content= + extras.toString()); AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId); // Calculate the grid spans needed to fit this widget CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen); int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight); // Try finding open space on Launcher screen final int[] xy = mCellCoordinates; if (!findSlot(cellInfo, xy, spans[0], spans[1])) { if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId); return; } // Build Launcher-specific widget info and save to database LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId); launcherInfo.spanX = spans[0]; launcherInfo.spanY = spans[1]; LauncherModel.addItemToDatabase(this, launcherInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP, mWorkspace.getCurrentScreen(), xy[0], xy[1], false); if (!mRestoring) { mDesktopItems.add(launcherInfo); // Perform actual inflation because we're live launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo); launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo); launcherInfo.hostView.setTag(launcherInfo); mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1], launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked()); } }Conclusion: After completing the first three steps, the system creates widgets based on the user's choice.
-- Update the AppWidget interface. The working principle is as follows:
1AppWidgetManager sends the broadcast transfer ID.
void updateProvidersForPackageLocked(String pkgName) { HashSet
keep = new HashSet
(); Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE); intent.setPackage(pkgName); List
broadcastReceivers = mPackageManager.queryBroadcastReceivers(intent, PackageManager.GET_META_DATA); // add the missing ones and collect which ones to keep int N = broadcastReceivers == null ? 0 : broadcastReceivers.size(); for (int i=0; i
0) { int[] appWidgetIds = getAppWidgetIds(p); // Reschedule for the new updatePeriodMillis (don't worry about handling // it specially if updatePeriodMillis didn't change because we just sent // an update, and the next one will be updatePeriodMillis from now). cancelBroadcasts(p); registerForBroadcastsLocked(p, appWidgetIds); // If it's currently showing, call back with the new AppWidgetProviderInfo. for (int j=0; j
=0; i--) { Provider p = mInstalledProviders.get(i); if (pkgName.equals(p.info.provider.getPackageName()) && !keep.contains(p.info.provider.getClassName())) { removeProviderLocked(i, p); } } }
2. You can customize the onUpdate () of AppwidgetProvider to receive data and update the data through the Manager interface. (as shown in the following figure, the official website provides two operations: 1. Update the interface. 2. Click the widget operation)
Public class ExampleAppWidgetProvider extends AppWidgetProvider {public void onUpdate (Context context, AppWidgetManager appWidgetManager, int [] appWidgetIds) {final int N = appWidgetIds. length; // Perform this loop procedure for each App Widget that belongs to this provider for (int I = 0; I
-- Updating the App Widget from the configuration Activity)
1. Create an Activity and configure it in AndroidManifest. xml:
2. Add attributes in the configuration xml of appwidget:
android:configure=org.lean.ConfigureActivity
3. operate on the Activity. 1. Modify the remote interface according to the configuration. 2. jump back to the selected activity and turn it off. The Launcher interface is displayed.
Package org. lean; import android. app. activity; import android. appwidget. appWidgetManager; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. editText; import android. widget. remoteViews;/***** @ author Lean @ date: 2014-8-27 */public class ConfigureActivity extends Activity {@ Overrideprotected void onCreate (Bundl E savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. configure_view); findViewById (R. id. button1 ). setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {int appwidgetId = getIntent (). getExtras (). getInt (AppWidgetManager. EXTRA_APPWIDGET_ID, AppWidgetManager. INVALID_APPWIDGET_ID); if (appwidgetId! = AppWidgetManager. INVALID_APPWIDGET_ID) {// modify the remote interface EditText editText = (EditText) findViewById (R. id. editText1); String currenEtStr = editText. getText (). toString (); RemoteViews views = new RemoteViews (ConfigureActivity. this. getPackageName (), R. layout. layout_widget); views. setTextViewText (R. id. btn, currenEtStr); AppWidgetManager manager = AppWidgetManager. getInstance (ConfigureActivity. this); manager. updateAppWidget (appwidgetId, views); // return activityIntent reIntent = new Intent (); reIntent. putExtra (AppWidgetManager. EXTRA_APPWIDGET_ID, appwidgetId); setResult (RESULT_ OK, reIntent); finish ();}}});}}