Showing posts with label widget. Show all posts
Showing posts with label widget. Show all posts

Mar 5, 2014

Digital clock Appwidget using Alarm manager




First you make Appwidget , if you need reference, can reference or my example.

Then you will layout digital clock on your widget.

Fortunately, you can see 'Digital Clock' widget view.

If you use 'Digital Clock'widget on your widget, can seen be below error image.




Because widget not support 'Digital Clock'. Just support 'Analog Clock'.

You can see reference, support widget classes.


A RemoteViews object (and, consequently, an App Widget) can support the following layout classes:
And the following widget classes:
So, you will make a Digital Clock using text view and alarm manager.

Step 1. make a text view.


widget.xml
  1.  <TextView
  2.                     android:id="@+id/widgetclock"
  3.                     android:layout_width="184dp"
  4.                     android:layout_height="71dp"
  5.                     android:gravity="center"
  6.                     android:textColor="#ffffffff"
  7.                     android:text="12:12"
  8.                     android:textSize="60dp"
  9.                     android:textStyle="bold" />
You will typing 'id'.

This is important.

Step 2. register alarm manager.

widget.java

  1. @Override
  2.     public void onUpdate(Context context, AppWidgetManager appWidgetManager,
  3.             int[] appWidgetIds) {
  4.        
  5.         super.onUpdate(context, appWidgetManager, appWidgetIds);
  6.         setTime(context);
  7.         ...
You will typing function for initialize  in 'onUpdate' method.

  1.     public void setTime(Context context){
  2.         PendingIntent service = null;
  3.         final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  4.         final Intent si = new Intent(context, alarmService.class);
  5.         if (service == null)
  6.         {
  7.             service = PendingIntent.getService(context, 0, si, PendingIntent.FLAG_CANCEL_CURRENT);
  8.         }
  9.         Log.d("timeService", "start");
  10.         m.setRepeating(AlarmManager.RTC, System.currentTimeMillis(),1000,service);
  11.     }
This function is register alarm manager and start service.

Important is 'setRepeating'. This method is repeated send service every 1sec in my case.

1000 is millisecond unit.

Step 3. make a service.java

service.java

  1.     @Override
  2.     public int onStartCommand(Intent intent, int flags, int startId)
  3.     {
  4.         update();
  5.         return super.onStartCommand(intent, flags, startId);
  6.     }
If service start when 'onStartCommand' start.

So, need a your function in 'onStartCommand' method.

  1.     private void update()
  2.     {
  3.             String time = getTime();
  4.         AppWidgetManager manager = AppWidgetManager.getInstance(this);
  5.         ComponentName weatherwidget = new ComponentName(this, WeatherWidget.class);
  6.         int[]ids = manager.getAppWidgetIds(weatherwidget);
  7.         final int N = ids.length;
  8.         for (int i = 0; i < N; i++){
  9.             int awID = ids[i];
  10.             RemoteViews views = new RemoteViews(getPackageName(), R.layout.widgetlayoutx2);
  11.             views.setTextViewText(R.id.widgetclock,time);          
  12.             manager.updateAppWidget(awID, views);
  13.         }
  14.     }
This function is update your digital clock on appwidget.

First highlight is getting clock infomation.

Second highlight is setting clock in your widget.

  1.     public String getTime(){
  2.        
  3.         String time=null;
  4.         Date date=new Date(System.currentTimeMillis());
  5.         SimpleDateFormat CurTimeFormat=new SimpleDateFormat("HH:mm");
  6.         time=CurTimeFormat.format(date);
  7.         return time;
  8.     }
This is getting current time using java api.

Step last. register AndroidManifest.xml

  1. <service android:enabled="true" android:name=".alarmService" />
If you don't register Manifest, service doesn't work.


Also, you will use alarm manager for widget update.

Because, android widget update time is minimum 30 min.


Feb 26, 2014

Weather app V5 (add widget& bug fix)


This version is completed weather widget version(maybe~)

I changed receiver class.(inner class->outer class)

So you can be seen "MainReceiver.java", new java file.

This file being communication  "WeatherWidget.java",appwidgetprovider using broadcasting.

You can be seen below displays.

This is first display. You can possible to choose that how to getting weather.

First , you choose location using spinner and touched '선택위치 보기' button.

Second , you touched '현재위치 보기' button.
This button using GPS and geocoding.

This picture is using widget.
You can installed short cut in main display.

App will be automatically getting weather ,if you touched '업데이트' button.


This version is complete widget version.(maybe)

Why do know I wrote 'maybe'?

I think many test this app.

Because I using database in  two type files.(MainActivity & MainReceiver)

Now I don't found bug. But need a many times test.

And I changed size of spinner.(static->wrap_content)

And I fixed bug in getaddress class(geocoding parser)







Feb 25, 2014

Weather app V4.99(add widget version)-but not completed version





This version added weather widget and progress.

Widget has 2 buttons.

First is go to weather app.(This is same old version)

Second is update button.(This button use to getting weather using GPS in this app)



If you install & run this app,can be seen this display.

And exit app(push back key) then go to widget menu.

And you add widget in main display.

If you touched '업데이트'button,can be seen this.
Problem 1
Exception!!
Maybe widget doesn't use method in mainactivity



But If you touched first '앱으로'button, can be seen this(updated widget).

Problem 2
Maybe about several minute after(I don't know exactly) you touched '업데이트' button, can be seen exception. 







Feb 24, 2014

android widget



This is a  android basic widget.

1. install this app.

2. go to widget.(long button on main display or menu button)

3. make a short cut in main display

4. run


Look at the widget menu. app installed in widget menu.


You make a short cut in main display.

Run app.

This app is basic. 

because just display activity.

If you want data communication, you will use broadcast or service.


MyWidget.java

  1. public class MyWidget extends AppWidgetProvider {
  2.     @Override
  3.     public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
  4.        
  5.         super.onUpdate(context, appWidgetManager, appWidgetIds);
  6.         int appId;
  7.         for(int i=0;i<appWidgetIds.length;i++){
  8.             appId=appWidgetIds[i];
  9.             Intent intent=new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://m.daum.net"));
  10.             PendingIntent pe=PendingIntent.getActivity(context, 0, intent, 0);
  11.             RemoteViews views =  new RemoteViews(context.getPackageName(),R.layout.activity_main);
  12.            
  13.             views.setOnClickPendingIntent(R.id.eventBtn, pe);
  14.             appWidgetManager.updateAppWidget(appId, views);
  15.              
  16.             }
  17.     }
  18. }

activity_main.xml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.    xmlns:tools="http://schemas.android.com/tools"
  3.    android:layout_width="match_parent"
  4.    android:layout_height="match_parent"
  5.    android:orientation="vertical"
  6.    tools:context=".MainActivity" >
  7.     <Button
  8.        android:id="@+id/eventBtn"
  9.        android:layout_width="match_parent"
  10.        android:layout_height="wrap_content"
  11.        android:text="GOGOGO" />
  12.            
  13. </LinearLayout>
This is a widget layout


If you choose Graphical Layout,you can be seen this display.

res/xml/widget_config.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <appwidget-provider
  3.    xmlns:android="http://schemas.android.com/apk/res/android"
  4.    android:minWidth= "294dp"
  5.    android:minHeight="72dp"
  6.    android:updatePeriodMillis="1000"
  7.    android:initialLayout="@layout/activity_main"
  8.    />
This is a widget config file.

You can configuration update time, widget size. etc...

AndroidManifest.xml
  1. <receiver android:name="MyWidget">                       
  2.             <meta-data android:name="android.appwidget.provider"       
  3.                 android:resource="@xml/widget_cofig"/>            
  4.             <intent-filter >                                    
  5.                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
  6.             </intent-filter>
  7.         </receiver>
And need a this. 

You change tag receiver instead of activity.

and metadata tag and intent-filter.