Android-widgets

提供:Dev Guides
移動先:案内検索

Android-ウィジェット

ウィジェットは、ホーム画面に配置されたAndroidアプリケーションの小さなガジェットまたはコントロールです。 ウィジェットを使用すると、お気に入りのアプリケーションをホーム画面に置いてすばやくアクセスできるため、非常に便利です。 おそらく、音楽ウィジェット、天気ウィジェット、時計ウィジェットなどの一般的なウィジェットを見たことがあるでしょう。

ウィジェットには、情報ウィジェット、コレクションウィジェット、コントロールウィジェット、ハイブリッドウィジェットなど、多くの種類があります。 Androidは、独自のウィジェットを開発するための完全なフレームワークを提供します。

ウィジェット-XMLファイル

アプリケーションウィジェットを作成するために最初に必要なのは、AppWidgetProviderInfoオブジェクトです。これは、別個のウィジェットXMLファイルで定義します。 そのためには、プロジェクトを右クリックして、 xml という名前の新しいフォルダーを作成します。 次に、新しく作成したフォルダーを右クリックして、新しいXMLファイルを作成します。 XMLファイルのリソースタイプは AppWidgetProvider に設定する必要があります。 xmlファイルでは、次のようないくつかのプロパティを定義します-

<appwidget-provider
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:minWidth="146dp"
   android:updatePeriodMillis="0"
   android:minHeight="146dp"
   android:initialLayout="@layout/activity_main">
</appwidget-provider>

ウィジェット-レイアウトファイル

次に、デフォルトのXMLファイルでウィジェットのレイアウトを定義する必要があります。 コンポーネントをドラッグして自動XMLを生成できます。

ウィジェット-Javaファイル

レイアウトを定義した後、新しいJAVAファイルを作成するか、既存のファイルを使用して、 AppWidgetProvider クラスで拡張し、次のように更新メソッドをオーバーライドします。

updateメソッドでは、PendingIntentとRemoteViewsの2つのクラスのオブジェクトを定義する必要があります。 その構文は-

PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);

最後に、AppWidgetManagerクラスの更新メソッドupdateAppWidget()を呼び出す必要があります。 その構文は-

appWidgetManager.updateAppWidget(currentWidgetId,views);

updateAppWidgetメソッドの一部であり、ウィジェットを操作するためにこのクラスで定義されている他のメソッドがあります。 彼らは次のとおりです-

Sr.No Method & Description
1

onDeleted(Context context, int[] appWidgetIds)

これは、AppWidgetProviderのインスタンスが削除されるときに呼び出されます。

2

onDisabled(Context context)

これは、AppWidgetProviderの最後のインスタンスが削除されたときに呼び出されます

3

onEnabled(Context context)

これは、AppWidgetProviderのインスタンスが作成されるときに呼び出されます。

4

onReceive(Context context, Intent intent)

クラスのさまざまなメソッドへの呼び出しをディスパッチするために使用されます

ウィジェット-マニフェストファイル

また、マニフェストファイルでAppWidgetProviderクラスを次のように宣言する必要があります。

<receiver android:name="ExampleAppWidgetProvider" >

   <intent-filter>
      <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
   </intent-filter>

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

以下に、アプリケーションWidgetの使用例を示す例を示します。 この現在のWebサイトをブラウザーで開く基本的なウィジェットアプリケーションを作成します。

この例を試すには、インターネットが実行されている実際のデバイスでこれを実行する必要があります。

Steps Description
1 You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication.
2 Modify src/MainActivity.java file to add widget code.
3 Modify the res/layout/activity_main to add respective XML components
4 Create a new folder and xml file under res/xml/mywidget.xml to add respective XML components
5 Modify the AndroidManifest.xml to add the necessary permissions
6 Run the application and choose a running android device and install the application on it and verify the results.

変更された MainActivity.java の内容は次のとおりです。

package com.example.sairamkrishna.myapplication;

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.widget.Toast;

public class MainActivity extends AppWidgetProvider{
   public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
      for(int i=0; i<appWidgetIds.length; i++){
         int currentWidgetId = appWidgetIds[i];
         String url = "http://www.finddevguides.com";

         Intent intent = new Intent(Intent.ACTION_VIEW);
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         intent.setData(Uri.parse(url));

         PendingIntent pending = PendingIntent.getActivity(context, 0,intent, 0);
         RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.activity_main);

         views.setOnClickPendingIntent(R.id.button, pending);
         appWidgetManager.updateAppWidget(currentWidgetId,views);
         Toast.makeText(context, "widget added", Toast.LENGTH_SHORT).show();
      }
   }
}

以下は、xml res/layout/activity_main.xml の変更されたコンテンツです。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
   android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin"
   tools:context=".MainActivity"
   android:transitionGroup="true">

   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials point"
      android:id="@+id/textView"
      android:layout_centerHorizontal="true"
      android:textColor="#ff3412ff"
      android:textSize="35dp"/>

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Widget"
      android:id="@+id/button"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="61dp"
      android:layout_below="@+id/textView"/>

</RelativeLayout>

以下は res/xml/mywidget.xml の内容です。

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:minWidth="146dp"
   android:updatePeriodMillis="0"
   android:minHeight="146dp"
   android:initialLayout="@layout/activity_main">
</appwidget-provider>

以下は res/values/string.xml の内容です。

<resources>
   <string name="app_name">My Application</string>
</resources>

以下は AndroidManifest.xml ファイルの内容です。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sairamkrishna.myapplication" >

   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      <receiver android:name=".MainActivity">

      <intent-filter>
         <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
      </intent-filter>

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

      </receiver>

   </application>
</manifest>

アプリケーションを実行してみましょう。 実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像の実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 アプリケーションを開始する前に、Androidスタジオは次のウィンドウを表示して、Androidアプリケーションを実行するオプションを選択します。

Android Widget Tutorial

オプションとしてモバイルデバイスを選択し、デフォルト画面を表示するモバイルデバイスを確認します-

Android Widget Tutorial

ウィジェットセクションに移動して、作成したウィジェットをデスクトップまたはホーム画面に追加します。 これは次のようになります-

Android Widget Tutorial

表示されたウィジェットボタンをタップして、ブラウザを起動します。 ただし、その前に、インターネットに接続していることを確認してください。 ボタンを押した後、次の画面が表示されます-

Android Widget Tutorial

注意。 JavaファイルのURLを変更するだけで、ウィジェットはブラウザで目的のWebサイトを開きます。