Aug 29, 2012

서비스 바인드와 핸들러 구현 (옵저버 패턴)

//// mActivity.java  
Messenger mService = null;  
boolean bindService = false;

class IncomingHandler extends Handler {
    @Override  
    public void handleMessage(Message msg) {

    }  
}  

final Messenger mMessenger = new Messenger(new IncomingHandler());

private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {

        mService = new Messenger(service);  

        try {  
            Message msg = Message.obtain(null, MessengerService.MSG_REGISTER_CLIENT);
            msg.replyTo = mMessenger;  
            mService.send(msg);  

        } catch (RemoteException e) {  
          
        }  
    }  

    public void onServiceDisconnected(ComponentName className) {

        mService = null;  
    }  
};  

void doBindService() {  
    // Establish a connection with the service. We use an explicit
    // class name because there is no reason to be able to let other
    // applications replace our component.  
    bindService = getApplicationContext().bindService(new Intent(mActivity.this, MessengerService.class), mConnection, Context.BIND_AUTO_CREATE);  

}  

void doUnbindService() {  
    if (bindService) {  
        // If we have received the service, and hence registered with it, then now is the time to unregister. 
        if (mService != null) {
            try {  
                Message msg = Message.obtain(null, MessengerService.MSG_UNREGISTER_CLIENT);
                msg.replyTo = mMessenger;  
                mService.send(msg);  
            } catch (RemoteException e) {  
                // There is nothing special we need to do if the service has crashed. 
            }  
        }  

        // Detach our existing connection.  
        getApplicationContext().unbindService(mConnection);  
        bindService = false;  
    }  
}  


////MessengerService.java  

     
    /** 
     * Command to the service to register a client, receiving callbacks 
     * from the service.  The Message's replyTo field must be a Messenger of 
     * the client where callbacks should be sent. 
     */ 
    public final static int MSG_REGISTER_CLIENT = 1;
    /** 
     * Command to the service to unregister a client, ot stop receiving callbacks 
     * from the service.  The Message's replyTo field must be a Messenger of 
     * the client as previously given with MSG_REGISTER_CLIENT. 
     */ 
    public final static int MSG_UNREGISTER_CLIENT = 2;
    /** 
     * Command to service to set a new value.  This can be sent to the 
     * service to supply a new value, and will be sent by the service to 
     * any registered clients with the new value. 
     */ 
    static final int MSG_SET_VALUE = 3;

    ArrayList<Messenger> mClients = new ArrayList<Messenger>(); 

    private final Messenger mMessenger = new Messenger(new IncomingHandler());

    private class IncomingHandler extends Handler {
        @Override 
        public void handleMessage(Message msg) {
            switch (msg.what) { 
            case MSG_REGISTER_CLIENT: 
                mClients.add(msg.replyTo); 
                break; 
            case MSG_UNREGISTER_CLIENT: 
                mClients.remove(msg.replyTo); 
                break; 
            } 
        } 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
        return mMessenger.getBinder(); 
    } 

    /** 
     * Send Message to Clients. 
     * @param msg Message 
     */ 
    private void sendMessageToClients(Message msg) {
        for (int i = mClients.size() - 1; i >= 0; i--) { 
            try { 
                // send a message to mClients. 
                // you may have handle this massage from IncomingHandler at any Activity
                mClients.get(i).send(msg); 
            } catch (RemoteException e) { 
                // The client is dead. Remove it from the list; 
                // we are going through the list from back to front
                // so this is safe to do inside the loop. 
                mClients.remove(i); 
            } 
        } 
    }

Aug 16, 2012

Home Launcher 등록



<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
 </intent-filter>

Aug 14, 2012

어두워짐 방지 (screen full wake up)

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "screen"); 
wl.acquire(); 
//     ..screen will stay on during this section.. 
//wl.release();



<uses-permission android:name="android.permission.WAKE_LOCK"/>

Aug 7, 2012

Create Thread


1.
    class mThread extends Thread {
        @Override 
        public void run() {
             
             
        } 
    } 
     
     
    mThread t = new mThread(); 
    t.start();

2.

Thread thread = new Thread(){
 @Override
 public void run() {
  // TODO Auto-generated method stub
 }
};

Timer

    // 타미어 테스크 정의     
    TimerTask mTask = new TimerTask() { 
        @Override 
        public void run() {
            // do something!! 
        } 
    }; 
     
     
    // 사용 
    Timer mTimer = new Timer(); 
    mTimer.schedule(mTask, WHEN, PERIOD);