void addLiveFolder(Intent intent) { // RESULT_PICK_LIVE_FOLDER // Handle case where user selected "Folder" String folderName = getResources().getString(R.string.group_folder); String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); if (folderName != null && folderName.equals(shortcutName)) { addFolder(!mDesktopLocked); } else { startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER); } }
完成添加
private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo, boolean insertAtFirst) { // REQUEST_CREATE_LIVE_FOLDER cellInfo.screen = mWorkspace.getCurrentScreen(); if (!findSingleSlot(cellInfo)) return; final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false); if (!mRestoring) { sModel.addDesktopItem(info); final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this, (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info); mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst); } else if (sModel.isDesktopLoaded()) { sModel.addDesktopItem(info); } }
3、AppWidget:AppWidgetProvider用来在HOME页面显示插件
实现步骤:
为AppWidget提供一个元布局文件AppWigdetProvider_Provider.xml,用来显示Widget的界面。
创建一个类继承自AppWidgetProvider,并覆写里面的相关的方法并且注册到配置文件。
为WidgetProvider创建一个引用的布局文件。
>>1、在res/xml/文件夹下创建AppWigdetProvider_Provider.xml文件
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialLayout="@layout/main" <!-- android:initialLayout 设置引用的布局文件 --> android:minHeight="50dip" android:minWidth="50dip" android:updatePeriodMillis="5000" > <!-- 设置更新时间,单位为毫秒 --> </appwidget-provider>
>>2、修改MainActivity继承自AppWidgetProvider并覆写里面的一些方法,实际上AppWidgetProvider就是一个BroadcastReceiver;
public class MainActivity extends AppWidgetProvider { @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); Timer timer = new Timer(); timer.scheduleAtFixedRate(new LYTimeTask(context, appWidgetManager), 1, 50000); } private class LYTimeTask extends TimerTask { RemoteViews remoteViews; AppWidgetManager appWidgetManager; ComponentName widget; @Override public void run() { Date date = new Date(); Calendar calendar = new GregorianCalendar(2013, 07, 24); long days = (calendar.getTimeInMillis() - date.getTime()) / 1000 / 86400; remoteViews.setTextViewText(R.id.worldcup, "还剩下" + days + "天"); appWidgetManager.updateAppWidget(widget, remoteViews); } public LYTimeTask(Context context, AppWidgetManager appWidgetManger) { super(); this.appWidgetManager = appWidgetManger; remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main); widget = new ComponentName(context, MainActivity.class); } }; }
>>3、为Widget创建一个显示用的布局文件:main.xml