I have already done one android application which stores date with person name and phone number.
Now I have to develop one widget for this application to show today's (date, person name, and phone number) with button.
Requirements:
I have done a simple widget demo but I don't know how to synchronize it with my application.
Please share your experience and give some idea about my widget and application.
App widgets are miniature application views that can be embedded in other applications (such as the home screen) and receive periodic updates. These views are referred to as widgets in the user interface, and you can publish one with an app widget provider (or widget provider).
While most people associate apps with mobile OS like Android and iOS, they are developed for desktop operating systems like Windows and web browsers like Chrome and Firefox. That's why they are called desktop apps and web apps, respectively. Widgets are also programs, but they are designed as an extension of an app.
Create and configure widget
To register a widget you create a BroadcastReceiver with an intent filter for the android.appwidget.action.APPWIDGET_UPDATE action.
<receiver
android:icon="@drawable/icon"
android:label="Example Widget"
android:name="MyWidgetProvider" >
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
You also specify the meta-data for the widget via the android:name="android.appwidget.provider attribute. The configuration file referred by this meta-data contains the configuration settings for the widget. If contains for example the update interface, the size and the initial layout of the widget.
Create a new file myshape.xml in the /res/drawable directory. This file will define the background we use in your widget.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="2dp"
android:color="#FFFFFFFF" />
<gradient
android:angle="225"
android:endColor="#DD2ECCFA"
android:startColor="#DD000000" />
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
Define the following widget_layout.xml
file under the res/layout folder.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dip"
android:background="@drawable/myshape" >
<TextView
android:id="@+id/update"
style="@android:style/TextAppearance.Medium"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center_horizontal|center_vertical"
android:layout_margin="4dip"
android:text="Static Text" >
</TextView>
</LinearLayout>
now make the class.
public class MyWidgetProvider extends AppWidgetProvider {
private static final String ACTION_CLICK = "ACTION_CLICK";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// Get all ids
ComponentName thisWidget = new ComponentName(context,
MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
// Create some random data
int number = (new Random().nextInt(100));
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
Log.w("WidgetExample", String.valueOf(number));
// Set the text
remoteViews.setTextViewText(R.id.update, String.valueOf(number));
// Register an onClickListener
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
I am finding couple of days I got somehow the solution. So I am sharing my idea.
This site has been very useful for me.
http://kasperholtze.com/android/how-to-make-a-simple-android-widget/
I made some changes and did my work.
Requirement solution:
1. Make Receiver and put into manifest permission like this.
<receiver
android:name=".WatchWidget"
android:label="WatchWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/watch_widget_provider" />
</receiver>
2. In my side require top TextView click so can make using TextView id in onUpdate()
.
remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_lay);
Intent intent = new Intent(context, TestActivity.class);
intent.setAction("callActivity");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.widget_textview, pendingIntent);
appWidgetManager.updateAppWidget(new ComponentName(context,WatchWidget.class), remoteViews);
3. I want to call database and fetch record and display it. Now question what about update?
So I have used timer class and run every 1000sec because of over widget is always update 30min(may be).
@Override
public void onUpdate( final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds )
{
this.appWidgetManager = appWidgetManager;
try {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager,appWidgetIds), 1, 1000);
} catch (Exception e) {
System.out.println("Exception:");
}
updateSwitch1(context, appWidgetManager, appWidgetIds[0]);
}
If any have queries then put a comment and thanks to all who gave me full support to make this answer useful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With