Android平台下简单Widget的搭建过程

推荐安卓开发神器(里面有各种UI特效和android代码库实例)

Android平台下Widget的搭建过程为:

转自:http://www.cnblogs.com/alwaysyouare/archive/2010/01/06/1640219.html

1.在res/layout下创建Widget的布局文件:digitalclock.xml

<?xml version="1.0" encoding="UTF-8"?>
<TextView  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/time"
    android:textSize="45px"
    android:scrollX="30px"
    android:scrollY="30px"
    android:textStyle="bold"
    android:textColor="#ff000000"
    android:background="@drawable/bg"
    />


2.在res/xml下创建Widget的描述文件:est_appwidget.xml

<?xml version="1.0" encoding="UTF-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="186dip"
    android:minHeight="212dip"
    android:updatePeriodMillis="1000"
    android:configure="com.activity.TestActivity" // 第一次启动时,配置界面
    android:initialLayout="@layout/digitalclock"/>


3. 从AppWidgetProvider继承一个类(ESTTime),重写其虚方法

package com.android.test.esttime;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.text.format.Time;
import android.widget.RemoteViews;


public class ESTTime extends AppWidgetProvider {
    
    /** Called when the activity is first created. */
    @Override
    public void onUpdate(Context context,
            AppWidgetManager appWidgetManager,int[] appWidgetIds )
    {
        context.startService(new Intent(context,UpdateService.class));        
    }
    
  //Service类
    public static class UpdateService extends Service {
        @Override
        public void onStart(Intent intent,int startId){
            Time estTime = new Time("EST");
            estTime.setToNow();
            RemoteViews updateViews = 
                new RemoteViews(getPackageName(),
                        R.layout.digitalclock);
            updateViews.setTextViewText(R.id.time, estTime.format("%H:%M"));
            
            ComponentName thisWidget = new ComponentName(this,ESTTime.class);
            
            AppWidgetManager manager = AppWidgetManager.getInstance(this);
            manager.updateAppWidget(thisWidget, updateViews);
        }

        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }

    }
    

}


4.在AndroidManifest.xml中注册本Widget

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.android.test.esttime"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <receiver android:name=".ESTTime"
                  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/est_appwidget"/>            
        </receiver>
        <service android:name=".ESTTime$UpdateService"/>
    </application>
    <uses-sdk android:minSdkVersion="3" />
</manifest> 



5.TestActivity.xml

int mAppWidgetId;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.oawidget);
		mBtn = (Button)findViewById(R.id.testbtn);
		mBtn.setOnClickListener(l);
		
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();

		if (extras != null) {
		    mAppWidgetId = extras.getInt(
		            AppWidgetManager.EXTRA_APPWIDGET_ID, 
		            AppWidgetManager.INVALID_APPWIDGET_ID);

		}
	}
	
	View.OnClickListener l = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			Intent resultValue = new Intent();
			resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
			setResult(RESULT_OK, resultValue);
			finish();
		}
	};


最后,编译运行。这样在Home上长按弹出的Widget列表中就有新创建的Weiget了 。

 

你可能感兴趣的:(html,xml,android,OS)