wc_home_widget.xml
<RelativeLayout android:id="@+id/widget_layout"xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#08458c" >
<TextView
android:id="@+id/widget_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:text="12:00 PM"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/day_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/widget_textView"
android:layout_below="@+id/widget_textView"
android:layout_marginTop="16dp"
android:text="Day"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/date_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/day_textView"
android:layout_alignBottom="@+id/day_textView"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/day_textView"
android:text="currentDate"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
TimeWidget.java
package com.rajeshvijayakumar.widget;import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RemoteViews;
import android.widget.Toast;
public class TimeWidget extends AppWidgetProvider implements OnClickListener {
private Date mCurrentTime;
private int mYear;
private PendingIntent pendingIntent;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Toast.makeText(context, "onUpdate", Toast.LENGTH_SHORT).show();
RemoteViews remoteViews1 = new RemoteViews(context.getPackageName (), R.layout.wc_home_widget);
// Intent intent = new Intent(context, AddLocationActivity.class);
// pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
private class MyTime extends TimerTask {
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault());
public MyTime(Context context, AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.wc_home_widget);
remoteViews.setOnClickPendingIntent(R.id.widget_layout, pendingIntent);
thisWidget = new ComponentName(context, TimeWidget.class);
}
@Override
public void run() {
mCurrentTime = new Date();
remoteViews.setTextViewText(R.id.widget_textView, "" + format.format(mCurrentTime));
remoteViews.setTextViewText(R.id.day_textView, ""+DateTimeUtils.getDayName(mCurrentTime.getDay()));
remoteViews.setTextViewText(R.id.date_textView, ""+mCurrentTime.getDate()+" "+DateTimeUtils.getMonthName(mCurrentTime.getMonth()));
// MMM
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
final int appWidgetId = intent.getExtras().getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
this.onDeleted(context, new int[] { appWidgetId });
}
} else {
super.onReceive(context, intent);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
Toast.makeText(context, "onDelete", Toast.LENGTH_SHORT).show();
super.onDeleted(context, appWidgetIds);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
DateTimeUtils.java
package com.rajeshvijayakumar.widget;import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class DateTimeUtils {
private static Calendar cal;
public static void setTimeZoneForApp(String timezoneId) {
cal = new GregorianCalendar(TimeZone.getTimeZone(timezoneId));
}
public static Calendar getTimeZoneForApp() {
return cal;
}
public static String getCountryTime(String timeZoneId, boolean countryTime) {
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone(timeZoneId));
int minutes = cal.get(Calendar.MINUTE); // 0..59
int seconds = cal.get(Calendar.SECOND); // 0..59
String min12 = (minutes < 10) ? "0" + minutes : "" + minutes;
String sec12 = (seconds < 10) ? "0" + seconds : "" + seconds;
boolean am = cal.get(Calendar.AM_PM) == Calendar.AM;
String amorpm = (am == true) ? "AM" : "PM";
String hour = "";
if (countryTime == true) {
int hour12 = cal.get(Calendar.HOUR); // 0..11
int hr12 = (hour12 == 0) ? 12 : hour12;
hour = hr12 + ":" + min12.trim()+ " " + amorpm;
} else {
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
int hour24 = cal.get(Calendar.HOUR_OF_DAY); // 0..23
String hr24 = (hour24 < 0) ? "0" + hour24 : "" + hour24;
hour = hr24 + ":" + min12;
}
return hour;
}
public static String getCurrentDate(String format,
boolean displayCurrentDate) {
if (displayCurrentDate == true) {
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat(format);
String formatted = df.format(c.getTime());
return formatted;
} else {
return "";
}
}
public static String getCountryDate(String timeZoneId,
boolean displayCountryDate) {
if (displayCountryDate == true) {
Calendar cal = new GregorianCalendar(
TimeZone.getTimeZone(timeZoneId));
int date = cal.get(Calendar.DATE);
int month = cal.get(Calendar.MONTH);
String monthName = getMonthName(month);
int year = cal.get(Calendar.YEAR);
return +date + "-" + monthName + "-" + year;
} else {
return "";
}
}
public static String getDayName(int dayNo) {
String dayName = "";
switch (dayNo) {
case 0:
dayName = "Sun";
break;
case 1:
dayName = "Mon";
break;
case 2:
dayName = "Tue";
break;
case 3:
dayName = "Wed";
break;
case 4:
dayName = "Thu";
break;
case 5:
dayName = "Fri";
break;
case 6:
dayName = "Sat";
break;
}
return dayName;
}
public static String getMonthName(int month) {
String monthName = "";
switch (month) {
case 0:
monthName = "Jan";
break;
case 1:
monthName = "Feb";
break;
case 2:
monthName = "Mar";
break;
case 3:
monthName = "Apr";
break;
case 4:
monthName = "May";
break;
case 5:
monthName = "Jun";
break;
case 6:
monthName = "Jul";
break;
case 7:
monthName = "Aug";
break;
case 8:
monthName = "Sep";
break;
case 9:
monthName = "Oct";
break;
case 10:
monthName = "Nov";
break;
case 11:
monthName = "Dec";
break;
}
return monthName;
}
}
Manifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rajeshvijayakumar.widget"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name=".TimeWidget"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/hello_widget_provider" />
</receiver>
</application>
</manifest>
Ouput :
GitHub : https://github.com/rajeshmcashc10/rajeshvijayakumar-android/tree/master/widgetdemo
No comments:
Post a Comment