Amazon Gift Cards

Sunday, October 18, 2009

Turn off, Turn on wifi in android using code tutorial

Here is how to turn on and turn off wifi in android.

First you need to declare the following in your manifest file
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>

After doing it that on your Activity class
private WifiManager wifiManager;
@Override 
public void onCreate(Bundle icicle) {
  ....................
  wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
  if(wifiManager.isWifiEnabled()){
    wifiManager.setWifiEnabled(false);
  }else{
    wifiManager.setWifiEnabled(true);
  }
}


Explanation
Get the Wifi service from our system
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

Check the our wifi is currently turned on or turned off
if(wifiManager.isWifiEnabled()){

Turn on/off our wifi
wifiManager.setWifiEnabled(<true|false>);

Reference
WifiEnabler
Permissions Journey: ACCESS_WIFI_STATE

Update History
   Jan 17, 2012 - Visual Update

Monday, October 12, 2009

How to insert image data to sqlite database in Android and how to retrieve it back

I ran across doing something that would required me to get an image from the internet and save it in the database instead of saving it in the file system then retrieving back the stored image from the database to be displayed in ImageView. Using other database server, we usually use blob to store binary data to the database, since SQLite has no field type we could use BLOB as its field type. Your create statement should appear like this
CREATE TABLE storedImages (_id INTEGER PRIMARY KEY, myImage BLOB)

To get the image from the internet and store it on our database
DefaultHttpClient mHttpClient = new DefaultHttpClient();
HttpGet mHttpGet = new HttpGet("your image url");
HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);
if (mHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  HttpEntity entity = mHttpResponse.getEntity();
    if ( entity != null) {
      // insert to database
      ContentValues values = new ContentValues();
      values.put(MyBaseColumn.MyTable.ImageField, EntityUtils.toByteArray(entity));
      getContentResolver().insert(MyBaseColumn.MyTable.CONTENT_URI, values);
    }
}


Explanation
Create a new HTTP Client
DefaultHttpClient mHttpClient = new DefaultHttpClient();

Make a request to get our image
HttpGet mHttpGet = new HttpGet("your image url");
HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);


See if our request is a 200 or has an okay return status
if (mHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

Get the entity of our request
HttpEntity entity = mHttpResponse.getEntity();

Convert the content of the entity to byte array so that we could store it on our database
values.put(MyBaseColumn.MyTable.ImageField, EntityUtils.toByteArray(entity));

Insert our image to the databse
getContentResolver().insert(MyBaseColumn.MyTable.CONTENT_URI, values);

Retrieving the image back
Here is it assumed that you are using Custom CursorAdaptor, see Custom View in your ListActivity and Custom Adapter in Android
ImageView myImage = (ImageView) findViewById(R.id.myImage);
byte[] bb = cursor.getBlob(cursor.getColumnIndex(MyBaseColumn.MyTable.ImageField));
myImage.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0, bb.length));


Explanation
Get the image back from our database
byte[] bb = cursor.getBlob(cursor.getColumnIndex(MyBaseColumn.MyTable.ImageField));

Decode our image to become a bitmap so that our imageView will display it properly
myImage.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0, bb.length));

Thursday, October 8, 2009

Launching other application using code in Android

Previously we blogged about how to delete application by their package name. Now here is how to open applications using code, note this is not the same as intent-filter.

Say you want to open fuelgauge in Android 1.6, to do this.
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.fuelgauge.PowerUsageSummary");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity( intent);


Explanation
To open other people's application, you need to make sure that in their manifest file, the author specify the class to have the android.intent.action.MAIN intent-filter added to them.
final Intent intent = new Intent(Intent.ACTION_MAIN, null);

We then add category that this new intent will be launching something
intent.addCategory(Intent.CATEGORY_LAUNCHER);

Then we get identify the application we need to open by using ComponentName, here you specify the package name of the application as first argument and the class we want to open as the second one. You must understand that com.android.settings has a lot of classes that have Main intent-filter under it making the second argument to be the specific class that we need. (this is more than one line)
final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.fuelgauge.PowerUsageSummary");

After we identify the component we want, we set it to our intent
intent.setComponent(cn);

We then tell the intent that open opening this one make it as a new task
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Then finally start our intent
startActivity( intent);

Resources
LauncherProvider.java
IRC #android-dev room

Update History
   Jan 17, 2012 - Visual Update

Tuesday, October 6, 2009

Deleting applications by their package name using codes in android

After releasing the application to delete applications by their package name, you may asked how is this done on android. And here is how i did it.
Uri uri = Uri.fromParts("package", <Your Package name here>, null);
Intent deleteIntent = new Intent(Intent.ACTION_DELETE, uri);
startActivity(deleteIntent);


Explanation
We build the proper package name using Uri.fromParts function, this will results in something like package://xxx
Uri uri = Uri.fromParts("package", <Your Package name here>, null);

We then create a new intent that have the delete action with our package uri
Intent deleteIntent = new Intent(Intent.ACTION_DELETE, uri);

Finally we start the deleteIntent that would show us the uninstall application dialog box
startActivity(deleteIntent);

Hope this helps

Update History
   Jan 17, 2012 - Visual Update