設計一個在平板桌面上跑的小時鐘,用thread每秒更新一次時間 據上一次程式,這小工具是在桌面上來顯示因此,新增Project時不選Create Activity,此次轉換到MacBook Air機器上開發(因為模擬器比較正常一些)
1.新增Project名稱叫widget_clock(File->New->Project->Android Project->Next->Project Name->Next->Android 3.0->
不選Create Activity
Pockage Name :test.clock_widget
不選Create Activity
Pockage Name :test.clock_widget
2.新增appwidget-provider(File->New->Other->Android XML File->Next->Resource Type選 AppWidget Provider)(如下圖)
產生好xml filfe(clock_widge.xml)後,修改下面紅色字體部份(亮藍色背景)
說明:
說明:
android:updatePeriodMillis="0"代表只讓appwidget更新一次,我要自行用thread來更新
android:initialLayout="@layout/main" 是指initialLayout指定你layout佈局的xml(main)
clock_widge.xml File:
clock_widge.xml File:
<xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="150dp"
android:updatePeriodMillis="0"
android:initialLayout="@layout/main"
>
</appwidget-provider>
3.修改main.xml的內容加入下面紅色字體部份(亮藍色背景)
說明:
說明:
android:id="@+id/now設定view id
main.xml File:
main.xml File:
<xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/now"
android:gravity="center"
android:textColor="@android:color/white"
android:textSize="32sp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello"
/>
LinearLayout>
4.修改AndroidManifest.xml加入下面紅色字體部份(亮藍色背景)
說明:
<receiver android:name=".Clock_widget" >
<service android:enabled="true" android:name=".Clock_widget" />
說明:
<receiver android:name=".Clock_widget" >
<service android:enabled="true" android:name=".Clock_widget" />
同時指定receiver,service為.Clock_widget(java class,這是要新增class更新appwidget的)
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
指定動作方式APPWIDGET_UPDATE
android:resource="@xml/clock_widge" />
指定resource="@xml/clock_widge" (步驟1新增的xml)
AndroidManifest.xml File:
<xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.widget_clock"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<receiver android:name=".Clock_widget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/clock_widge" />
</receiver>
<service android:enabled="true" android:name=".Clock_widget" />
application>
manifest>
5.新增class(Clock_widget)File->New->Class
說明:
說明:
Clock_widget.java內容如下
此class繼承了AppWidgetProvider並覆寫更新onUpdate程序,Handler是要工作的內容,用thread來時時送出handler訊息
updateViews.setTextViewText(R.id.now, str );
updateViews.setTextColor(R.id.now, -1);
這兩行now對應到main.xml所設的id
updateViews.setTextViewText(R.id.now, str );
updateViews.setTextColor(R.id.now, -1);
這兩行now對應到main.xml所設的id
Clock_widget.java File:
package test.widget_clock;
import java.util.Calendar;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.RemoteViews;
public class Clock_widget extends AppWidgetProvider{
public Handler c_Handler;
public Context cc;
public int[] aapid;
public AppWidgetManager appw;
private Thread c_Thread;
public Calendar c_Calendar; //日曆
private int c_Minutes; //分
private int c_Hour; //時
private int c_Second; //秒
private String str;
@Override
public void onUpdate(Context context,AppWidgetManager appWidgetManager, int[] appWidgetIds) {
cc=context;
aapid=appWidgetIds;
appw=appWidgetManager;
c_Handler =new Handler(){
public void handleMessage(Message msg){
long time=System.currentTimeMillis();
final Calendar c_Calendar = Calendar.getInstance();
c_Calendar.setTimeInMillis(time);
c_Hour=c_Calendar.get(Calendar.HOUR);
c_Minutes=c_Calendar.get(Calendar.MINUTE);
c_Second=c_Calendar.get(Calendar.SECOND);
str="現在時間 "+c_Hour+":"+c_Minutes+":"+c_Second;
RemoteViews updateViews = new RemoteViews( cc.getPackageName(), R.layout.main);
updateViews.setTextViewText(R.id.now, str );
updateViews.setTextColor(R.id.now, -1);
appw.updateAppWidget(aapid, updateViews);
}
};
//產執行續物件
c_Thread = new clock_Thread();
//啟動執行續
c_Thread.start();
}//onupdate
//thread class
class clock_Thread extends Thread{
public void run(){
super.run();
try{
do{
Thread.sleep(1000);
Message m=new Message();
// 將Message物件送入MessageQueue裡
c_Handler.sendMessage(m);
}while(true);
}catch (Exception e) {
// handle exception
}//cttch
}//run
}//thread
}//Clock_widget class
5.模擬器上跑的結果
沒有留言:
張貼留言