Collection of Android Development Engineers - 1 hour learning how to develop Widget

Keywords: Android xml Java

Preface

Hello, I'm Vic. Today I'll bring you a collection of Android Development Engineers - an overview of learning Widget development in one hour. I hope you like it

Learn to use Widget

Widget widgets are very convenient, fast, personalized, customized, relevant functions, and can update the latest content in real time.

  • Widget layout, widget configuration, AppWidget provider and Configuration Activity
  • Android Widget
  • xml layout type: AppWidget Provider
<appwidget-provider 
 xmlns:android="http://schemas.android.com/apk/res/android"
 //Referenced layout file
 android:initialLayout="@layout/widget"
 android:minHeight="50dp"
 android:minWidth="100dp"
 //Set update time
 android:updatePerioMillis="66666666">
</appwidget-provide>
public class WidgetProvider extends AppWidgetProvider {
 @Override
 public void onDeleted(Context context,int[] appWidgetIds){
  super.onDeleted(context,appWidgetIds);
}
 @Override
 public void onDisabled(Context context){
  super.onDisabled(context);
 }
 @Override
 public void onEnabled(Context context){
  super.onEnabled(context);
  }
 @Override
 public void onReceive(Context context,Intent intent){
  super.onReceive(context,intent);
  }
 @Override
 public void onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds){
 super.onUpdate(context,appWidgetManager,appWidgetIds);
}
}
//TimerService extends Service
Timer timer=new Timer();
timer.shedule(new TimerTask(){
 @Override
 public void run(){
   updateTime();
 }
 },0,1000);
}
//onDestroy
public void onDestroy(){
 super.onDestroy();
 timer=null;
}
//updateTime();
private void updateTime(){
 String time = simpletime.format(new Date());
}

Widget is a desktop widget. With widget, we can directly perform various operations on the desktop.

Widget use

  • AppWidget declared in Android manifest
  • Defining AppWidget configuration file in xml
  • Define Widget layout file in layout
  • Create class inherits AppWidgetProvider class
//In manifest file
<meta-data
   android:name="android.appwidget.provider"
   android:resource="@layout/widget_setting"/>
//onUpdate() key points
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
 super.onUpdate(context, appWidgetManager, appWidgetIds);
 RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout_widget);
 //Intent intent = new Intent();
 Intent intent = new Intent();
 intent.setClass(context, TestWidget.class); 
 intent.setAction(WIDGET_BTN_ACTION);
 PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
 remoteViews.setOnClickPendingIntent(R.id.widget_btn, pendingIntent);
 appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); 
} 
//Inventory file
 <intent-filter>      
   <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />      
   </intent-filter>      
   <meta-data android:name="android.appwidget.provider"      
      android:resource="@xml/widget_provider"/>      

summary

  • This article talks about the collection of Android Development Engineers - 1 hour to learn how to develop Widget. If you have a better understanding, welcome to communicate
  • Positioning: share Android & java knowledge points, if you are interested, you can continue to pay attention

Posted by Nandini on Fri, 03 Apr 2020 14:36:15 -0700