Android

Android Widget桌面小物件

1.先定義想要顯示佈局的layout

使用ConstrainLayout的話就沒辦法顯示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
  <TextView
        android:id="@+id/tv_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="22dp"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="13dp"
        android:text="時間顯示"
        android:textColor="#00ff00"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>

2.在res下新增xml的文件並創建一個xml檔來配置物件大小以及初始化layout

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

3.寫一個類繼承AppWidgetProvider,寫完Service後來這個類調用

public class WidgetProvider extends AppWidgetProvider {
  public static final String TAG = WidgetProvider.class.getSimpleName();

  @Override
  public void onDeleted(Context context, int[] appWidgetIds) {
     super.onDeleted(context, appWidgetIds);
        //螢幕上工具被移除
        Log.d(TAG, "onDeleted: ");
        context.stopService(new Intent(context, TimerService.class));
 }

 @Override
 public void onDisabled(Context context) {
        super.onDisabled(context);
        //螢幕上最後一個工具被移除
        context.stopService(new Intent(context, TimerService.class));
        Log.d(TAG, "onDisabled: ");
  }

 @Override
  public void onEnabled(Context context) {
     super.onEnabled(context);
        //工具添加到螢幕上
       context.startService(new Intent(context, TimerService.class));
       Log.d(TAG, "onEnabled: ");
 }

 @Override
 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
      super.onUpdate(context, appWidgetManager, appWidgetIds);
     //工具刷新的時候
     //remoteView&AppWidgetManager
     context.startService(new Intent(context, TimerService.class));
  }
}

4.撰寫想要讓這個物件取得什麼資訊,基本上都要寫在Service裡面

public class TimerService extends Service {
    private Timer mTimer;
    private SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

 @Nullable
 @Override
 public IBinder onBind(Intent intent) {
     return null;
 }

 @Override
 public void onCreate() {
        super.onCreate();
        mTimer = new Timer();
       mTimer.schedule(new TimerTask() {
           @Override
           public void run() {
               updateView();
           }
      }, 0, 1000);
    }

 private void updateView() {
       String time = mSimpleDateFormat.format(new Date());
       RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget);
       remoteViews.setTextViewText(R.id.tv_time, time);
       AppWidgetManager manager = AppWidgetManager.getInstance(getApplicationContext());
       ComponentName cn = new ComponentName(getApplication(), WidgetProvider.class);
       manager.updateAppWidget(cn, remoteViews);
  }

 @Override
 public void onDestroy() {
     super.onDestroy();
     mTimer = null;
  }
}

5.在Manifests裡配置物件以及Service

<service android:name=".TimerService" />
<receiver android:name=".WidgetProvider">
   <intent-filter>
     <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
   </intent-filter>
   <meta-data android:name="android.appwidget.provider"
         android:resource="@xml/widgetconfig"/>
</receiver>

發表迴響