Android implementation widget Clock sample sharing _android

Source: Internet
Author: User
Tags current time

Configuring Widgets in Androidmanifest.xml Files:

Copy Code code as follows:

<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.example.widget"
Android:versioncode= "1"
Android:versionname= "1.0" >
<uses-sdk
Android:minsdkversion= "8"
Android:targetsdkversion= "/>"
<application
Android:allowbackup= "true"
android:icon= "@drawable/ic_launcher"
Android:label= "@string/app_name"
Android:theme= "@style/apptheme" >
<receiver android:name= ". Timewidgetprovider ">
<intent-filter>
<action android:name= "Android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data
Android:name= "Android.appwidget.provider"
Android:resource= "@xml/timewidget_info"/>
</receiver>
<service android:name= ". Timerservice "></service>
</application>
</manifest>

II. create an XML directory in the project's res directory, creating a Timewidget_info.xml file with the following contents:

Copy Code code as follows:

<?xml version= "1.0" encoding= "UTF-8"?>
<appwidget-provider xmlns:android= "Http://schemas.android.com/apk/res/android"
android:initiallayout= "@layout/time_appwidget"
android:minheight= "40DP"
Android:minwidth= "40DP"
android:updateperiodmillis= "0"/>

Third, in the Layout folder to establish the file Time_appwidget.xml:

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical"
android:background= "@drawable/rectangle"
>
<textview
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:id= "@+id/textview"
android:text= "Current Time"
/>
</LinearLayout>

Four, in the Drawable folder to establish Rectangle.xml file (this can be omitted, mainly to beautify the TextView control, gradient effect):

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<shape xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:shape= "Rectangle" >

<!--set radians-->
<corners android:radius= "9DP"/>

<gradient
Android:angle= "270"
Android:endcolor= "#5EADF4"
Android:startcolor= "#B3F0FF"/>
<padding
Android:bottom= "5DP"
android:left= "5DP"
android:right= "5DP"
android:top= "5DP"/>
<stroke
android:dashgap= "1DP"
Android:dashwidth= "10DP"
Android:width= "6DP"
Android:color= "#0000FF"/>
</shape>

Five, the background code implementation:

Copy Code code as follows:

Package com.example.widget;

Import Android.appwidget.AppWidgetManager;
Import Android.appwidget.AppWidgetProvider;
Import Android.content.Context;
Import android.content.Intent;

public class Timewidgetprovider extends Appwidgetprovider {
@Override
public void OnUpdate, Appwidgetmanager Appwidgetmanager,
Int[] appwidgetids) {
Super.onupdate (context, Appwidgetmanager, appwidgetids);
}
When a widgets is invoked
public void ondeleted (context context, int[] appwidgetids) {
TODO auto-generated Method Stub
Super.ondeleted (context, appwidgetids);
}
The first time you add a widgets to the desktop, it is invoked, and then the same type widgets is not invoked
public void onenabled {
Context.startservice (New Intent (context, timerservice.class));
}
is called when the last widgets is deleted from the desktop
public void ondisabled {
Context.stopservice (New Intent (context, timerservice.class));
}
}

Copy Code code as follows:

Package com.example.widget;

Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Java.util.Timer;
Import Java.util.TimerTask;

Import Android.annotation.SuppressLint;
Import Android.app.Service;
Import Android.appwidget.AppWidgetManager;
Import Android.content.ComponentName;
Import android.content.Intent;
Import Android.os.IBinder;
Import Android.widget.RemoteViews;

public class Timerservice extends Service {
private timer timer;

@Override
public void OnCreate () {
Super.oncreate ();
Timer = new timer ();
Timer.schedule (New Mytimertask (), 0, 1000);
}
Private Final class Mytimertask extends timertask{
@SuppressLint ("SimpleDateFormat")
@Override
public void Run () {
SimpleDateFormat sdf= New SimpleDateFormat ("Hh:mm:ss");
String time = Sdf.format (new Date ());
Get Widgets Manager
Appwidgetmanager Widgetmanager =appwidgetmanager.getinstance (Getapplicationcontext ());
The remote view of the widget corresponding to the Widgetmanager is the layout file of the current widget
Remoteviews RemoteView = new Remoteviews (Getpackagename (), r.layout.time_appwidget);
Remoteview.settextviewtext (R.id.textview, time);
The world that triggers when you click Widgets
Remoteview.setonclickpendingintent (Viewid, pendingintent)
ComponentName componentname = new ComponentName (Getapplicationcontext (), timewidgetprovider.class);
Widgetmanager.updateappwidget (ComponentName, RemoteView);
}
}
@Override
public void OnDestroy () {
Timer.cancel ();
Timer=null;
Super.ondestroy ();
}
@Override
Public IBinder Onbind (Intent Intent) {
return null;
}
}

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.