中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android小部件Widget開發過程中會出現哪些問題

發布時間:2020-10-29 19:38:41 來源:億速云 閱讀:206 作者:Leah 欄目:開發技術

Android小部件Widget開發過程中會出現哪些問題?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。

AndroidManifest.xml

Receiver

切記里面的字母不要弄錯,最好復制粘貼再修改相對應自定義的地方就好,一個字母的錯誤搞了我一天,吐血

<receiver android:name=".desktop.DesktopWidget">
  <meta-data
    android:name="android.appwidget.provider"
    android:resource="@xml/widget_desktop_options" />
  <intent-filter>
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
  </intent-filter>
</receiver>

Service

如果小部件中使用到了列表項如 ListView,GridView 等,在綁定數據時需要使用 RemoteViewsService 并提供一個 RemoteViewsFactory 實例來填充數據 而非 Adapter
再提,這里面一定不能敲錯字母,特別是那個 permission 也一定要有,不然無法綁定數據

<service
  android:name=".desktop.DesktopViewsService"
  android:permission="android.permission.BIND_REMOTEVIEWS" />

Options

res/xml/

widget_desktop_options.xml

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
  android:minWidth="40dp"
  android:minHeight="40dp"
  android:initialLayout="@layout/widget_desktop"
  android:resizeMode="horizontal|vertical"
  android:widgetCategory="home_screen">
</appwidget-provider>

常用參數

Size

  • 尺寸大小最終以單元格數據來顯示,但定義時為 dp
  • 單元格數轉換基本工式 size = 70 x cells - 30
  • 如:1格 = 70 x 1 - 30 = 40dp
  • 最小尺寸定義時最好不要超過 4 個單元格就是 250dp

updatePeriodMillis

更新時間毫秒數,即間隔多少時間呼叫一次 onUpdate() 方法

initialLayout

加載布局文件

Using App Widgets with Collections

小部件中使用列表項

官方參考

Google Develper

AppWidgetProvider

public class DesktopWidget extends AppWidgetProvider {

  public String ACTION_START_ACTIVITY = "STARTACTIVITYACTION";

  public DesktopWidget() {
    super();
  }

  //當接收到廣播的時候會被調用
  //onUpdate, onEnabled等時都會發送廣播
  @Override
  public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    pub.log("onReceive:" + intent.getAction());
    if (intent.getAction().equals(ACTION_START_ACTIVITY)) {
      String strPackage = intent.getStringExtra("app_package");
      Intent appIntent = context.getPackageManager().getLaunchIntentForPackage(strPackage);
      pub.log(appIntent.getPackage());
      if (appIntent != null) {
        appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        pub.log("start activity");
        context.startActivity(appIntent);
        pub.log("finish");
      }
    }
  }

  //被添加或者被更新時調用
  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, 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) {
    pub.log("onUpdate:" + appWidgetId);
    // Construct the RemoteViews object
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_desktop);
    //設定小部件上 TextView 值
    //views.setTextViewText(R.id.tv_widget_desktop_title, "ZGTools");
    //setRemoteAdapter設定Listivew,Gridview等復雜布局的adapter
    //第一個參數為要綁定的widget的Gridview的id,第二個參數為RemoteViewsService的intent
    Intent intent = new Intent(context, DesktopViewsService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
    views.setRemoteAdapter(R.id.gv_app_group, intent);

    //綁定 item 的定點事件
    //Activity方式測試不成功,改用廣播的方式進行發送并執行事件
    //此處不能是空的Intent 否則廣播無法發送
    //定義Intent事件模板,相當于公用設定,再在fillIntent里面設定具體的 extra 值
    Intent templateIntent = new Intent(context, DesktopWidget.class);
    templateIntent.setAction(ACTION_START_ACTIVITY);
    templateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    templateIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, templateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    views.setPendingIntentTemplate(R.id.gv_app_group, pendingIntent);

    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget(appWidgetId, views);
  }


  //被添加或者更改大小時調用,在這個方法中可以選擇性的根據界面大小顯示或隱藏某些控件
  @Override
  public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
    super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
    updateAppWidget(context, appWidgetManager, appWidgetId);
  }
  @Override
  public void onEnabled(Context context) {
    super.onEnabled(context);
    // Enter relevant functionality for when the first widget is created
  }
  @Override
  public void onDisabled(Context context) {
    super.onDisabled(context);
    // Enter relevant functionality for when the last widget is disabled
  }
}

RemoteViewsService

public class DesktopViewsService extends RemoteViewsService {
  @Override
  public RemoteViewsFactory onGetViewFactory(Intent intent) {
    return new DesktopViewsFactory(this, intent);
  }
}

RemoteViewsFactory

類型于 BaseAdapter,重點是 onCreate() 加載數據, getViewAt() 方法返回布局綁定數據

public class DesktopViewsFactory implements RemoteViewsService.RemoteViewsFactory {
  private Context mContext;
  private int mAppWidgetId;
  private List<AppPackage> lstApps = new ArrayList<AppPackage>();

  public DesktopViewsFactory(Context context, Intent intent){
    this.mContext = context;
    this.mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
  }

  @Override
  public void onCreate() {
    DesktopDbHelper dbHelper = new DesktopDbHelper(mContext);
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    String strSql = "Select * from tb_app";
    try {
      Cursor cursor = db.rawQuery(strSql, null);
      if (cursor != null && cursor.getCount() > 0) {
      	lstApps.clear();
        while (cursor.moveToNext()) {
          byte[] blob = cursor.getBlob(cursor.getColumnIndex("app_icon"));
          Bitmap bmpIcon = BitmapFactory.decodeByteArray(blob, 0, blob.length);
          String strLabel = cursor.getString(cursor.getColumnIndex("app_label"));
          String strPackage = cursor.getString(cursor.getColumnIndex("app_package"));
          lstApps.add(new AppPackage(strLabel, strPackage, bmpIcon));
        }
      }
      if (cursor != null) {
        cursor.close();
      }
    } catch (Exception e) {
      db.close();
      e.printStackTrace();
    }
    db.close();
  }

  @Override
  public void onDataSetChanged() {
  	//這里我加了重新加載數據的調用,用于更新小部件的列表內容
		onCreate();
	}
  @Override
  public void onDestroy() {}
  @Override
  public int getCount() {return 0;}

  @Override
  public RemoteViews getViewAt(int i) {
    if (i < 0 || i >= lstApps.size()) return null;
    //Construct a remote views item based on the app widget item XML file
    RemoteViews views = new RemoteViews(mContext.getPackageName(), R.layout.widget_desktop_item);
    views.setTextViewText(R.id.tv_widget_desktop_label, lstApps.get(i).getAppLabel());
    views.setImageViewBitmap(R.id.iv_widget_desktop_icon, lstApps.get(i).getAppIcon());

    Intent intent = new Intent();
    Bundle bundle = new Bundle();
    bundle.putString("app_package", lstApps.get(i).getAppPackage());
    intent.putExtras(bundle);
    views.setOnClickFillInIntent(R.id.iv_widget_desktop_icon, intent);
    views.setOnClickFillInIntent(R.id.tv_widget_desktop_label, intent);

    return views;
  }

  @Override
  public RemoteViews getLoadingView() { return null;}
  @Override
  public int getViewTypeCount() {return 0;}
  @Override
  public long getItemId(int i) { return 0;}
  @Override
  public boolean hasStableIds() { return false; }
}

DesktopActivity

 //更新widget布局
  private void updateWidget(){
    AppWidgetManager widgetManager = AppWidgetManager.getInstance(this);
    ComponentName componentName = new ComponentName(this, DesktopWidget.class);
    int[] ids = widgetManager.getAppWidgetIds(componentName);
    widgetManager.notifyAppWidgetViewDataChanged(ids, R.id.gv_app_group);
  }

看完上述內容,你們掌握Android小部件Widget開發過程中會出現哪些問題的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

同仁县| 饶阳县| 麻城市| 台前县| 历史| 北辰区| 洞头县| 屏南县| 达拉特旗| 塔城市| 张家界市| 罗源县| 同仁县| 英山县| 玛多县| 武穴市| 军事| 石门县| 元朗区| 乳山市| 东莞市| 沧州市| 怀宁县| 加查县| 山阳县| 景宁| 张北县| 泾川县| 策勒县| 米脂县| 孟村| 青铜峡市| 荃湾区| 海丰县| 大新县| 太康县| 抚松县| 宁河县| 洪雅县| 金寨县| 汾西县|