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
- public class MyWidget extends AppWidgetProvider {
- @Override
- super.onUpdate(context, appWidgetManager, appWidgetIds);
- int appId;
- for(int i=0;i<appWidgetIds.length;i++){
- appId=appWidgetIds[i];
- Intent intent=new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://m.daum.net"));
- PendingIntent pe=PendingIntent.getActivity(context, 0, intent, 0);
- RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.activity_main);
- views.setOnClickPendingIntent(R.id.eventBtn, pe);
- appWidgetManager.updateAppWidget(appId, views);
- }
- }
- }
activity_main.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity" >
- <Button
- android:id="@+id/eventBtn"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="GOGOGO" />
- </LinearLayout>
This is a widget layout
If you choose Graphical Layout,you can be seen this display.
res/xml/widget_config.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="1000"
- android:initialLayout="@layout/activity_main"
- />
This is a widget config file.
You can configuration update time, widget size. etc...
AndroidManifest.xml
- <receiver android:name="MyWidget">
- <meta-data android:name="android.appwidget.provider"
- android:resource="@xml/widget_cofig"/>
- <intent-filter >
- <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
- </intent-filter>
- </receiver>
And need a this.
You change tag receiver instead of activity.
and metadata tag and intent-filter.
No comments:
Post a Comment