Android's launcher launcher adds shortcuts and widget instances _android

Source: Internet
Author: User

Foreword: Has been looking at the Launcher module recently, after almost two months study, finally understood the launcher some main function realization, now continues still in
Groping state. Did not see launcher, in my case, can only worship, thought that all functions are it to achieve; Getting started, only to discover that launcher's many functions are just
Integrates the functionality provided by the framework/application. A lot of unfamiliar things, only contact to sigh: Oh, the original is so! ”

Add a shortcut

Today to share the next launcher how to implement Add shortcut (shortcut), follow-up will slowly add other aspects of the function, to help everyone "one leaf and know autumn."
Specifically, there are two types of shortcuts in launcher:
1, "pseudo" shortcut--Application type
2, "true" shortcut--activity with <action/> for action_create_shortcut configuration information
These two types of shortcuts are how to hook together, in the following people through the code to understand themselves, it is not convenient to elaborate.
Knowledge Point Introduction:
Knowledge point One, action_pick_activity use instructions, specific reference to the SDK intent class
Features: Displays all the activity that matches the added value to extra_intent and renders them as a list to the user. When the user selects an item from the list
, the activity is not started (unlike Action_chooser, this action initiates a user-selected activity), but the detailed letter of the activity
(May include action, componentname, data information, etc.) to return the intent object to the caller (usually the Onactivityresult method).

Added value: Extra_intent shows all matches showing all matches added to extra_intent activity,
Extra_title is the title of all activity as a display list.

Therefore, it takes two steps to actually create a shortcut, depending on the nature of the action_pick_activity:

Step one: Send Action_pick_activity and extra_intent to find the list of activity we want to create a shortcut to.
Step two: Send this intent object again, based on the intent object returned by the activity selected in the first step, to create the activity to provide
We have a shortcut.

For example, here we simply send a request to display the intent of all applications, as follows:

Resend an ACTION-pick_activity Intent to get all application information 
    Intent pickintent = new Intent (intent.action_pick_activity); 
    Intent mainintent = new Intent (); 
    Mainintent.setaction (intent.action_main); 
    Mainintent.addcategory (intent.category_launcher); 
    
    Pickintent.putextra (Intent.extra_intent, mainintent); 
    Pickintent.putextra (Intent.extra_title, "select Application"); Set the interface title 
    
    //continue to select all applications 
    startactivityforresult (pickintent,my_request_all_application); 


The Action_pick_activity effect chart is as follows:

By clicking on a specific activity, you can choose to create a shortcut to the activity.

Knowledge point two, Intent.shortcuticonresource class introduction
Features: Provides picture resources for shortcuts (shortcut) and folders (Live folder)
The common methods are:
public static Intent.shortcuticonresource Fromcontext (context context, int ResourceID)
Function: Create a Intent.shortcuticonresource object
Parameter Description: Context Context Class object
ResourceID the specific picture resource ID.
Common Properties:
PackageName the package name of the application, the type is Packagename:type/entityname
ResourceName ResourceID The resource name of the corresponding

For example: a picture resource R.id.icon = 0x7f020000, then resourcename is Packagename:drawable/icon

Specifically how to get the picture resources through the Intent.shortcuticonresource object, please refer to the sample demo.

Sample Demo
Note: Click to create a shortcut dialog box, select a specific shortcut, you can add to the Mainactivity interface, continue to click each view, then
You can start the app with the quick shortcut, it's very power.

PS: Because I simply use the linearlayout to when the container, there will be limitations, we can on this basis, the use of Gridview/listview to build better
Layout and, of course, more NB, to provide a custom layout similar to launcher.

Because executing a shortcut may require some specific permissions, we must configure the corresponding permissions in the Androidmanifest.xml. For example, call directly
The required permissions are: <uses-permission android:name= "Android.permission.CALL_PHONE" ></uses-permission>

As shown on the effect diagram, add a few shortcuts after the screenshot below, click to start the application.

The main engineering logic is as follows:

Package com.qin.addshortcut; 
 
Import java.util.ArrayList; 
Import android.app.Activity; 
Import Android.content.ComponentName; 
Import android.content.Intent; 
Import Android.content.Intent.ShortcutIconResource; 
Import Android.content.pm.ActivityInfo; 
Import Android.content.pm.PackageManager; 
Import android.content.pm.PackageManager.NameNotFoundException; 
Import android.content.res.Resources; 
Import Android.graphics.Bitmap; 
Import Android.graphics.Rect; 
Import android.graphics.drawable.BitmapDrawable; 
Import android.graphics.drawable.Drawable; 
Import Android.os.Bundle; 
Import android.os.Parcelable; 
Import Android.util.Log; 
Import Android.view.LayoutInflater; 
Import Android.view.View; 
Import Android.widget.Button; 
Import Android.widget.ImageView; 
Import Android.widget.LinearLayout; 
Import Android.widget.TextView; 
Import Android.widget.Toast; /** * * @author http://http://blog.csdn.net/qinjuning/public class Mainactivity extends activity implements View .Onclicklistener {private linearlayout linearlayout;//container for all shortcuts---Parent view private Button btaddshortcut; private static final int my_request_short_cut = 1;   First step, show all the activity that can create a shortcut private static final int my_create_shourt_cut = 2; Step two, create the real shortcut private static final int my_request_all_application = 3; 
  
 Step two, create pseudo shortcuts-application class private static String TAG = "addshortactivity"; 
  
 Private Packagemanager Mpackagemanager = null; /** called the activity is a. 
  * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
 
  Setcontentview (R.layout.main); 
  Btaddshortcut = (Button) Findviewbyid (r.id.bt_addshortcut); 
  LinearLayout = (linearlayout) Findviewbyid (r.id.linearlayout); 
   
  Linearlayout.setonclicklistener (this); Demonstrates how to obtain the integer descriptor//getidentifier parameter for the resource file in the R file as Package:type/entry through the type of a resource. For example, the resource descriptor for icon is as follows: int iconid = This.getresources (). Getidentifier ("Com.qin.addshOrtcut:drawable/icon ", NULL, NULL); 
   
  LOG.I (TAG, "icon ID:" + iconid); 
     
  Gets the Packagemanager object Mpackagemanager = Getpackagemanager (); 
    Btaddshortcut.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) { 
   Create Shortcut dialog box Createshortdialog (); 
   
 } 
  }); private void Createshortdialog () {//begin: Add icon to create application (' shortcuts ')//This bundle value is a list item for additional create Pick_activity Bundl 
  E bundle = new bundle (); 
  Set name arraylist&lt;string&gt; shortcutnames = new arraylist&lt;string&gt; (); 
  Shortcutnames.add ("Application"); 
  Bundle.putstringarraylist (Intent.extra_shortcut_name, shortcutnames); 
  Set the corresponding Avatar arraylist&lt;shortcuticonresource&gt; shortcuticonres= new arraylist&lt;shortcuticonresource&gt; (); 
  Shortcuticonres.add (Shortcuticonresource.fromcontext (Mainactivity.this, R.drawable.icon)); 
  Bundle.putparcelablearraylist (Intent.extra_shortcut_icon_resource, shortcuticonres); End Intent Pickintent = new Intent (intent.action_pick_activity); 
 
  Intent extraintent = new Intent (intent.action_create_shortcut); Pickintent.putextra (Intent.extra_intent, extraintent);/the activity Intent Pickintent.putextra to find (Intent.extra_ TITLE, "select Shortcut"); 
  Title//See no, pseudo shortcuts are created here. 
   
  Pickintent.putextras (bundle); 
 Startactivityforresult (Pickintent, my_request_short_cut); } protected void Onactivityresult (int requestcode, int resultcode, final Intent data) {Super.onactivityresul 
  T (Requestcode, ResultCode, data); 
   
   
  Unsuccessful selection of any shortcut (for example, press the Back button directly), directly returned if (ResultCode = = result_canceled) return; Switch (requestcode) {case my_request_short_cut://After the first send Pick_activity, select the selected list items, do the intermediate processing operation, you need to decide whether to choose "Application" or real shortcut 
   //Get Shortcut label String label = Data.getstringextra (Intent.extra_shortcut_name); This field is the one we proactively added if (Label.equals ("Application")) {//Resend an action-pick_activity Intent, get all application information Intent pickintent = newIntent (intent.action_pick_activity); 
    Intent mainintent = new Intent (); 
    Mainintent.setaction (Intent.action_main); 
     
    Mainintent.addcategory (Intent.category_launcher); 
    Pickintent.putextra (Intent.extra_intent, mainintent); Pickintent.putextra (Intent.extra_title, "select Application"); 
     
   Set the interface title//continue to select all applications Startactivityforresult (pickintent,my_request_all_application); 
    else{//re-request to create the activity of the last click, and then create the LOG.V (TAG, "My_request_short_cut Intent Info---&gt;" + data); 
   Next, create a specific shortcut Startactivityforresult (data, my_create_shourt_cut); 
    
  } break; The following actions are the actions that really handle adding shortcuts My_create_shourt_cut://data data encapsulates the value that all shortcuts should append LOG.V (TAG, "my_create_shourt_cut in 
   Tent Info---&gt; "+ data); 
   Completeaddshortcut (data); 
    
  break; Case My_request_all_application://Creating an application's "shortcuts"//data data encapsulates the click of an application's intent, including Action,componentname group information, We can then get information about the application via the intent object LOG.V (TAG, "My_requeSt_all_application Intent Info---&gt; "+ data); 
   Completeaddapplication (data); 
  break; 
    
  Default:break; }//Add an application's "shortcut" private void Completeaddapplication (Intent data) {////Print application return Intent information, a full startup application 
   
  Program Intent LOG.I (TAG, "application Intent Info----&gt;" +data); 
  ComponentName componentname = Data.getcomponent (); 
   
  LOG.I (TAG, "componentname Info----&gt;" + componentname); 
   try {//Get resource picture Activityinfo Activityinfo = Mpackagemanager.getactivityinfo (componentname, 0); 
   Charsequence Applabel = Activityinfo.loadlabel (Mpackagemanager); 
    
   drawable AppIcon = Activityinfo.loadicon (Mpackagemanager); 
   Create a View object view view = Makeviewforshortcut (Applabel, AppIcon); 
   Set the onclick monitor View.setonclicklistener (this) for view of the shortcut; 
    
   The intent object is set to the Tag property of the view, which gets the tag when it is onclick,---&gt;gettag () view.settag (data); Add the View object to LinearLayout, because the size has changed, the system will go back to "measure", "layout", "DRAW "process//Set long width high linearlayout.layoutparams llparams = new Linearlayout.layoutparams (80,90); 
  Linearlayout.addview (View,llparams); 
  catch (Namenotfoundexception e) {log.e (TAG, "namenotfoundexception at Completeaddapplication method"); }//Add shortcuts (real, non-application) private void Completeaddshortcut (Intent data) {drawable Shortcuticon = null;//Quick 
   
  The Czech style icon can be obtained in two ways, as follows if else judge//Get Shortcut label String label = Data.getstringextra (Intent.extra_shortcut_name); 
  Parcelable bitmap = Data.getparcelableextra (Intent.extra_shortcut_icon);  Direct the picture, that is, set the Extra_shortcut_icon parameter value if (bitmap!= null &amp;&amp; bitmap instanceof bitmap) {Shortcuticon = 
  New Bitmapdrawable ((Bitmap) Bitmap); else//Set Extra_shortcut_icon_resource added value {parcelable iconparcel = data. Getparcelableextra (Intent.extra_sho 
   Rtcut_icon_resource); if (iconparcel!= null &amp;&amp; iconparcel instanceof Shortcuticonresource) {//Get SHORTCUticonresource object Shortcuticonresource iconres = (shortcuticonresource) iconparcel; Gets the Inconres object's resource object try {//Get the Resources object resources = Mpackagemanager corresponding to PackageName.     
     Getresourcesforapplication (Iconres.packagename); 
     Gets the ID number of the corresponding picture int iconid = Resources.getidentifier (iconres.resourcename, NULL, NULL); 
     LOG.I (TAG, "icon identifier is" + Iconres.resourcename); 
    Get resource Picture Shortcuticon = Resources.getdrawable (iconID); 
    catch (Namenotfoundexception e) {log.e (TAG, "namenotfoundexception at Completeaddshortcut method"); The shortcuts do not set any images for us as well as the Shortcuticonresource object, we need to reset the shortcut's avatar if (Shortcuticon = null) {//will definitely 
   There are pictures, here I have a simple deal. 
   Toast.maketext (Mainactivity.this, "Sorry, we could not shortcut image", toast.length_short); 
  return; //Get shortcut Intent, direct startactivity can Intent shortcut_intent = Data.getparcelableextra (intent.extra_shortcut_inte NT); 
     
  if (shortcut_intent!= null) log.i (TAG, "shortcut Intent info----&gt;" +shortcut_intent); 
  Create a View object view view = makeviewforshortcut (label, Shortcuticon); 
  Set the onclick monitor View.setonclicklistener (this) for view of the shortcut; 
   
  The intent object is set to the Tag property of the view, which gets the tag when it is onclick,---&gt;gettag () view.settag (shortcut_intent); Add the View object to the LinearLayout, because the size has changed, the system will go back to the "measure", "layout", "Draw" process/set the long width and height linearlayout.layoutparams llparam 
   
  s = new Linearlayout.layoutparams (100,90); 
  
 Linearlayout.addview (View,llparams); 
  //Click event @Override public void OnClick (View v) {Object tag = V.gettag (); 
   if (tag!=null &amp;&amp; tag instanceof Intent) {Intent Intent = (Intent) tag; 
  Startactivityforsafely (Intent); 
   
  }///Secure start an activity private void startactivityforsafely (Intent data) {Intent launchintent = data; Some startup activity needs to set this option to set up an interface for the initiated activity, such as a contact's shortcut, so let's add it Launchintent.setsourceboUnds (New Rect (0,0,300,300)); 
   
  Launchintent.setflags (Intent.flag_activity_new_task); 
 StartActivity (launchintent); //Create a View object for each shortcut private view makeviewforshortcut (charsequence label, drawable icon) {layoutinflater Infla 
  ter = Layoutinflater.from (this); 
   
  View Shortcut_view = inflater.inflate (R.layout.shortcut_view, NULL); 
  TextView TV = (TextView) Shortcut_view.findviewbyid (R.id.shortcut_label); 
   
  Tv.settext (label); 
  ImageView img = (imageview) Shortcut_view.findviewbyid (r.id.shortcut_img); 
  Img.setimagedrawable (icon);  
 return shortcut_view; 
 } 
  
  
}


Add widget
We know:

1. Each appwidget has a Appwidgetproviderinfo object that describes the basic data (Meta-data) information for each appwidget.
It is defined in the <appwidget-provider> node information.

2, each appwidget corresponds to a Remoteviews view object, which provides a specific appwidget display (view view) and operation
(for example, clicking on the remoteviews will handle things across the process).

3. The Appwidgetmanager class maintains all the appwidget in the application and identifies them for each appwidget specific ID (generally we
To identify with a appwidgetid). The corresponding appwidget can be managed by a given appwidgetid,appwidgetmanager, for example: updating the
Appwidgetid remoteviews View, delete the Appwidget object, and so on.

4, Appwidgetprovider broadcast class is a listener, the system to appwidget operations (for example, create and update, etc.) distributed to
Appwidgetprovider class to deal with.


For each appwidget, we can create multiple instances of it, and of course these instances correspond to different appwidgetid. Assuming there is such a
Myappwidgetprovider broadcast class, and the corresponding Myappwidgetproviderinfo object. Then, the following relationships exist:

Myappwidgetprovider.class: Represents the type of widget (Appwidget) created by the Myappwidgetprovider, typically
The Compontentname object is represented as a form. Then there are the following relationships:

From above, however, each appwidget corresponds to a Myappwidgetprovider class, so when any one of the appwidgetid changes, we need
Synchronize other instances to maintain synchronization.

Appwidgetproviderinfo Class Supplementary Note:
Publiccomponentnameconfigure: Typically an activity that indicates the complex need to manage appwidget creation operations.
public int updateperiodmillis: Used to update appwidget, but the attribute was abolished in SDK1.5

Appwidgetprovider Class Introduction:
Common methods:
Ondeleted (): This method is called when the appwidget of the type is deleted each time
Ondisabled (): This method is called when the widget (Appwidget) for that type is completely deleted
Onenabled (): This method is called when the appwidget of the type is created the first time
OnReceive (): Broadcast recipient method, used to accept broadcast messages
OnUpdate (): This method is called every time the appwidget of the type is created, and usually we need to specify the Appwidget in the method
Remoteviews object.

Appwidgetmanager Class Introduction:
Common constants:
action_appwidget_pick= "Android.appwidget.action.APPWIDGET_PICK"
Description: Lists all the objects that can create Appwidget, which is generally a custom Appwidgetprovider broadcast recipient.
Note: You must add additional value to send this intent: extra_appwidget_id.
The extra_appwidget_id meaning: the Appwidgetid with us sends the action to Action_appwidget_pick after
The selected Appwidget binding. Therefore, we can get the information of the appwidget through this appwidgetid.

Action_appwidget_configure= "Android.appwidget.action.APPWIDGET_CONFIGURE"
Note: If the selected Appwidget is configured with the Android:configure attribute, it is necessary to start the corresponding activity again, and then further select
Appwidget. Simultaneous sending of the intent must add added value: extra_appwidget_id, meaning ditto.

Common methods:

Public int[] Getappwidgetids (componentname provider)

function: Get all appwidgetid corresponding to componentname type
Parameter description: Provider is usually xxxappprovider.class type

Publicappwidgetproviderinfo getappwidgetinfo (int appwidgetid) 

Function: Get a specific appwidgetid corresponding Appwidgetproviderinfo object

Public Staticappwidgetmanager getinstance (contextcontext)

Function: Get Appwidgetmanager Object

public void Updateappwidget (int appwidgetid, remoteviews views)


Features: Updates the Appwidgetid widget (appwidget) with a specific views view. Action_appwidget_update broadcasts are also sent

public void Updateappwidget (int[] appwidgetids,remoteviews views)

Features: Updates all Appwidgetids widget (appwidget) with a specific views view and sends Action_appwidget_update
Broadcasting

public void Updateappwidget (componentname provider, remoteviews views)

Feature: All Widgets (Appwidget) with a specific view Update component type Provider are sent at the same time Action_appwidget_update
Broadcasting.

Sample Demo:

Description: Create a simple Appwidget instance, click on the button can be more this picture resource display, the specific code in
Screenshot is:

On how to create a appwidget textbook, I will not say more, you can refer to the above I mentioned the two heavyweight blog to learn:

1, the SDK to Appwidget introduction
2, Android Desktop Components "widget" exploration

PS: The specific code can be downloaded later.

Again, each appwidget corresponds to Appwidgetprovider, and we need to update these Appwidget objects synchronously.
Add a widget to your application
The main function of this section is to add appwidget like Launchcer. The knowledge points are as follows:

Appwidgethost class

Features: For each app app, this class provides and Appwidgetservice (the Appwidgetservice is used to manage all appwidget, similar to
Notificationmanagerservice system service Management All notifciation, do not understand? In fact, I do not understand, to the effect can be interactive, used to update, manage
Appwidget. Metaphor: Appwidgethost is the host object, each appwidget is a parasite and can be attached to (display) appwidgethost.
Each activity that can add and display Appwidget is a Appwidgethost object, such as Launcher.java (the Activity object), and we will later customize
A righteous Mainactivity.java (activity object).

The common methods are:
Public Appwidgethost (context context, int hostId)
function: Construct a Appwidgethost object
Parameter: HostId is the Appwidgethost (host object) corresponding to the ID number, generally given an integer can be.
public int Allocateappwidgetid ()
Function: Request a new appwidgetid that will be bound to the newly created appwidget.
public void startlistening ()
Function: Monitor all appwidget changes, this method must be called in the activity's OnCreate ()/onstart (), otherwise appwidget will not be updated
public void stoplistening ()
Function: Corresponds to startlistening (), that is, stop the update listening to the Appwidget. Can be invoked in the OnStop () method of the activity,
It is generally not necessary to call this method to stop listening.

Public final Appwidgethostview CreateView (context, int appwidgetid, Appwidgetproviderinfo appwidget)
Function: Constructs a Appwidgethostview object based on the specified Appwidgetid and Appwidgetproviderinfo objects (the object,
See below).


the difference between Appwidgethostview and Remoteviews

For each appwidget, there is a Remoteviews object for the view display, and for the outside, the Remoteviews view is endorsed in Appwidgethostview form. In other words, Appwidgethost object, it does not know remoteviews exist, but only Remoteviews spokesperson Appwidgethostview.


Well, the instructions are all explained, and here's how to add Widgets (Appwidget) to your application.
is also two steps to go:

Step one: Send the Intent with action as Action_appwidget_pick, then all the appwidgetprovider that can create widgets will be broadcast
The recipient displays and assigns a appwidgetid to the newly created appwidget, the Appwidgetid uniquely marks the
Appwidget.

Step two: If the selected Appwidget corresponds to the Appwidgetproviderinfo object configured with the Android:configure attribute, you need to start the configuration here
Property (typically an activity class), and then completes adding appwidget; otherwise, the Android:configure property is not configured to add
Appwidget the operation.

Sample Demo screenshot:

The main engineering process is as follows:

Package com.qin.addappwidget; 
Import android.app.Activity; 
Import Android.appwidget.AppWidgetHost; 
Import Android.appwidget.AppWidgetHostView; 
Import Android.appwidget.AppWidgetManager; 
Import Android.appwidget.AppWidgetProviderInfo; 
Import android.content.Intent; 
Import Android.os.Bundle; 
Import Android.util.Log; 
Import Android.view.View; 
Import Android.widget.Button; 
Import Android.widget.ImageView; 
Import Android.widget.LinearLayout; 
Import Android.widget.TextView; 
 
Import Android.widget.Toast; 
  
 public class Mainactivity extends activity {private static String TAG = "Addappwidget"; 
 Private Button btaddshortcut; Private LinearLayout LinearLayout; 
 Mount Appwidget's parent view private static final int my_request_appwidget = 1; 
  
 private static final int my_create_appwidget = 2; 
  
 private static final int host_id = 1024; 
 Private Appwidgethost mappwidgethost = null; 
  
 Appwidgetmanager Appwidgetmanager = null; /** called is fiRST created. 
  * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
 
  Setcontentview (R.layout.main); 
 
  Btaddshortcut = (Button) Findviewbyid (r.id.bt_addshortcut); 
  
  LinearLayout = (linearlayout) Findviewbyid (r.id.linearlayout); 
  Its parameter is hostid to specify the tag ID of the Appwidgethost, which is the activity, and set it directly to an integer value. 
   
  Mappwidgethost = new Appwidgethost (mainactivity.this, host_id); In order to ensure the timely updating of appwidget, it is necessary to call the method in the Oncreate/onstar method of the activity//certainly in the OnStop method, call Mappwidgethost.stoplistenering () 
   
  Stop Appwidget update mappwidgethost.startlistening (); 
   
   
  Obtain Appwidgetmanager Object Appwidgetmanager = Appwidgetmanager.getinstance (mainactivity.this); 
     Btaddshortcut.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) { Show all the lists that can create appwidget send this action_appwidget_pick ACTION Intent pickintent = new Intent (APPWIDGETMANAGER.ACTION_APPW 
     
     Idget_pick); Apply to the system for a new AppwidgetiD, the Appwidgetid is bound with the appwidget we have chosen to send the action to Action_appwidget_pick//. Therefore, we can obtain the Appwidget information through this Appwidgetid//request a new Appwidgetid int newappwidgetid = mappwidgethost.al for the current process. 
     Locateappwidgetid (); 
     
     LOG.I (TAG, "The new allocate Appwidgetid is----&gt;" + newappwidgetid); As a intent added value, the Appwidgetid will bind to the selected Appwidget Pickintent.putextra (appwidgetmanager.extra_appwidget_id, Newappwid 
     
     GETID); 
         
   When a appwidget is selected, it returns immediately, that is, the callback Onactivityresult () method Startactivityforresult (Pickintent, my_request_appwidget); 
 } 
  }); //If protected void onactivityresult (int requestcode, int resultcode, Intent data) {//Direct return, no item is selected, for example, by BAC 
   
  K-key if (ResultCode = = result_canceled) return;  Switch (requestcode) {case my_request_appwidget:log.i (TAG, "My_request_appwidget Intent Info-----&gt;" +data 
    ) ; int appwidgetid = Data.getintextra (appwidgetmanager.extra_appwidget_id, appwidgetmanager.inVALID_APPWIDGET_ID); 
     
    LOG.I (TAG, "My_request_appwidget:appwidgetid is----&gt;" + appwidgetid); Obtained for valid ID if (Appwidgetid!= appwidgetmanager.invalid_appwidget_id) {//query specified Appwidgetid appwidgetproviderinfo pair Like, that is, in the XML file configuration &lt;appwidget-provider/&gt; node information appwidgetproviderinfo appwidgetproviderinfo = Appwidgetmanager.getapp 
      
     Widgetinfo (Appwidgetid); If the Configure property is configured, that is, android:configure = "", the class file specified by the Configure needs to be started again, typically an activity if (Appwidgetproviderinfo.configur e!= null) {LOG.I (TAG, "The Appwidgetproviderinfo Configure info-----&gt;" + appwidgetproviderinfo.config 
       
      URE); 
      Configure this ACTION Intent Intent = new Intent (appwidgetmanager.action_appwidget_configure); 
      Intent.setcomponent (appwidgetproviderinfo.configure); 
       
       
      Intent.putextra (appwidgetmanager.extra_appwidget_id, Appwidgetid);     
     Startactivityforresult (Intent, my_create_appwidget); } elsE//Directly create a appwidget Onactivityresult (My_create_appwidget, RESULT_OK, data); 
   parameter is different, simple callback only} break; 
    Case My_create_appwidget:completeaddappwidget (data); 
  break; 
  }///Add a user-selected private void Completeaddappwidget (Intent data) {Bundle extra = Data.getextras () to the current view; 
  int appwidgetid = Extra.getint (appwidgetmanager.extra_appwidget_id,-1); Equivalent to the above acquisition method//int Appwidgetid = Data.getintextra (appwidgetmanager.extra_appwidget_id, appwidgetmanager.invalid_ 
   
  APPWIDGET_ID); 
   
  LOG.I (TAG, "Completeaddappwidget:appwidgetid is----&gt;" + appwidgetid); 
   if (Appwidgetid = = 1) {Toast.maketext (Mainactivity.this, "Add widget Error", Toast.length_short); 
  return; 
   
  } appwidgetproviderinfo appwidgetproviderinfo = Appwidgetmanager.getappwidgetinfo (appWidgetId); 
     
  Appwidgethostview Hostview = Mappwidgethost.createview (Mainactivity.this, Appwidgetid, AppWidgetProviderInfo); Linearlayout.addview (HOstview); 
  int widget_minwidht = Appwidgetproviderinfo.minwidth; 
  int widget_minheight = Appwidgetproviderinfo.minheight; Sets the MinWidth and MinHeight properties of a long-width appwidgetproviderinfo object Linearlayout.layoutparams linearlayoutparams = new LinearLayou 
  T.layoutparams (WIDGET_MINWIDHT, widget_minheight);   
 Add to the LinearLayout parent view Linearlayout.addview (Hostview,linearlayoutparams); 
 } 
}

        
Finally, about the corresponding events triggered after the Appwidget click, is triggered by setting a control click event for the Appwidget's remoteviews.

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.