2011年10月29日 星期六

Android appwidget service筆記













一開始service未啟動















按按鈕啟動service














再按按鈕顯示時間











android appwidget透過一個按鈕來啟動背景servicey,在透過另一按鈕更新時間(appwidget的建立可參考上一篇)











1. 
reciver註冊action (給按鈕1)Clock_widget
  <action android:name="test.widget_clock.CLICK1" />
service註冊action(給按鈕2)Myservice

  <action android:name="test.widget_clock.CLICK1" />

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="test.Clock_widget1"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="11" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
            <receiver android:name=".Clock_widget" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
              
                <action android:name="test.widget_clock.CLICK2" />
           
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                android:resource="@xml/clockprovider" />
           </receiver>
           <service android:enabled="true" android:name=".Clock_widget$MyService">
      
           <intent-filter>
                <action android:name="test.widget_clock.CLICK1" />
            </intent-filter>
        </service>

    </application>
</manifest>

2.
//綁定service,注意androidmanifest receiver需註冊action
               Intent intent1 = new Intent(context, MyService.class);
              //啟動服務 
              context.startService(intent1);


 action動作的溝通需建立Intent


Clock_widget.java



package test.Clock_widget1;

import java.util.Calendar;
import java.util.Date;

import android.app.PendingIntent;
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.Handler;
import android.os.IBinder;
import android.widget.RemoteViews;
import android.widget.Toast;




public class Clock_widget extends AppWidgetProvider {
  
  
  
    //變數宣告
    public Handler c_Handler;
    public Handler r_Handler;
 
  
  
    public Calendar c_Calendar; //日曆
  
    public static String CLICK_ACTION1 ;
    public static String CLICK_ACTION2 ;
  

 
    public  Clock_widget() {
        //按鈕1字串
        CLICK_ACTION1 = "test.widget_clock.CLICK1";
        //按鈕2字串
        CLICK_ACTION2 = "test.widget_clock.CLICK2";
        c_Handler=new Handler();
      
      
     
      
      
    }
    @Override
     public void onReceive(Context context, Intent intent) {
          super.onReceive(context, intent);
          final String action = intent.getAction();
          if(action.equals(CLICK_ACTION2)){
              //if(c_Thread.isInterrupted())c_Thread.resume();
          
              //綁定service
               Intent intent1 = new Intent(context, MyService.class);
              //啟動服務  
              context.startService(intent1);
              
            
              Toast.makeText(context, "開時service", Toast.LENGTH_LONG).show();
          }
        
     }

        @Override
      
        public void onUpdate(Context context,AppWidgetManager appWidgetManager, int[] appWidgetIds) {
      
      
        
            //綁定Clock_widget注意androidmanifest receiver需註冊action
            final Intent refreshIntent1 = new Intent(context, Clock_widget.class);
            //set action字串
            refreshIntent1.setAction(Clock_widget.CLICK_ACTION2);
            final PendingIntent refreshPendingIntent1 = PendingIntent.getBroadcast(context, 0,refreshIntent1, PendingIntent.FLAG_UPDATE_CURRENT);
            //final PendingIntent refreshPendingIntent = PendingIntent.getService(cc, 0,refreshIntent, 0);
            RemoteViews updateViews1 = new RemoteViews( context.getPackageName(), R.layout.main);
            //設定button
            updateViews1.setOnClickPendingIntent(R.id.button2, refreshPendingIntent1);
            //update view
            appWidgetManager.updateAppWidget(appWidgetIds, updateViews1);
        
        
      
    
        }//onupdate
  
     
      
        public static class MyService extends Service {
          

          
          
            @Override
            public void onStart(Intent intent, int startId) {
                ComponentName thisWidget = new ComponentName(this, Clock_widget.class);
                AppWidgetManager manager = AppWidgetManager.getInstance(this);
                RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.main);
              
                //intent.getAction()取得action字串
                if (CLICK_ACTION1.equals(intent.getAction())) {
                  
                    Toast.makeText(this, "開時時間", Toast.LENGTH_LONG).show();
                    remoteViews.setTextViewText(R.id.textView1, new Date().toLocaleString());
                }else{
                    remoteViews.setTextViewText(R.id.textView1, "時間顯示器");  
                }
                //綁定Clock_widget注意androidmanifest receiver需註冊action
                Intent clickIntent = new Intent();
                clickIntent.setAction(CLICK_ACTION1);
                PendingIntent pendingIntent = PendingIntent.getService(this, 0, clickIntent, 0);
               
                remoteViews.setOnClickPendingIntent(R.id.button1, pendingIntent);
                manager.updateAppWidget(thisWidget, remoteViews);
              
            }
            @Override
            public IBinder onBind(Intent intent) {
                return null;
            }
        }

}









read more...

2011年10月21日 星期五

Android筆記 - 設計appwidget


























設計一個在平板桌面上跑的小時鐘,用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

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:

<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:

<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,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

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.模擬器上跑的結果




read more...

2011年10月15日 星期六

Android筆記

UBUNTU1004下使用ECLIPSE,撰寫Android,Java安裝使用Synaptic安裝OpenJdk,其他設定google找一下

1.Ctrl+Shift+o自動import
2.連點res->layout->gt;main.xml啟動Layout(UI設計)
3.事件處理:Layout中選取元件->Properties->On click填入處理method name(如下圖右側)














4.Java程式主體
連點SRC下的Activity(下圖左側),利用thread,來改變textview的text,顯示當前時間,按下按鈕1
送出c_Handler.sendMessage(m),啟動時間,按下另一按鈕則不送出c_Handler.sendMessage(m),時間停止












package t.u;

import java.util.Calendar;
public class Clock2Activity extends Activity {
private TextView mTextView;
public Calendar c_Calendar; //日曆
public int c_Minutes; //分
public int c_Hour; //時
public int c_Second; //秒
private boolean run_clock;
public Handler c_Handler;
private Thread c_Thread;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mTextView=(TextView)findViewById(R.id.textView1);
mTextView.setTextSize(30);

//hander指派工作
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);
mTextView.setText(c_Hour+":"+c_Minutes+":"+c_Second);
}
};

run_clock=false;
//產執行續物件
c_Thread = new clock_Thread();
//啟動執行續
c_Thread.start();
}
//button事件,啟動時間
public void clock(View cv) {
run_clock=true;
}//start_clock
//button事件,停止時間
public void clock_stop(View cv) {
run_clock=false;
}//stop_clock
//執行續
class clock_Thread extends Thread{
public void run(){
super.run();
try{
do{
Thread.sleep(1000);

Message m=new Message();
// 將Message物件送入MessageQueue裡
if(run_clock){
c_Handler.sendMessage(m);
}
}while(true);

}catch (Exception e) {
// TODO: handle exception
}//cttch
}//run
}//thread
}


5.main.xml

使用ECLIPSE來設計,這部份都可自動加上,上面3.事件處理有填入clock,下面xml程式會自動加入,就不用自己加

















6平板模擬器上的結果





















7.實際裝到eeepad

在project名字目錄下bin目錄,ECLIPSE已經打包好apk,只要拿到平板安裝,實際執行如下圖


read more...

2010年5月9日 星期日

TCL改變TEXT的括號


有時需要改變括號,因此就寫了一個能改變的程式
在判斷時不是很好寫,dbGet取得時,會多加\,{,},要想辦法先清掉




read more...

2010年5月8日 星期六

Tk gui
















tcl的GUI可用Tk來撰寫,下面就是一個簡單的button






#!/usr/bin/wish
button .a -text "早上" -command { puts "跑步" }
button .b -text "中午" -command { puts "休息" }
button .c -text "晚上" -command { puts "睡覺" }
button .d -text "離開" -command { exit }
pack .a .b .c .d -side top -fill x









read more...

自動打上POLY CONTACT


這之前有寫過,不過改寫了,功能變強很多
1.通常transistor MCELL的POLY是3塊,要取的合起來的BBOX
2.判斷R0,R90,R180,R170
然後CONT是要加在BOTTOM或TOP,並自動補齊POLY LAER



read more...

2010年5月2日 星期日

續上篇


修改可以根據LAYER或BLOCK來對齊,若有TEXT也會跟著移動

當滑鼠選取區域,取得該區域裡的LAYER,把這些LAYER弄在選單內,以便選取,來對齊用

原理很簡單:
取得LAYER或BLOCK的BBOX,然後判定R0,R90,R180,R270,算出x和y的pitch




read more...