Tuesday, November 9, 2010

Check Internet Connection

import YourPackageName

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.widget.Toast;

class CheckInternetConnection
{
public Context context = null;
public ProgressDialog dialogProcess = null;

public CheckInternetConnection(Context ctx)
{
this.context = ctx;
}

@SuppressWarnings("unchecked")
public boolean CheckInternet()
{
ConnectivityManager connec = (ConnectivityManager)context.getSystemService(context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = connec
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

if (wifi.isConnected())
{
return true;
}
else if (!mobile.isConnected())
{
Toast.makeText(context,"No Internet Connection",5000).show();
return false;
}
else
{
new AsyncCheckInternet().execute();
}

return false;
}

private class AsyncCheckInternet extends AsyncTask
{
@Override
protected void onPreExecute()
{
dialogProcess = ProgressDialog.show(context, "","Checking Internet Connection...",true);
}

@Override
protected Boolean doInBackground(Object... arg0)
{
try
{
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url
.openConnection();
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(3000); // This is time limit if the connection time limit
urlc.connect();
if (urlc.getResponseCode() == 200)
{
return true;
}
}
catch (MalformedURLException e1)
{
e1.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
}

@Override
protected void onPostExecute(Object result)
{
super.onPostExecute(result);
dialogProcess.dismiss();
}

}
}

Sunday, September 19, 2010

Custom Listview with Image

public static ArrayList vidID = new ArrayList();
public static Hashtable> VideoTable = new Hashtable>();
Hashtable _tempVideo = new Hashtable();
ArrayList db_id;

--Start any required Loop

_tempVideo.put("id", curValue);
_tempVideo.put("title", curValue);
_tempVideo.put("filename", curValue);
_tempVideo.put("thumbnail", curValue);

VideoTable.put(curVidId, _tempVideo);
vidID.add(curVidId);

_tempVideo = new Hashtable();

---End Loop

private void fillData() {

ListView list = (ListView) findViewById(R.id.ListView01);

ArrayList> mylist = new ArrayList>();
HashMap map;

db_id = new ArrayList();
for (int i = 0; i < vidID.size(); i++) {
Hashtable temp = VideoTable
.get(vidID.get(i));
try {
map = new HashMap();
map.put("id", temp.get("id"));
map.put("title", temp.get("title"));
map.put("description", temp.get("description"));
map.put("theartist", temp.get("theartist"));
map.put("duration", temp.get("duration"));
map.put("views", temp.get("views") + " views");
map.put("server", temp.get("server"));
map.put("filename", temp.get("filename"));
map.put("thumbnail", temp.get("thumbnail"));

mylist.add(map);
db_id.add(vidID.get(i));
} catch (Exception e) {
}
}
SimpleAdapter videolist = new SimpleAdapter(this, mylist,
R.layout.videorow, new String[] { "id", "title", "views",
"duration", "description", "server", "filename",
"thumbnail"}, new int[] { R.id.TextView05,
R.id.TextView01, R.id.TextView02, R.id.TextView03,
R.id.TextView04, R.id.TextView06, R.id.TextView07,
R.id.TextView08 });
list.setAdapter(videolist);
list.setOnItemClickListener(this);
list.setTextFilterEnabled(true);
list.setOnHierarchyChangeListener(this);
}

@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {

TextView filename = (TextView) arg1.findViewById(R.id.TextView07);
String url = filename.getText().toString();
Log.e(".", url);
Bundle bundle = new Bundle();
bundle.putString("URL", url);
Intent myIntent = new Intent();
myIntent.setClassName("Package Name", "Package Name With Class");
myIntent.putExtras(bundle);
startActivity(myIntent);
}


@Override
public void onChildViewAdded(View parent, View child) {
View row = child;
try {
ImageView imView = (ImageView) row.findViewById(R.id.ImageView01);
imView.setImageBitmap(null);
TextView id = (TextView) row.findViewById(R.id.TextView05);
TextView thumbnail = (TextView) row.findViewById(R.id.TextView08);
TextView filename = (TextView) row.findViewById(R.id.TextView07);
imView.setTag(filename.getText().toString());
String imgurl = thumbnail.getText().toString();
grabURL(imgurl, imView, filename.getText().toString());

ImageView addFav = (ImageView) row.findViewById(R.id.ImageView02);
addFav.setTag(id.getText().toString());
addFav.setOnClickListener(myListner);
} catch (Exception e) {
}
}
@Override
public void onChildViewRemoved(View parent, View child) {

try {
View row = child;
ImageView imView = (ImageView) row.findViewById(R.id.ImageView02);
imView.setTag(null);
imView.setImageBitmap(null);
} catch (Exception e) {
}
}


public void grabURL(String url, ImageView iv, String name) {
GrabURL a = new GrabURL();
a.imgView1 = iv;
a.name = name;
a.execute(url);
}

private class GrabURL extends AsyncTask {
ImageView imgView1;
String name;

protected void onPreExecute() {
}

protected Bitmap doInBackground(String... urls) {
URL myFileUrl = null;
Bitmap bmImg = null;
try {
myFileUrl = new URL(urls[0]);
} catch (MalformedURLException e) {
}

try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);

} catch (Exception e) {
Log.e(".", e.getMessage());
}
return bmImg;
}

protected void onPostExecute(Bitmap bm) {
if (name == imgView1.getTag().toString())
imgView1.setImageBitmap(bm);
}

}

Sunday, July 4, 2010

Remove app from Emulator

First,make sure the emulator is running.Then follow below steps:

1. go to tools directory in command

2. adb shell

3. cd /data/app

4. ls (It will display all the .apk installed in your emulator)

5. rm ***.apk(which you want to remove)

6. exit

Installtion on Window

1) To Install the Android on Your Window Machine
you need the following .....

A) JDK (5.0 or 6.0) Link -

B) Eclipse 3.5 Link - http://www.eclipse.org/downloads/
(Download the First One Which has 190 MB)

C) Android SDK - http://developer.android.com/sdk/index.html

2) Install the JDk

3) Extract the Eclipse

4) Unzip Android SDK at any location you want...

5) On Windows, right-click on My Computer, and select Properties.
Under the Advanced tab, hit the Environment Variables button,
and in the dialog that comes up, double-click on Path (under System
Variables).

Add the full path to the tools/ directory to the path.

6) Now Open Eclipse Go To Help->Install New Software->Add
Click on the add button that will open a new dialog box

Name : ADT Plugins
URL : https://dl-ssl.google.com/android/eclipse/

7) Back in the Available Software view, you should now see
"Developer Tools" added to the list. Select the checkbox
next to Developer Tools, which will automatically select the
nested tools Android DDMS and Android Development Tools.
Click Next.

8) In the resulting Install Details dialog, the Android DDMS
and Android Development Tools features are listed.
Click Next to read and accept the license agreement and
install any dependencies, then click Finish.

9) Restart the Eclipse.

10) Once you've successfully downnloaded ADT as described above,
the next step is to modify your ADT preferences in
Eclipse to point to the Android SDK directory:

1. Select Window > Preferences... to open the Preferences panel (Mac

OS X: Eclipse > Preferences).
2. Select Android from the left panel.
3. For the SDK Location in the main panel, click Browse... and

locate your downloaded SDK directory.
4. Click Apply, then OK.

11) Now Go Where You extract the android SDk....
Click on SDK Set Up

12) That will open a dialog box of Android SDK and AVD Manager

13) Go to Available Packages Click on the Check box
which is located in your right...

14) It Will Show u List of Api....

15) Then please tick on Your SDK Platform Android ( Your Version ) API

16) After selecting API Please Click on Install Selected Button
Which is located right bottom

17) After Installing Your Api go to Virtual Device listed in left ..

18) Click on New Button ...

19) GIve ANy Name ... And Select Your Target API....
Rest of Informatio is Optional...then click on Create AVD

20) Now Open the Eclipse and Enjoy the Android....

Installation on Linux

1) To Install the Android on Your Linux Machine
you need the following .....

A) JDK (5.0 or 6.0)

B) Eclipse 3.5 Link - http://www.eclipse.org/downloads/
(Download the First One Which has 190 MB)

C) Android SDK - http://developer.android.com/sdk/index.html

2) Install the JDk

3) Extract the Eclipse

4) Unzip Android SDK at any location you want...
(Recomended - /home/usr)

5) On Linux, edit your ~/.bash_profile or ~/.bashrc file.
Look for a line that sets the PATH environment variable and add
the full path to the tools/ directory to it. If you don't see a line
setting the path, you can add one:

export PATH=${PATH}:/tools

6) Now Open Eclipse Go To Help->Install New Software->Add
Click on the add button that will open a new dialog box

Name : ADT Plugins
URL : https://dl-ssl.google.com/android/eclipse/

7) Back in the Available Software view, you should now see
"Developer Tools" added to the list. Select the checkbox
next to Developer Tools, which will automatically select the
nested tools Android DDMS and Android Development Tools.
Click Next.

8) In the resulting Install Details dialog, the Android DDMS
and Android Development Tools features are listed.
Click Next to read and accept the license agreement and
install any dependencies, then click Finish.

9) Restart the Eclipse.

10) Once you've successfully downnloaded ADT as described above,
the next step is to modify your ADT preferences in
Eclipse to point to the Android SDK directory:

1. Select Window > Preferences... to open the Preferences panel (Mac OS X: Eclipse >
Preferences).
2. Select Android from the left panel.
3. For the SDK Location in the main panel, click Browse... and
locate your downloaded SDK directory.
4. Click Apply, then OK.

11) Now Go Where You extract the android SDk....
tools->android (Click on It)

12) That will open a dialog box of Android SDK and AVD Manager

13) Go to Available Packages Click on the Check box
which is located in your right...

14) It Will Show u List of Api....

15) Then please tick on Your SDK Platform Android ( Your Version ) API

16) After selecting API Please Click on Install Selected Button
Which is located right bottom

17) After Installing Your Api go to Virtual Device listed in left ..

18) Click on New Button ...

19) GIve ANy Name ... And Select Your Target API....
Rest of Informatio is Optional...then click on Create AVD

20) Now Open the Eclipse and Enjoy the Android....