Saturday, February 2, 2013

Phonegap Sqlite (android)

Below Android environment use Phonegap operates Sqlite, code sees accessory.

(function(){$('#main').live('pageshow',function(){$('#createBtn').bind('click',function(){create('test','1.0','testDb',1000000);});$('#DropBtn').bind('click',function(){dropTable('DEMO','test','1.0','testDb',1000000);});$('#selectBtn').bind('click',function(){select('test','1.0','testDb',1000000);});$('#insertBtn').bind('click',function(){$('#addWindow').popup('open');});$('#saveBtn').bind('click',onSaveBtnClick);$('#deleteBtn').bind('click',onDeleteBtnClick);});function onSaveBtnClick(){var id = $('#idfield').val();var data = $('#datafield').val();if(id!=''&& data!=''){var db = getDb('test','1.0','testDb',1000000);db.transaction(function(tx){tx.executeSql('INSERT INTO DEMO (id, data) VALUES (?, ?)', [id,data]);}, errorCB, function(){$('#addWindow').popup('close');Alert(' adds successful'); newly$('#selectBtn').trigger('click');$('#msgdiv').empty();$('#idfield').val('');$('#datafield').val('');});}else{$('#msgdiv').text(' inputs Idand Data please! ! ');}}function onDeleteBtnClick(){var db = getDb('test','1.0','testDb',1000000);db.transaction(function(tx){tx.executeSql('DELETE FROM DEMO');}, errorCB, function(){Alert(' deletes overall recordsuccessfully! ');$('#resultList > tbody').empty();});}function getDb(dbName,dbVersion,dbDisplayname,dbSize){return window.openDatabase(dbName, dbVersion,dbDisplayname, dbSize);}function select(dbName,dbVersion,dbDisplayname,dbSize){var db = getDb(dbName,dbVersion,dbDisplayname,dbSize);db.transaction(queryDB, errorCB);}function dropTable(tableName,dbName,dbVersion,dbDisplayname,dbSize){var db = getDb(dbName,dbVersion,dbDisplayname,dbSize);db.transaction(function(tx){tx.executeSql('DROP TABLE IF EXISTS '+tableName);}, errorCB,function(){Alert(' deletes a success,tableName='+tableName);$('#resultList > tbody').empty();});}function queryDB(tx) {$('#resultList > tbody').empty();tx.executeSql('SELECT * FROM DEMO', [], function (tx, results) {var len = results.rows.length;var html = [];for (var i=0; i tbody').append($(html.join('')));},errorCB);}function create(dbName,dbVersion,dbDisplayname,dbSize){var db = getDb(dbName,dbVersion,dbDisplayname,dbSize);db.transaction(function(tx){tx.executeSql('DROP TABLE IF EXISTS DEMO');tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');Tx.executeSql('INSERTINTO DEMO (Id, data) VALUES (3, "Phonegap Sqlitechecks ")'); } , errorCB, function(){Alert(' establishs asuccess, dbName='+dbName);$('#selectBtn').trigger('click');});}function errorCB(err) {alert("Error processing SQL: "+err.code);}})();

Get screen resolution

The following code is to obtain screen resolution, through WindowManager.

WindowManager Wm = (WindowManager) Context.getSystemService(Context.WINDOW_SERVICE);Display D = Wm.getDefaultDisplay();MScreenWidth = D.getWidth();MScreenHeight = D.getHeight();//The following code gets condition column heightResources Res = Context.getResources();MStatusBarHeight = Context.getResources().getDimensionPixelSize(Com.android.internal.R.dimen.status_bar_height);//The following statement gets current configuration ConfigurationGetResources().getConfiguration()

Get Android WiFi Status in Android

Pass WifiManager to be able to obtain the state information of Wifi, these information are enclosed in WifiInfo, can obtain material state information from inside WifiInfo.
The means that obtains WifiManager is:
WifiManager WifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
The snippet that obtains WifiInfo is:

WifiInfo Info = WifiManager.getConnectionInfo();

The Wifi state information that can obtain has:

  • SSID;
  • BSSID;
  • MacAddress;
  • HiddenSSID;
  • IpAddress;
  • LinkSpped;
  • NetWorkId;
  • Rssi;
  • SupplicantState;

About detailed information, referenced Android SDK! : )