11
10

More than 5 years have passed since last update.

[AndroidWidget] WidgetでHelloWorldするまで

Last updated at Posted at 2016-01-12

BGSLogger(BackGroundSensorsLogger)要件

  1. バックグラウンドで動く
  2. 加速度・加圧センサーの取得
  3. ログファイルの書き出し
  4. Widgetから実行できるといい

開発環境

  • AndroidStudio 1.4
  • API 19:Android 4.4(KitKat)

必要そうな手順

  • WidgetでHelloWorld
  • ボタンをトリガーにしたトースト表示
  • バックグラウンドのLooper
  • バックグラウンドのセンサー取得
  • ログファイル書き出し

WidgetでHelloWorld

Widjetで出来ること

  • ホームスクリーンに常駐
  • 同一ウィジェットを複数起動
  • 起動中にフルスクリーンを使用しない

出来ないこと

  • きめ細かいイベントディスパッチ
  • きめ細かいレイアウト

利用可能なレイアウト
FrameLayout
LinearLayout
RelativeLayout
GridLayout

ウィジェットがサポートするクラス
AnalogClock
Button
Chronometer
ImageButton
ImageView
ProgressBar
TextView
ViewFlipper
ListView
GridView
StackView
AdapterViewFlipper

Widgetのライフサイクル

image
androidアプリに比べてかなりシンプル

必要なファイル

  • AndroidManifest.xml
  • 通常のアプリケーションと同様にレイアウトを定義したXMLファイル
  • Widgetの描画サイズや更新時間などを記述したXMLファイル
  • 実際の処理を行うAppWidgetProviderクラスを継承したクラス

AndroidManifest.xmlに以下を追記

AndroidManifest.xml
<receiver android:name=".BGSLoggerWidget" >
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>

    <meta-data
        android:name="android.appwidget.provider"
        android:resource="@xml/bgslogger_widget_info" />
</receiver>

レイアウト定義ファイル

bgslogger_widget.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:padding="@dimen/widget_margin" android:background="#09C">

    <TextView android:id="@+id/appwidget_text" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" android:text="HelloWorld"
        android:textColor="#ffffff" android:textSize="24sp" android:textStyle="bold|italic"
        android:layout_margin="8dp" android:contentDescription="@string/appwidget_text"
        android:background="#09C" />

</RelativeLayout>

Widgetの描画サイズや更新時間などを記述したXMLファイル

bgslogger_widget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="250dp" android:minHeight="250dp"
    android:updatePeriodMillis="86400000"
    android:previewImage="@drawable/example_appwidget_preview"
    android:initialLayout="@layout/bgslogger_widget"
    android:resizeMode="horizontal"
    android:widgetCategory="home_screen"
    android:initialKeyguardLayout="@layout/bgslogger_widget">
</appwidget-provider>

重要項目
- android:minWidth:Widgetの幅(単位:dip)
- android:minHeight:Widgetの高さ(単位:dip)
- android:initialLayout:Widgetの初期レイアウト
- android:updatePeriodMillis:Widgetの更新間隔(単位:ms) ただし0の場合は更新しない

あとはエミュレータで普通に起動できた

完成画面
スクリーンショット 2016-01-13 8.19.18.png

なんか中心からずれてね?まぁいいか

参考 
AndroidStudioでWidget作成
Androidでの簡易なWidget作り方
AppWidgetの作成(1)

11
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
10