2012年3月17日 星期六

Widget -- Thread


定義自己的runtime

1. 修改 Manifest.xml
2. 建立主程式 SlideShow.java
3. 建立一個常駐程式的資訊檔 res/xml/slide_show.xml
4. 建立上述資訊檔的佈局檔案 res/layout/slide_show.xml


****************************************
Manifest.xml


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

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

    <application
        android:icon="@drawable/track"
        android:label="WidgeThread" >
     
        <receiver android:name="SlideShow" android:label="SlideShow">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/slide_show" />
</receiver>

    </application>

</manifest>

***********************************************

SlideShow.java


public class SlideShow extends AppWidgetProvider{


//index要定義成static
    static int index = 0;
    private static Thread thread;
    //照片圖檔的陣列安排
    private static int[] images = {
        R.drawable.pic0,
        R.drawable.pic1,
        R.drawable.pic2,
        R.drawable.pic3,
        R.drawable.pic4,
        R.drawable.pic5,
     
    };
    //onUpdate階段
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
//super.onUpdate(context, appWidgetManager, appWidgetIds);

Toast.makeText(context, "onUpdate", Toast.LENGTH_LONG).show();
   
startTimer(context, appWidgetManager);
}
 



public static void showPicture (Context context, AppWidgetManager appWidgetManager){

RemoteViews remoteViews = new RemoteViews (context.getPackageName(), R.layout.slide_show);
remoteViews.setImageViewResource(R.id.ImageView01, images[index]);
index = ++index % images.length;
ComponentName thisWidget = new ComponentName(context, SlideShow.class);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}

//Create customized Timer
private void startTimer(final Context context, final AppWidgetManager appWidgetManager) {
// TODO Auto-generated method stub
//Set Handler
final Handler handler = new Handler();
final Runnable callback = new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
   showPicture(context, appWidgetManager);
}
};

//Create a thread to run
thread = new Thread (){
@Override
public void run(){
try {
while (true){
Thread.sleep(10000);

handler.post(callback);
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
};
thread.start();
}

}

******************************************************
res/xml/slide_show.xml:


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

********************************************************
res/layout/slide_show.xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<ImageView
android:id="@+id/ImageView01"
android:layout_height="fill_parent"
android:layout_width="fill_parent" />
</LinearLayout>



沒有留言:

張貼留言