Android development-desktop plug-in

Source: Internet
Author: User

Three steps are required to develop a plug-in displayed on the mobile phone desktop:

1. inherit from appwidgetprovider

2. Compile the widget interface XML and write the mywidget attribute XML.

3. androidmanifest. xml registration plug-in

Take the auto-increment number as an example.

1. inherit from appwidgetprovider

Public class mywidgetprovider extends appwidgetprovider
{
Private Static timer mytimer;
Private Static int Index = 0;

Private final string broadcaststring = "knowheart. WD. appwidgetupdate ";

@ Override
Public void ondeleted (context, int [] appwidgetids)
{
// Todo auto-generated method stub
Super. ondeleted (context, appwidgetids );

}

@ Override
Public void onenabled (context)
{
// Todo auto-generated method stub
Super. onenabled (context );

}

@ Override
Public void onupdate (context, appwidgetmanager,
Int [] appwidgetids)
{
// Todo auto-generated method stub
Super. onupdate (context, appwidgetmanager, appwidgetids );
}



@ Override
Public void onreceive (context, intent)
{
Super. onreceive (context, intent );
}

}

 

Add a class to inherit appwidgetprovider and control the update and deletion operations of our plug-ins. The related methods include onenabled, onupdate, and onreceive.

2. Compile the widget's XML interface and write the mywidget property XML

Layout/widget_layout.xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: Background = "#000000"
Android: layout_gravity = "center_horizontal"
Android: layout_width = "300dp"
Android: layout_height = "wrap_content">

<Textview
Android: Id = "@ + ID/update"
Android: text = "0"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: gravity = "center"
/>

</Linearlayout>

 

Create an XML folder and add the XML file: XML/mywidget. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Appwidget-provider xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: minwidth = "300dip" Android: minheight = "72dp"
Android: updateperiodmillis = "86400000"
Android: initiallayout = "@ layout/widget_layout"
>
</Appwidget-provider>
Set the plugin length and width, refresh time (0 indicates manual refresh), and plug-in interface XML

3. androidmanifest. xml registration plug-in

<! -- Register the plug-in -->
<Cycler Android: Name = "mywidgetprovider">
<Intent-filter>
<Action Android: Name = "android. appwidget. Action. appwidget_update"/>

<Action Android: Name = "com. WD. appwidgetupdate">
</Action>
</Intent-filter>

<Meta-data
Android: Name = "android. appwidget. provider"
Android: Resource = "@ XML/mywidget"/>
</Cycler>

 

<Action Android: Name = "com. WD. appwidgetupdate"> Register a broadcast with the system. During logic processing, the broadcast is sent to the appwidgetprovider's update method for manual refreshing.

At this point, the expected functions are basically implemented !!!!!

The number displayed by the plug-in/1000 ms + 1 must be processed in the background.

Public class mywidgetprovider extends appwidgetprovider
{
Private Static timer mytimer;
Private Static int Index = 0;

// Define the event we want to send
Private final string broadcaststring = "com. WD. appwidgetupdate ";

@ Override
Public void ondeleted (context, int [] appwidgetids)
{
// Todo auto-generated method stub
Super. ondeleted (context, appwidgetids );

System. Out. println ("ondeleted ");
}

@ Override
Public void onenabled (context)
{
System. Out. println ("onenabled ");
// Todo auto-generated method stub
Super. onenabled (context );

// This will be called when the plug-in is created, so we enable a timer to run once per second.
Mytask mmytask = new mytask (context );
Mytimer = new timer ();
Mytimer. Schedule (mmytask, 1000,100 0 );
System. Out. println ("onenabled2 ");
}



@ Override
Public void onupdate (context, appwidgetmanager,
Int [] appwidgetids)
{
System. Out. println ("onupdate ");
// Todo auto-generated method stub
Super. onupdate (context, appwidgetmanager, appwidgetids );
}



@ Override
Public void onreceive (context, intent)
{
// When it is determined that the event was sent, we will obtain the plug-in interface, and then import the index self-added to textview.
System. Out. println ("onreceive ");
If (intent. getaction (). Equals (broadcaststring ))
{
Index ++;

Remoteviews Rv = new remoteviews (context. getpackagename (), R. layout. widget_layout );

RV. settextviewtext (R. Id. Update, integer. tostring (INDEX ));

// Display the interface to the plug-in
Appwidgetmanager = appwidgetmanager. getinstance (context );

Componentname = new componentname (context, mywidgetprovider. Class );

Appwidgetmanager. updateappwidget (componentname, RV );
}
// Todo auto-generated method stub
Super. onreceive (context, intent );
}



Class mytask extends timertask
{

Private context mcontext = NULL;
Private intent = NULL;

Public mytask (context ){

// Create a new intent
Mcontext = context;
Intent = new intent ();
Intent. setaction (broadcaststring );
}
@ Override
Public void run ()
{
System. Out. println ("2 ");
// Send broadcast (received by onreceive)
Mcontext. sendbroadcast (intent );
}

}
}

Related Article

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.