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! : )

Use clipboard in Android

You can access the clipboard ClipboardManager service

Main code is as follows: 

import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.text.ClipboardManager;import android.text.TextUtils;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class ClipboardTestActivity extends Activity implements OnClickListener {	private EditText editText;	private Button copyButton;	private Button pasteButton;	private Button clearButton;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        editText = (EditText) findViewById(R.id.editText1);        copyButton = (Button) findViewById(R.id.button1);        pasteButton = (Button) findViewById(R.id.button2);        clearButton = (Button) findViewById(R.id.button3);        copyButton.setOnClickListener(this);        pasteButton.setOnClickListener(this);        clearButton.setOnClickListener(this);    }	@Override	public void onClick(View v) {		switch (v.getId()) {		case R.id.button1:			copy("Number:" + Math.random() * 100);			break;		case R.id.button2:			paste(editText);			break;		case R.id.button3:			clear();			break;		default:			break;		}	}	private void clear() {		ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);		clipboardManager.setText(null);	}	private void paste(EditText editText) {		ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);		CharSequence text = clipboardManager.getText();		if (TextUtils.isEmpty(text)) {			Toast.makeText(this, "The clipboard is empty!", Toast.LENGTH_SHORT).show();			return;		}		editText.setText(text);	}	private void copy(String text) {		ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);		clipboardManager.setText(text);	}}

Steps:

  1. Copy: Produce a number randomly, install clipboard next;
  2. Paste: Obtain data from clipboard,  installing EditText;
  3. Clear: Clear empty clipboard—-Stopped with Copy principle actually;

 

PS: Detailed content asks referenced developer documentation! : )