Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- appwidget_provider.xml
- <?xml version="1.0" encoding="utf-8"?>
- <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
- android:minWidth="294dp"
- android:minHeight="72dp"
- android:updatePeriodMillis="86400000"
- android:initialLayout="@layout/appwidget">
- </appwidget-provider>
- appwidget.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <AnalogClock
- android:id="@+id/myAnalogClock"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- </AnalogClock>
- <TextView
- android:id="@+id/myText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- AppWidgetProvider.java
- public class AppWidgetProdiverDemo extends AppWidgetProvider{
- int mYear;
- int mMonth;
- int mDay;
- int mHour;
- int mMinutes;
- String str;
- @Override
- public void onDeleted(Context context, int[] appWidgetIds) {
- // TODO Auto-generated method stub
- super.onDeleted(context, appWidgetIds);
- }
- @Override
- public void onDisabled(Context context) {
- // TODO Auto-generated method stub
- super.onDisabled(context);
- }
- @Override
- public void onEnabled(Context context) {
- // TODO Auto-generated method stub
- super.onEnabled(context);
- }
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
- super.onReceive(context, intent);
- }
- @Override
- public void onUpdate(Context context, AppWidgetManager appWidgetManager,
- int[] appWidgetIds) {
- // TODO Auto-generated method stub
- super.onUpdate(context, appWidgetManager, appWidgetIds);
- Timer timer=new Timer();
- timer.scheduleAtFixedRate(new MyTime(context,appWidgetManager), 1, 60000);
- }
- class MyTime extends TimerTask{
- RemoteViews remoteViews;
- AppWidgetManager appWidgetManager;
- ComponentName thisWidget;
- public MyTime(Context context, AppWidgetManager appWidgetManager) {
- // TODO Auto-generated constructor stub
- this.appWidgetManager=appWidgetManager;
- remoteViews=new RemoteViews(context.getPackageName(),R.layout.appwidget);
- thisWidget=new ComponentName(context, AppWidgetProdiverDemo.class);
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- try{
- long time=System.currentTimeMillis();
- Calendar mCalendar=Calendar.getInstance();
- mCalendar.setTimeInMillis(time);
- mYear=mCalendar.get(Calendar.YEAR);
- mMonth=mCalendar.get(Calendar.MONTH);
- mDay=mCalendar.get(Calendar.DAY_OF_MONTH);
- mHour=mCalendar.get(Calendar.HOUR_OF_DAY);
- mMinutes=mCalendar.get(Calendar.MINUTE);
- str=mYear+"?"+mMonth+"?"+mDay+"?"+mHour+"?"+mMinutes+"?";
- remoteViews.setTextViewText(R.id.myText,str);
- appWidgetManager.updateAppWidget(thisWidget, remoteViews);
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
- }
- AndroidManifest.xml
- <receiver android:name="AppWidgetProdiverDemo">
- <intent-filter>
- <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
- </intent-filter>
- <meta-data android:name="android.appwidget.provider"
- android:resource="@xml/appwidget_provider" />
- </receiver>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement