layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
android:textColor="#000000" />
<Button
android:id="@+id/wid_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:text="Click"
android:textColor="#000000" />
</RelativeLayout>
res/xml/hello_widget_provider.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/activity_main"
android:minHeight="100dp"
android:minWidth="300dp"
android:updatePeriodMillis="1000"
>
</appwidget-provider>
MainActivity.java
public class MainActivity extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.activity_main);
remoteViews.setOnClickPendingIntent(R.id.wid_button,
buildButtonPendingIntent(context));
pushWidgetUpdate(context, remoteViews);
}
public static PendingIntent buildButtonPendingIntent(Context context) {
Intent intent = new Intent();
intent.setAction("CHANGE_TEXT");
return PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
ComponentName myWidget = new ComponentName(context, MainActivity.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myWidget, remoteViews);
}
}
AndroidManifest.xml
In <application> add this
<receiver android:name="MainActivity" >
<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>
<receiver android:name="MainActivityProvider" >
<intent-filter>
<action android:name="CHANGE_TEXT" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/hello_widget_provider" />
</receiver>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
android:textColor="#000000" />
<Button
android:id="@+id/wid_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:text="Click"
android:textColor="#000000" />
</RelativeLayout>
res/xml/hello_widget_provider.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/activity_main"
android:minHeight="100dp"
android:minWidth="300dp"
android:updatePeriodMillis="1000"
>
</appwidget-provider>
MainActivity.java
public class MainActivity extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.activity_main);
remoteViews.setOnClickPendingIntent(R.id.wid_button,
buildButtonPendingIntent(context));
pushWidgetUpdate(context, remoteViews);
}
public static PendingIntent buildButtonPendingIntent(Context context) {
Intent intent = new Intent();
intent.setAction("CHANGE_TEXT");
return PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
ComponentName myWidget = new ComponentName(context, MainActivity.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myWidget, remoteViews);
}
}
MainActivityProvider.java
public class MainActivityProvider extends BroadcastReceiver {
private static int clickCount = 0;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("CHANGE_TEXT")) {
updateWidgetText(context);
}
}
private void updateWidgetText(Context context) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.activity_main);
remoteViews.setTextViewText(R.id.textView1, getImageToSet());
// remoteViews.setOnClickPendingIntent(R.id.wid_button,
// MainActivity.buildButtonPendingIntent(context));
MainActivity.pushWidgetUpdate(context.getApplicationContext(),
remoteViews);
}
private CharSequence getImageToSet() {
clickCount++;
return clickCount % 2 == 0 ? "Hai" : "Bye";
}
}
AndroidManifest.xml
In <application> add this
<receiver android:name="MainActivity" >
<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>
<receiver android:name="MainActivityProvider" >
<intent-filter>
<action android:name="CHANGE_TEXT" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/hello_widget_provider" />
</receiver>
No comments:
Post a Comment