码迷,mamicode.com
首页 > 移动开发 > 详细

android高手之路之Android Widget

时间:2015-01-29 17:36:05      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

改写,原文地址: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>
搞定。

android高手之路之Android Widget

标签:

原文地址:http://blog.csdn.net/howlaa/article/details/43273649

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!