Oct 29, 2013

[Tomcat] How to change root folder of tomcat

Open server.xml and edit as follows.


<Host name="localhost" appBase="c:\works\root"
  unpackWARs="true" autoDeploy="true"
  xmlValidation="false" xmlNamespaceAware="false">

<Context path="" docBase="." reloadable="true"/>
</Host>

Oct 22, 2013

[android] File Input and Output

If you want to write something:

File myFile = mCon.getDir("myFile", Activity.MODE_WORLD_WRITEABLE); 
String path = myFile.getAbsolutePath(); 
File file = new File(path + "/config.txt");
String strContent  = "my file save"; 

try
{
FileOutputStream fos = new FileOutputStream(file, false);
fos.write(strContent.getBytes());
fos.close();
}
catch(Exception e)
{
Log.e("klx", e.toString());
}


And how to read from file:

File myFile = mCon.getDir("myFile", Activity.MODE_WORLD_WRITEABLE); 
String path = myFile.getAbsolutePath(); 
File file = new File(path + "/config.txt");

if (file.exists() == false)  
{
    // do something
}

try 
{            
FileInputStream fis = new FileInputStream(path + "/config.txt");
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(fis));
        String str = bufferReader.readLine(); 
   
Log.d("klx", "klx  " + str);  // Now you have what you want. 
   
        fis.close();  
     
}    
catch(IOException e)
{
     Log.e("klx", "e.toString());  
}

Oct 17, 2013

[android] StringTokenizer



Same as in Java.

 StringTokenizer stk = new StringTokenizer(strTest, "/");

while(stk.hasMoreElements())
{
     System.out.println(stk.nextToken());  // or stk.nextElement().toString(); 
}


Oct 10, 2013

[android] How to bring app in background to foreground

First, you should get the list of active apps like this.
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasklist =am.getRunningTasks(10);
// Number of tasks you want to get

Then, you will have the active app list in  tasklist.  
moveTaskToFront () function will bring the app to foreground.
if(!tasks.isEmpty()) 
{
      int nSize = 
tasklist.size();
      for(int i = 0; i >= nSize;  i++) 
     {
            RunningTaskInfo taskinfo = tasklist.get(i);
           if(taskinfo.topActivity.getPackageName().equals("name of the package")) 
         {
                 
am.moveTaskToFront(taskinfo.id, 0);
         }
     }
}

Finally, add permissions to the manifest.

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

Oct 8, 2013

[android] How to hide action bar

If you create a project, action bar will be shown by default.
Anyway, you can hide or show action bar in the run time.

hide:    getActionBar().hide(); 
show:  getActionBar().show(); 



This might be better:

final ActionBar actionBar = getActionBar();
actionBar.hide(); 




[Unity3d] How to access variables from other scripts

This is based on Unity 4.1. It might be different depending on Unity versions. 
Suppose that you want to access aaa.js from bbb.js. 

aaa.js

var nSource : int;  //  It doesn`t have to be  static, but private is not allowed here. 

bbb.js 

var src  : aaa;
function Start ()
{
      src =  FindObjectOfType(aaa);
}

Usage: 
function customFunction()
{
        var a = aaa.nSource; 
}

[Unity3d] How to convert 3d coords to 2d GUI position

You can use WorldToScreenPoint function. 

var screenPos : Vector3 = GameObject.FindWithTag("MainCamera").camera.WorldToScreenPoint( transform.position); 


[Unity3d] C++ dll for iPhone and XBox 360

#if UNITY_IPHONE || UNITY_XBOX360

   // On iOS and Xbox 360 plugins are statically linked into
   // the executable, so we have to use __Internal as the
   // library name.
   [DllImport ("__Internal")]

   #else

   // Other platforms load plugins dynamically, so pass the name
   // of the plugin's dynamic library.
   [DllImport ("PluginName")]

   #endif
iOS and X-Box use dll as a static library. So, you should use "__internal" when importing. 

[Unity3d] How to use C++ dll in Unity3d

1. Make CLI  Class library project in Visual Studio. 


2.  Delete all files except main cpp  file. You don`t need other auto created files. 


3. Write down the codes you need like this. 

4.  Drag and drop the dll file to Unity Assets/Plugins. It is recommended that you use release mode rather than debug mode while building the project. 
(Make sure you drag and drop to Plugins folder. Other folders  may not work or cause errors.) 
5.  Create a C# script and attach it to a game object. Write code as below.  
 Then you will have value of '1102' in integer variable "nRet". 

[Unity3d] How to change mouse cursor.

1.  Put the png file in  "Assets/Textures/UI/Resources "
2. Write the code as below.   
Then you will see the mouse cursor changed if the mouse button is down. 
Have fun. 

[Unity3d] How to pop up web browser.

Application.ExternalEval("window.open('http://www.google.com','_blank')");  

Caution: Popup prevention option of the web browser must be turned off. 

[Unity3d] How to send message from web browser to Unity Web Player.

It is especially useful, when you start and initialize your unity web player. 
First, you need to write java script in the web side like this. 
 document.getElementById("UnityContent").SendMessage("UnityObject", "UnityFunction", "argument"); 

If you  have Unity Object, then it will be more simple like this 
var u = new UnityObject2(config) ;
u.getUnity().SendMessage("UnityObject", "UnityFunction", "argument"); 

Then make an empty game object in unity editor. In this case,  "UnityObject" is the name of the game object. Add a javascript to the object and write as below. 
function UnityFunction(param : string)
{
      var ret = param;
}

Then your Unity Web Player will have the message from the web server. 
Caution : 
While loading, Unity Web Player can not receive any messages. So, is is possible that the message is ignored. To prevent this, ask the web server to send message after completing loading like this. 
function Start () 
{
    Application.ExternalCall("GetMessage");
}
GetMessage is a user defined function name. And write in your web server as below. 

        function GetMessage()
      {
          u.getUnity().SendMessage("IdObject", "PutPlayerId", "serfefer");
      }

Then, web server will send a message after Unity Web Player is completely loaded, and no message will be lost. 

[MySQL] How to fetch only 10 results from "SELECT"

Oracle, MS-SQL, and MySQL differs slightly. Here is the MySQL version. 
Just add "LIMIT" after selection statement. 

     SELECT nIdx 
        FROM tb_table 
ORDER BY time DESC 
        LIMIT 10; 

[C++] How to check client`s IP address.

When you call  accept(),  you will have client`s  information in  SOCKADDR_IN structure. 
 SOCKADDR_IN clntAddr; 
int addrLen = sizeof(clntAddr);  
SOCKET hSocket = ::accept(mListenSocket, (SOCKADDR*)&clntAddr, &addrLen); 
printf("Server IP = %s\n"inet_ntoa(clntAddr.sin_addr)); 

Result will be like this:     "Server IP = 211.22.32.15" 

[C#] How to add comma to numbers


int nNum = 123456;  
string strNum = nNum.ToString("#.##0");

Then you will have "123,456". 

[android] How to add listener to hardware button.

How to add listener to hardware menu button…
It is simple and easy.  Just override it. 
    @Override
    public boolean onKeyDown(int keycode,  KeyEvent e)
    {
        switch(keycode)
        {
        case KeyEvent.KEYCODE_MENU:
            Toast.makeText(this,  "Menu Button", Toast.LENGTH_SHORT).show(); 
            return true;           
        case KeyEvent.KEYCODE_BACK:
           Toast.makeText(this,  "Back Button", Toast.LENGTH_SHORT).show();  
            return true;           
        case KeyEvent.KEYCODE_HOME:
           Toast.makeText(this,  "Home Button", Toast.LENGTH_SHORT).show();  
            return true;           
        }     
        return super.onKeyDown(keycode,  e);
    }

[android] How to extract list of all apps installed on the device.

If you want to make a launcher, this might be helpful. 
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
List<ResolveInfo> pkgAppList = getPackageManager().queryIntentActivities(mainIntent, 0);       

Then you have your list in "pkgAppList". 

[android] How to hide keyboard

If you are frustrated because soft keyboard of your app does not disappear, try this. 

InputMethodManager imm = (InputMethodManager) getSystemService (Context.INPUT_METHOD_SERVICE); 

imm.toggleSoftInput(0,  0);  

[Unity3d] How to change scenes without interrupting or pausing the game.

GetStreamProgressForLevel returns progression of the loading. 
You can show the progress bar using this  function. 
If you want to change scenes without interruption, do like this. 

if(Application.GetStreamProgressForLevel("mainui") == 1)
{    
        Application.LoadLevel("mainui");
}  

[Unity3d] How to find camera

If  you can check the tag of the game object, it is as simple as below. 

GameObject.FindWithTag("MainCamera").camera