Android Widgets
App Widgets is a small view of an application.We can embedded this widgets in other application.We can update this widgets periodically.App widget is a simple way to enhance your application by having a view on android Home Screen. Widgets is used to convey the update information of our application to the user.
RemoteViews is used to describes a view hierarchy that can be displayed in another process. The hierarchy is inflated from a layout resource file, and this class provides some basic operations for modifying the content of the inflated hierarchy.
A RemoteViews object can support the following layout classes:
FrameLayout
LinearLayout
RelativeLayout
GridLayout
And the following widget classes:
AnalogClock
Button
Chronometer
ImageButton
ImageView
ProgressBar
TextView
ViewFlipper
ListView
GridView
StackView
AdapterViewFlipper
You can download source code here.
RemoteViews
A RemoteViews object can support the following layout classes:
FrameLayout
LinearLayout
RelativeLayout
GridLayout
And the following widget classes:
AnalogClock
Button
Chronometer
ImageButton
ImageView
ProgressBar
TextView
ViewFlipper
ListView
GridView
StackView
AdapterViewFlipper
package com.etr.widgetsexample;
import java.util.Date;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class DemoWidgetProvider extends AppWidgetProvider {
@Override
public
void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[]
appWidgetIds) {
RemoteViews
remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
remoteViews.setTextViewText(R.id.title,
getTitle());
remoteViews.setTextViewText(R.id.desc,
getDesc());
pushWidgetUpdate(context,
remoteViews);
context.startService(new
Intent(context, WidgetService.class));
}
private
static CharSequence getDesc() {
return
new Date().toString()
+
" ::: Update \n\n http://androidtutorialspoint.blogspot.com ";
}
private
static CharSequence getTitle() {
return
"Every minute update";
}
public
static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
ComponentName
myWidget = new ComponentName(context,
DemoWidgetProvider.class);
AppWidgetManager
manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myWidget,
remoteViews);
}
}
AppWidgetProvider
onUpdate() is called to update the App Widget at intervals defined by the updatePeriodMillis attribute in the AppWidgetProviderInfo.
onAppWidgetOptionsChanged() is called when the widget is first placed and any time the widget is resized.
onDeleted(Context, int[]) is called every time an App Widget is deleted from the App Widget host.
onEnabled(Context) is called when an instance the App Widget is created for the first time.
onDisabled(Context) is called when the last instance of your App Widget is deleted from the App Widget host.
onReceive(Context, Intent) is called for every broadcast and before each of the above callback methods.
package com.etr.widgetsexample;
import java.util.Date;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class DemoWidgetIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(DemoWidgetUtils.WIDGET_UPDATE_ACTION)) {
updateWidgetPictureAndButtonListener(context);
}
}
private void updateWidgetPictureAndButtonListener(Context context) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
remoteViews.setTextViewText(R.id.title, getTitle());
remoteViews.setTextViewText(R.id.desc, getDesc(context));
DemoWidgetProvider.pushWidgetUpdate(context.getApplicationContext(),
remoteViews);
}
private String getDesc(Context context) {
return new Date().toString()
+ " ::: Update \n\n http://androidtutorialspoint.blogspot.com ";
}
private String getTitle() {
return "Every minute update";
}
}
AppWidgetManager
AppWidgetManager is used to Updates AppWidget state.
This is code for WidgetService.Every minutes it will update app widget content.
This is code for appwidget-provider,It is placed inside of res/xml folder.
This is code widget_layout.xml
Output video
This is code for WidgetService.Every minutes it will update app widget content.
package com.etr.widgetsexample;
import java.util.Timer;
import java.util.TimerTask;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class WidgetService extends Service {
Timer updateWidgetsService;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
updateWidgetsService = new Timer();
updateWidgetsService.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Intent intent = new Intent();
intent.setAction(DemoWidgetUtils.WIDGET_UPDATE_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
try {
pendingIntent.send();
} catch (CanceledException e) {
e.printStackTrace();
}
}
}, 1000, 60000);
return START_STICKY;
}
@Override
public void onDestroy() {
}
}
import java.util.Timer;
import java.util.TimerTask;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class WidgetService extends Service {
Timer updateWidgetsService;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
updateWidgetsService = new Timer();
updateWidgetsService.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Intent intent = new Intent();
intent.setAction(DemoWidgetUtils.WIDGET_UPDATE_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
try {
pendingIntent.send();
} catch (CanceledException e) {
e.printStackTrace();
}
}
}, 1000, 60000);
return START_STICKY;
}
@Override
public void onDestroy() {
}
}
This is code for appwidget-provider,It is placed inside of res/xml folder.
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_layout"
android:minHeight="146dp"
android:minWidth="292dp"
android:previewImage="@drawable/widget"
android:updatePeriodMillis="10000" >
</appwidget-provider>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_layout"
android:minHeight="146dp"
android:minWidth="292dp"
android:previewImage="@drawable/widget"
android:updatePeriodMillis="10000" >
</appwidget-provider>
This is code widget_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contentContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@drawable/appwidget_dark_bg"
android:orientation="vertical"
android:padding="8dp" >
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:maxLines="2"
android:paddingBottom="5dp"
android:textColor="#fcfcfc"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/desc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:gravity="center"
android:maxLines="5"
android:textColor="#fcfcfc"
android:textSize="13sp"
android:textStyle="normal" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contentContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@drawable/appwidget_dark_bg"
android:orientation="vertical"
android:padding="8dp" >
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:maxLines="2"
android:paddingBottom="5dp"
android:textColor="#fcfcfc"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/desc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:gravity="center"
android:maxLines="5"
android:textColor="#fcfcfc"
android:textSize="13sp"
android:textStyle="normal" />
</LinearLayout>
Output video
Comments
Post a Comment