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.








No comments:

Post a Comment