AppWidget for android Learning

Source: Internet
Author: User

AppWidget for android Learning
1. What is AppWidget
The small windows seen on the desktop can provide users with convenient and quick operations, a little like a shortcut...

Ii. AppWidget-related data
1. The AppWidgetProviderInfo object provides metadata for the appWidget, including layout and update frequency. This object is defined in the xml file.
2. appWidgetProvider defines the basic lifecycle of appWidget
3. Create an AppWidget
1. Define the AppWidgetProviderInfo object
Define a file named example_appwidget_info.xml In the res/xml folder.
<Appwidget-provider
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: minWidth = "294dp" <? Minimum Width?>
Android: minHeight = "72dp" <? Minimum height?>
Android: updatePeriodMillis = "86400000" <? Update time, in ms?>
Android: initialLayout = "@ layout/example_appwidget" <? The layout file initialized by appwidget?>
/>
2. Add a layout file for the AppWidget
<TextView
Android: id = "@ + id/widgetText"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "myWidget"
/>
3. Implement appWidgetProvider
OnUpdate: it is called after the specified Update Time on the avenue or when you want to add an appwidget to the desktop.
OnDelete: called when the appWidget is deleted
OnEnabled: called when appwidget is created for the first time
OnDisabled: called when the last appwidget is deleted
OnRecieve: The appwidget that receives broadcasts relies on the broadcast mechanism.
4. Declare broadcast in AndroidManifest
<Cycler android: name = "com. example. appwidget. appWidget">
<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"
/>
</Cycler>
Iv. How to Use AppWidget
1. pendingIntent is used to exchange data between appwidget and other activities. The appwidget and the activity that creates it are not in the same process.
2. How to Create pendingIntent:
(1) getActivity () // used to start a new activity
(2) getBroadcast () // used to start a new broadcast. The broadcast will receive it in the onReceive method.
(3), getService () // used to start a new service
3. remoteViews indicates a series of view objects in another thread.
4. Method for adding a listener to remoteViews: setOnClickPendingIntent
V. Source Code
1. MainActivity. java
No operation is required here.

 
 
  1. public class MainActivity extends Activity
  2. {

  3. @Override
  4. protected void onCreate(Bundle savedInstanceState)
  5. {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. }

  9. @Override
  10. public boolean onCreateOptionsMenu(Menu menu)
  11. {
  12. // Inflate the menu; this adds items to the action bar if it is present.
  13. getMenuInflater().inflate(R.menu.main, menu);
  14. return true;
  15. }

  16. }
2. myAppWidget. java
Operate AppWidget here
 
 
  1. Public class appWidget extends AppWidgetProvider
  2. {
  3. // Define a constant for Custom action
  4. Private static final String UPDATE_ACTION = "appwidget. UPDATE_APPWIDGET ";
  5. @ Override
  6. Public void onDeleted (Context context, int [] appWidgetIds)
  7. {
  8. // TODO Auto-generated method stub
  9. Super. onDeleted (context, appWidgetIds );
  10. }

  11. @ Override
  12. Public void onDisabled (Context context)
  13. {
  14. // TODO Auto-generated method stub
  15. Super. onDisabled (context );
  16. }

  17. @ Override
  18. Public void onEnabled (Context context)
  19. {
  20. // TODO Auto-generated method stub
  21. Super. onEnabled (context );
  22. }

  23. @ Override
  24. Public void onReceive (Context context, Intent intent)
  25. {
  26. // TODO Auto-generated method stub
  27. String action = intent. getAction ();
  28. If (UPDATE_ACTION.equals (action ))
  29. {
  30. System. out. println (action );
  31. // Obtain remoteViews
  32. RemoteViews remoteViews = new RemoteViews (context. getPackageName (), R. layout. example_appwidget );
  33. // Set the action for remoteViews
  34. RemoteViews. setTextViewText (R. id. widgetText, "hello ");
  35. // Create AppWidgetManager
  36. AppWidgetManager appWidgetManager = AppWidgetManager. getInstance (context );
  37. // Create ComponentName
  38. ComponentName compenentName = new ComponentName (context, appWidget. class );
  39. AppWidgetManager. updateAppWidget (compenentName, remoteViews );
  40. }
  41. Else
  42. {
  43. Super. onReceive (context, intent );
  44. }
  45. }

  46. @ Override
  47. Public void onUpdate (Context context, AppWidgetManager appWidgetManager,
  48. Int [] appWidgetIds)
  49. {
  50. // Each time an appwidget is created, an id is added.
  51. For (int I = 0; I <appWidgetIds. length; I ++)
  52. {
  53. System. out. println (appWidgetIds [I]);
  54. // Create an intent object
  55. Intent intent = new Intent (context, targetActivity. class );
  56. // Create a pendingIntent object and use getActivity to start a new activity.
  57. PendingIntent pendingIntent = PendingIntent. getActivity (context, 0, intent, 0 );
  58. // Create remoteViews
  59. RemoteViews remoteViews = new RemoteViews (context. getPackageName (), R. layout. example_appwidget );
  60. // Bind the event processor to remoteViews and execute pendingIntent after a click event occurs.
  61. // The first parameter is the bound control, and the second parameter specifies the pendingIntent to be executed when an event occurs.
  62. RemoteViews. setOnClickPendingIntent (R. id. appWidgetBtn1, pendingIntent );
  63. // Update the appwidget
  64. AppWidgetManager. updateAppWidget (appWidgetIds [I], remoteViews );
  65. }


  66. For (int I = 0; I <appWidgetIds. length; I ++)
  67. {
  68. System. out. println (appWidgetIds [I]);
  69. // Create an intent object
  70. Intent intent = new Intent ();
  71. // Set the action, which must be declared in androidManifest.
  72. Intent. setAction (UPDATE_ACTION );
  73. // Create a pendingIntent object and use getBroadcast to send broadcasts. The sent broadcasts are received in the onReceive method.
  74. PendingIntent pendingIntent = PendingIntent. getBroadcast (context, 0, intent, 0 );
  75. // Create remoteViews
  76. RemoteViews remoteViews = new RemoteViews (context. getPackageName (), R. layout. example_appwidget );
  77. // Bind the event processor to remoteViews and execute pendingIntent after a click event occurs.
  78. // The first parameter is the bound control, and the second parameter specifies the pendingIntent to be executed when an event occurs.
  79. RemoteViews. setOnClickPendingIntent (R. id. appWidgetBtn2, pendingIntent );
  80. // Update the appwidget
  81. AppWidgetManager. updateAppWidget (appWidgetIds [I], remoteViews );
  82. }
  83. // TODO Auto-generated method stub
  84. Super. onUpdate (context, appWidgetManager, appWidgetIds );

  85. }
  86. }
4. example_appwidget_info.xml
This xml file is not a layout file, it provides metadata for the AppWidget
 
 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <appwidget-provider
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:minWidth="294dp"
  5. android:minHeight="72dp"
  6. android:updatePeriodMillis="86400000"
  7. android:initialLayout="@layout/example_appwidget"
  8. />
5. example_appwiet.xml
This is the layout file of the AppWidget.
 
 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >

  6. <TextView
  7. android:id="@+id/widgetText"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="myWidget"
  11. android:background="#000000"
  12. />
  13. <Button
  14. android:id = "@+id/appWidgetBtn1"
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:text="test Button1"
  18. />
  19. <Button
  20. android:id = "@+id/appWidgetBtn2"
  21. android:layout_width="fill_parent"
  22. android:layout_height="wrap_content"
  23. android:text="test Button2"
  24. />
  25. </LinearLayout>
6. activity_main.xml
This is the layout file of MainActivity.
 
 
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >

  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="@string/hello_world" />

  14. </RelativeLayout>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.