2012年3月17日 星期六

Widget --Timer Task


Manifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cq.android.app.widget.timertask"
    packageQ="com.example.android.appwidget"
    android:versionCode="1"
    android:versionName="1.0" >

    <application
        android:icon="@drawable/trackerfile"
        android:label="Widget_TimerTask" >
        <receiver
            android:label="WhatTimeIsItNow"
            android:name="WhatTimeIsItNow" >
            <intent-filter >
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/what_time_is_it_now" />
        </receiver>

        <service android:name="WhatTimeIsItNow$MyService" />
    </application>

    <uses-sdk android:minSdkVersion="10" />

</manifest>

*******************************************
res/xml/what_time_is_it_now.xml:


<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="72dip"
android:updatePeriodMillis="0"
android:initialLayout="@layout/what_time_is_it_now"
/>

**********************************************
res/layout/what_time_is_it_now.xml:


<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Starting..."
android:textColor="#FF0000"
android:textSize="10pt" />

***********************************************
主程式:


public class WhatTimeIsItNow extends AppWidgetProvider{

private static Thread thread;
private Timer timer = new Timer();
private int[] appWidgetIds;
private AppWidgetManager appWidgetManager;
private Context context;

     @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
//super.onUpdate(context, appWidgetManager, appWidgetIds);
this.appWidgetManager = appWidgetManager;
this.appWidgetIds = appWidgetIds;
this.context = context;
timer = new Timer();

//Start timer schedule
timer.schedule(timerTask02, 0, 1000);
}

//Set Handler
private Handler handler = new Handler(){
public void handleMessage(Message msg){
Intent intent = new Intent (context, MyService.class);
context.startService(intent);
}
};


private TimerTask timerTask02 = new TimerTask(){

@Override
public void run() {
// TODO Auto-generated method stub
Message message = new Message();
handler.sendMessage(message);
}
};


//MyService服務程式
public static class MyService extends Service{
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
//super.onStart(intent, startId);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.what_time_is_it_now);
remoteViews.setTextViewText(R.id.TextView01, new Date().toLocaleString());
ComponentName thisWidget = new ComponentName(this, WhatTimeIsItNow.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, remoteViews);
}

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

}
}





沒有留言:

張貼留言