June 12, 2011

How to Prevent Screen Lock in Android while Running Application


To prevent lock while running the application can be done two ways.

->PowerManager

->WindowManager

Using Power Manager is not a good way. They easily drain battery. So using Window Manager we can avoid the screen locking while application is running. Add the below snippet in your activity to avoid screen lock :
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

No permissions required. Put the above code in the activity.

June 11, 2011

Checking Availability of Internet from Android

The following method returns true if internet is available and false if not available.

public boolean checkInternetAvailablity()
{
ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo().isConnected();
}

June 01, 2011

How to Show Download Progress Dialog in Android

In your application u want to get the data from the server. In this example i am putting all that parsing logic in a thread and showing progress dialog while loading the data. After getting all the data i am dismissing the dialog.
The following example illustrates the same ...


public void parsing()
{
        ProgressDialog pdlg ;
     
       //SHOWING DIALOG
        pdlg = ProgressDialog.show(getParent(), "", "Loading..");
        Thread thr_parsing = new Thread()
        {
            public void run()
            {
               //do the parsing
                runOnUiThread(new Runnable()
                {                   
                    @Override
                    public void run()
                    {   
                            //DISMISSING THE DIALOG
                            pdlg.dismiss();
                    }
                });
            }
        };
        thr_parsing.start();
       
    }

May 22, 2011

How to Pass Intent to Browser in Android

Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(stringurl));
startActivity(intent);

May 17, 2011

How to Hide Title Bar in Android Application

  To hide the title bar through out application we have to include android:theme attribute to application tag in the Andriod Manifast.xml


 <application android:name="TestApplication" android:theme="@android:style/Theme.NoTitleBar">


Then in entire application title bar was not visible.

May 15, 2011

How to Get Current Screen Orientation In Android

The following example gives the current screen orientation.

public int getScreenOrientation()
{
        return getResources().getConfiguration().orientation;
}

The above method return integer. We will compare as follows.

 if(getScreenOrientation() == Configuration.ORIENTATION_PORTRAIT)
 {
            //App is in Portrait mode
 }
 else
{
           //App is in LandScape mode
}

May 11, 2011

How to Check Availability of Wi-Fi through Android

 The following method Return whether Wi-Fi is enabled or disabled.
 True if enabled otherwise false.
 
 public boolean checkWIFI()
 {
        WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        return wm.isWifiEnabled();
 }