Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Dec 12, 2013

[android] How to link to google play store app market

1. Search the web and find out URL of your target app.
    It will look like this:
   
    http://play.google.com/store/apps/details?id=com.xxx.xxx


2. Insert the source code.

   Intent intent = new Intent(Intent.ACTION_VIEW,     Uri.parse("http://play.google.com/store/apps/details?id=com.xxx.xxx"));

   startActivity(intent);

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(); 




[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);