2013年12月12日 星期四

copy file

public static void copyFile(String originalFileName, String destFileName) 
            throws Exception {
        byte[] buffer = new byte[4096]; 
        int size = -1;
        FileInputStream fis = new FileInputStream(originalFileName); 
        FileOutputStream fos = new FileOutputStream(destFileName);
        while ((size = fis.read(buffer, 0, buffer.length)) != -1) { 
            fos.write(buffer, 0, size); 
        }
        fis.close(); 
        fos.close();
        fis = null; 
        fos = null;
    }

2013年12月10日 星期二

delete directory / Folder

public static void deleteDirectory( File dir )
{

    if ( dir.isDirectory() )
    {
        String [] children = dir.list();
        for ( int i = 0 ; i < children.length ; i ++ )
        {
         File child =    new File( dir , children[i] );
         if(child.isDirectory()){
             deleteDirectory( child );
             child.delete();
         }else{
             child.delete();

         }
        }
        dir.delete();
    }
}

packagename to label

final PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
   ai = pm.getApplicationInfo("呢度係packagename", 0);
} catch (final NameNotFoundException e) {
   ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");

2013年12月5日 星期四

Android Activity生命週期 OnPause() -> OnStop(). OnReStart() -> OnStart() -> OnResume().onDestroy().OnCreate()

通過這個練習知道, 原來按下HOME按鈕離開應用程式, 和按下BACK按鈕或調用finish()方法離開應用程式是不相同的.

按下HOME按鈕離開應用程式, 會順序調用: OnPause() -> OnStop().
再次進入應用程式會調用: OnReStart() -> OnStart() -> OnResume().

按下BACK按鈕或調用finish()方法離開應用程式: OnPause() -> OnStop() -> onDestroy().
再次進入應用程式會調用: OnCreate() -> OnStart() -> OnResume().


package com.AndroidLifeCycle;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class AndroidLifeCycle extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       
       Button finish = (Button)findViewById(R.id.finish);
       finish.setOnClickListener(new Button.OnClickListener(){
 
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    finish();
   }});
       
       Toast.makeText(this, "onCreate", Toast.LENGTH_LONG).show();
       
   }
 
 @Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
  Toast.makeText(this, "onDestroy", Toast.LENGTH_LONG).show();
 }
 
 @Override
 protected void onPause() {
  // TODO Auto-generated method stub
  super.onPause();
  Toast.makeText(this, "onPause", Toast.LENGTH_LONG).show();
 }
 
 @Override
 protected void onRestart() {
  // TODO Auto-generated method stub
  super.onRestart();
  Toast.makeText(this, "onRestart", Toast.LENGTH_LONG).show();
 }
 
 @Override
 protected void onResume() {
  // TODO Auto-generated method stub
  super.onResume();
  Toast.makeText(this, "onResume", Toast.LENGTH_LONG).show();
 }
 
 @Override
 protected void onStart() {
  // TODO Auto-generated method stub
  super.onStart();
  Toast.makeText(this, "onStart", Toast.LENGTH_LONG).show();
 }
 
 @Override
 protected void onStop() {
  // TODO Auto-generated method stub
  super.onStop();
  Toast.makeText(this, "onStop", Toast.LENGTH_LONG).show();
 }
}
Related Posts Plugin for WordPress, Blogger...