Android 안드로이드2014. 10. 25. 23:07

에디트텍스트에 텍스트를 붙여넣기

EditText지정한 위치에 글자를 붙여넣기 가능

텍스트에서 특정한 글자가 선택되어도 붙여넣기할 수 있다.


private void insertSomeText(String st){
if(IsDebugging) Log.d(TAG, "insertSomeText()");

EditText et = (EditText)findViewById(R.id.et_sourcetext);
/** http://stackoverflow.com/questions/3609174/android-insert-text-into-edittext-at-current-position */
int start = Math.max(et.getSelectionStart(), 0);
int end = Math.max(et.getSelectionEnd(), 0);
et.getText().replace(Math.min(start, end), Math.max(start, end), st);
}








Posted by 코드버무려
Android 안드로이드2014. 10. 20. 22:03


출처: 슈퍼드로이드


16. Activity에 대해서 - Thread 와 Android Main Thread

http://cafe.daum.net/superdroid/aAfL/98


--------------------------------------------------------------


package com.test.ThreadTest;


import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.view.View;

import android.widget.TextView;


public class A extends Activity {

static final int REQUEST_DRAW_COUNT = 1;

TextView   mTextView = null;

Handler mHandler = null;

int mCount = 0;

    /** 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.text_view);

        

        //mHandler = new Handler(); // runnable 방식

        

        mHandler = new Handler() // handleMessage 방식

        {

        public void handleMessage(Message msg) {

        switch (msg.what)

        {

        case REQUEST_DRAW_COUNT:

        mCount = msg.arg1;

        mTextView.setText("Count : " + mCount);

        break;

        }

        }

        };

    }

    

    public void onClick(View v)

    { 

    switch(v.getId())

    {

    case R.id.set_count_btn:

    new Thread()

    {

    public void run()

    {

    /*  // runnable방식

    Runnable runnable = new Runnable ()

    {

    public void run()

    {

    mTextView.setText("count : " + mCount);

    }

    };

    Message msg = Message.obtain(mHandler, runnable);

    mCount = 50; 

    mHandler.sendMessage(msg); */

   

    // handleMessage 방식

    Message msg = Message.obtain(mHandler, REQUEST_DRAW_COUNT);

    msg.arg1 = 50;

    mHandler.sendMessage(msg);

    }  

    }.start();

   

    /*    //오류

    new Thread()

    {

    @Override

        public void run() 

        {

    mTextView.setText("Count : " + 50);

        }

   

    }.start(); */

   

    break;

    }

    }

}




Posted by 코드버무려
Android 안드로이드2014. 10. 14. 14:40


연결타입 서비스

같은 프로세스 내부 Main Thread에 서비스를 만들고 Activity와 통신 



==================================================================

먼저

AndroidManifest.xml에 <service android:name=".GPService"  >라고 서비스 등록.


아래와 같이 인텐트필터를 등록해도 작동한다

            <intent-filter>

                <action android:name="com.example.GService"/>

            </intent-filter> 


==================================================================

Activity에 구현할 코드



선언

private GService mGService;

private boolean mGBound = false;   // 서비스와 연결된 상태를 점검



바인드를 끊을 때

       if(mGSBound){

      unbindService(mGConnection);

      mGBound = false;

      }



바인드를 연결할 때

    Intent intent = new Intent(MnGAActy.this, GService.class);

    bindService(intent, mGConnection, Context.BIND_AUTO_CREATE);

   


서버역할 중인 서비스에서 실행 결과 확인

if(mGSBound){

int intResult= mGService.getDoSomething();

String strResult= mGService.getDoSomething();


Do something what you want here!

}



서비스와 통신할 인터페이스 객체 생성

    private ServiceConnection mGConnection = new ServiceConnection(){


@Override

public void onServiceConnected(ComponentName name, IBinder service) {

        LocalBinder binder = (LocalBinder) service;

            mGService = binder.getService();

            mGBound = true;

}

    @Override

    public void onServiceDisconnected(ComponentName arg0){

    mGBound = false;

    }

    };

==================================================================



public class GService extends Service{

private static final String TAG = "GService";

private final IBinder mGBinder = new LocalBinder();

public class LocalBinder extends Binder{

GService getService(){

return GService.this;

}

}

public int getDoSomething(){


get result of some Processing


return result;

}


    @Override

    public void onCreate() 

    {

        super.onCreate();        

        Log.i(TAG, "onCreate()");

}

    

    @Override

    public int onStartCommand(Intent aIntent, int aFlags, int aStartId)

    {

    super.onStartCommand(aIntent, aFlags, aStartId);

    Log.i(TAG, "onStartCommand()");  

   

        return START_STICKY ;

    }

    

@Override

public void onDestroy()

{

Log.i(TAG, "onDestroy()");

}

    

@Override

public IBinder onBind(Intent intent) {

return mGBinder;

}

}




Posted by 코드버무려
Android 안드로이드2014. 10. 13. 13:24

실제 단말기에서는 잘 나올지 곧 테스트할 예정이다.

그전에 에러를 캡쳐해보았다.

아래에 보면 AdRequest를 하나 마지막 줄에서 Ad를 끝내버린다.


11-24 23:49:58.891: I/Ads(2786): Starting ad request.

11-24 23:49:58.919: I/Ads(2786): Use AdRequest.Builder.addTestDevice("B3EEABB8EE11C2BE770B684D95219ECB") to get test ads on this device.

11-24 23:49:59.163: I/Ads(2786): Please set theme of AdActivity to @android:style/Theme.Translucent to enable transparent background interstitial ad.

11-24 23:50:00.261: I/Choreographer(2786): Skipped 129 frames!  The application may be doing too much work on its main thread.

11-24 23:50:00.453: D/gralloc_goldfish(2786): Emulator without GPU emulation detected.

11-24 23:50:01.325: I/art(2786): Background sticky concurrent mark sweep GC freed 1639(131KB) AllocSpace objects, 0(0B) LOS objects, 6% free, 3MB/3MB, paused 2.245ms total 659.502ms

11-24 23:50:05.988: I/Choreographer(2786): Skipped 36 frames!  The application may be doing too much work on its main thread.

11-24 23:50:08.704: W/art(2786): Suspending all threads took: 16.056ms

11-24 23:50:08.788: I/art(2786): Background partial concurrent mark sweep GC freed 643(42KB) AllocSpace objects, 0(0B) LOS objects, 25% free, 3MB/4MB, paused 72.351ms total 1.275s

11-24 23:50:08.798: I/Choreographer(2786): Skipped 65 frames!  The application may be doing too much work on its main thread.

11-24 23:50:13.314: W/chromium(2786): [WARNING:proxy_service.cc(901)] PAC support disabled because there is no system implementation

11-24 23:50:13.358: W/chromium(2786): [WARNING:data_reduction_proxy_settings.cc(403)] SPDY proxy OFF at startup

11-24 23:50:13.534: W/art(2786): Attempt to remove local handle scope entry from IRT, ignoring

11-24 23:50:13.598: W/AwContents(2786): onDetachedFromWindow called when already detached. Ignoring

11-24 23:50:20.284: I/Ads(2786): Scheduling ad refresh 30000 milliseconds from now.

11-24 23:50:20.314: I/Ads(2786): Ad finished loading.



Posted by 코드버무려
Android 안드로이드2014. 9. 29. 16:11









앱 실행중 Resources 의 String.xml 내용을 바꾸는 코드 [올 펌]

아이템 하나 사용할 때

String.format(String, Object...)

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
Resources res = getResources();
String text = String.format(
    res.getString(R.string.welcome_messages),
    username, mailCount);


plurals인 경우

<plurals name="welcome_messages">
    <item quantity="one">Hello, %1$s! You have a new message.</item>
    <item quantity="other">Hello, %1$s! You have %2$d new messages.</item>
</plurals>
Resources res = getResources();
String text = res.getQuantityString(R.string.welcome_messages,
     mailCount, username, mailCount);



세번째 방법

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE resources [
  <!ENTITY appname "MrQuery">
  <!ENTITY author "Oded">
]>

<resources>
    <string name="app_name">&appname;</string>
    <string name="description">The &appname; app was created by &author;</string>
</resources>

http://stackoverflow.com/questions/3656371/dynamic-string-using-string-xml






Posted by 코드버무려