Create TemperatureWidget class and register with Temperature sensor intend
Download Source
Create widget_layout.xml under res/layout folder
package com.javaorigin.widget; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.widget.RemoteViews; package com.javaorigin.widget; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.widget.RemoteViews; public class TemperatureWidget extends AppWidgetProvider { double temp=0; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { context.getApplicationContext().registerReceiver(this,new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); updateUI(context) ; } @Override public void onReceive(Context context, Intent intent) { temp=intent.getIntExtra("temperature", 0)/10.0D; updateUI(context) ; super.onReceive(context, intent); } private void updateUI(Context context) { RemoteViews thisViews = new RemoteViews(context.getApplicationContext() .getPackageName(), R.layout.widget_layout); thisViews.setTextViewText(R.id.update, temp + ""); ComponentName thisWidget = new ComponentName(context,TemperatureWidget.class); AppWidgetManager.getInstance(context).updateAppWidget(thisWidget,thisViews); } }
Create widget_layout.xml under 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>Create myshape.xml under res/drawable folder
<?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>Create widget_info.xml under res/xml folder ,We can create this xml file using " File-->New --> Android xml file " menu with resource type is AppWidget Provider option ,then set attributeandroid:widgetCategory="keyguard|home_screen"
<?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="20dp" android:minWidth="20dp" android:updatePeriodMillis="30000" android:widgetCategory="keyguard|home_screen"> </appwidget-provider>Configure android manifest xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.javaorigin.widget" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <receiver android:name="TemperatureWidget" > <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> </application> </manifest>
Top 10 Marketplace (websites) where you can buy android app source code.
ReplyDelete