In Android development, sometimes our app design more features, need to be more concise as needed to provide users with clearly used features, the desktop widget is a good choice, that is, the app widget, below we use Android Studio to create a simple desktop widget:
- Select New->widget->app Wight
- Next set up some information about the app widget where minimum Width and M Inimum height is the size of the app widget, and the size we set is shown on the phone is the same size as an app icon.
- Next click on the Finish button and you're done. Is it easy to run your app and you can add widgets to your app on the desktop?
< Span class= "token MD md-src" >< Span class= "token lf" > Next look at what Android studio did for us , after you generate the app widget, you'll find three more files in your project.
< Span class= "token MD md-alt" > < Span class= "token lf" > The first one is the Java class of the app widget, The main logic is implemented here, and the class overrides the three methods of the parent class Appwidgetprovider:
- < Span class= "token MD md-bang" > < Span class= "token lf" >onupdate () This method is called every time the app widget is updated, and the logic for updating the data is written in this method. For example: TextView update, make a control visible or invisible.
- < Span class= "token MD md-alt" > < Span class= "token lf" > Onenabled () This method is called when the app widget is available, such as when the user is adding our widget, and doing some initialization in the function, such as initializing the database.
- < Span class= "token MD md-alt" > < Span class= "token lf" > Ondisabled () This method will be called when the app widget is unavailable, such as when the user deletes our widget, which in this function can end the resource consumed by the app widget.
< Span class= "token lf" > < Span class= "token MD md-li" > The second one is a layout file New_app_widget.xml, The layout of our widgets is set inside this. Third is the configuration file for the app widget inside the XML folder,
< Span class= "token p" > These are the implementations of the entire app widget. I hope I can help you ~
< Span class= "token MD md-li" >< Span class= "token MD md-em Md-start" > < Span class= "token lf" > [< Span class= "token MD md-underlined-text" >PS: I developed a small application to make two-dimensional code, interested friends can try ~ ^_^ creative QR code production "
< Span class= "token lf" >
Implementing desktop widgets in Android development