改写,原文地址:http://blog.csdn.net/android_tutor/article/details/5544471
Widget就是Android中的一个挂件。曾几何时,这个被认为是革命性的东西,现在慢慢没落。其原因有很多。流量、耗电、内存。。。
下面还是说下widget的使用吧:
首先看效果图:
1.首先在res文件夹下新建一个xml文件夹。注意是文件夹。然后在这个文件夹下新建一个xml文件:widget_provider.xml。resouce-type选择appwidget-provider,如图:
xml文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="50dp"
android:minHeight="50dp"
android:updatePeriodMillis="10000"
android:initialLayout="@layout/main"
/>
2.修改layout文件夹的main.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/git"
>
<TextView
android:id="@+id/wordcup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello"
android:textSize="12px"
android:textColor="#ff0000"
/>
</LinearLayout>
3.修改WidetDemo.java
package com.wf.mywidgetdemo;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Timer;
import java.util.TimerTask;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.widget.RemoteViews;
public class WidetDemo extends AppWidgetProvider{
/** Called when the activity is first created. */
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTime(context,appWidgetManager), 1, 60000);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
private class MyTime extends TimerTask{
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
public MyTime(Context context,AppWidgetManager appWidgetManager){
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(),R.layout.main);
thisWidget = new ComponentName(context,WidetDemo.class);
}
@Override
public void run() {
Date date = new Date();
Calendar calendar = new GregorianCalendar(2018,06,8);
long days = (((calendar.getTimeInMillis()-date.getTime())/1000))/86400;
remoteViews.setTextViewText(R.id.wordcup, "距离俄罗斯世界杯还有" + days+"天");
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
}
4.修改AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wf.mywidgetdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".WidetDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_provider"
/>
</receiver>
</application>
</manifest>
搞定。