2012년 6월 21일 목요일

[Android] application이 background에 있는지 확인...



public static boolean isApplicationInBackground(Context context) 
{
    ActivityManager am = 
      (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    List<RunningTaskInfo> tasks = am.getRunningTasks(1);

    if (!tasks.isEmpty()) 
    {
        ComponentName topActivity = tasks.get(0).topActivity;

        if (!topActivity.getPackageName().equals(context.getPackageName())) 
        {
            return true;
        }
    }

    return false;
}

 출처:
http://stackoverflow.com/questions/3236746/determining-if-app-is-running-in-background

2012년 6월 13일 수요일

[Android] exit 확인창 출력

Back key를 눌렀을때, 종료 확인창을 출력..
한번 더누르면 종료 처리..

@Override
 protected Dialog onCreateDialog(int id) {
  switch(id)
  {
   case DIALOG_QUIT:
    return new AlertDialog.Builder(this)
    .setMessage("Are you sure you want to leave the game?")
    .setPositiveButton("YES", new DialogInterface.OnClickListener() {
    
     @Override
     public void onClick(DialogInterface dialog, int which) {
      finish();
     }
    })
    .setNegativeButton("NO", new DialogInterface.OnClickListener() {
    
     @Override
     public void onClick(DialogInterface dialog, int which) {
      dialog.dismiss();
     }
    })
    .setOnKeyListener(new DialogInterface.OnKeyListener() {
           @Override
           public boolean onKey (DialogInterface dialog, int keyCode, KeyEvent event) {
               if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled()) {
                dialog.dismiss();
                finish();
                   return true;
               }
               return false;
           }
       }).create();
   
    //break;
  
   default:
    break;
  }
 
  return null;
 }

2012년 6월 12일 화요일

[Android] (JNI) native function name...

Native function Name:
[return value]_[package name]_[class name]_[function name](...)

ex:

jstring Java_com_jj_example_HelloWorldActivity_stringFromJNI(JNIEnv* env, jobject thiz)
 

2012년 6월 11일 월요일

[Android] idle(sleep) mode 방지 코드...

private static PowerManager.WakeLock fullWakeLock;

PowerManager pm = (PowerManager) m_sInstance.getSystemService(Context.POWER_SERVICE);
fullWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "GangstarGreeActivity");
keepScreenOn(true);


public static boolean keepScreenOn(boolean keep)
{
  if (keep && !fullWakeLock.isHeld())
  {
    if (fullWakeLock != null)
    {
      fullWakeLock.acquire();
      return true;
    }
  }
  else if (!keep && fullWakeLock.isHeld())
  {
    if (fullWakeLock != null)
    {
      fullWakeLock.release();
      return true;
    }
  }
 
  return false;
}