public class StreamToString {
public static void main(String[] args) throws Exception {
StreamToString sts = new StreamToString();
/*
* Get input stream of our data file. This file can be in
* the root of you application folder or inside a jar file
* if the program is packed as a jar.
*/
InputStream is = sts.getClass().getResourceAsStream("/data.txt");
/*
* Call the method to convert the stream to string
*/
System.out.println(sts.convertStreamToString(is));
}
public String convertStreamToString(InputStream is)
throws IOException {
/*
* To convert the InputStream to String we use the
* Reader.read(char[] buffer) method. We iterate until the
* Reader return -1 which means there's no more data to
* read. We use the StringWriter class to produce the string.
*/
if (is != null) {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(
new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
return writer.toString();
} else {
return "";
}
}
}
Wednesday, February 29, 2012
How do I convert InputStream to String in android?
How do I convert String to Date object in android?
public class StringToDate {
public static void main(String[] args) {
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
try {
Date today = df.parse("20/12/2005");
System.out.println("Today = " + df.format(today));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Below is the Result
Today = 20/12/2005
How to get HTML content of URL open in Android Webview?
In OnCreate method copy the below code and the desire'd URL....
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
progressDialog.ShowProgressDialog(0, getString(R.string.Loading_data));
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
mWebView.loadUrl(url);
return false;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
progressDialog.CloseProgressDialog();
}
@Override
public void onPageFinished(WebView view, String url) {
progressDialog.CloseProgressDialog();
mWebView.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
});
mWebView.loadUrl(selectedSocial);
And below is the method which we have registered will give the html content in String.......
/* An instance of this class will be registered as a JavaScript interface */
class MyJavaScriptInterface {
public void showHTML(String html) {
String htmlString = html;
}
}
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
progressDialog.ShowProgressDialog(0, getString(R.string.Loading_data));
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
mWebView.loadUrl(url);
return false;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
progressDialog.CloseProgressDialog();
}
@Override
public void onPageFinished(WebView view, String url) {
progressDialog.CloseProgressDialog();
mWebView.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
});
mWebView.loadUrl(selectedSocial);
And below is the method which we have registered will give the html content in String.......
/* An instance of this class will be registered as a JavaScript interface */
class MyJavaScriptInterface {
public void showHTML(String html) {
String htmlString = html;
}
}
Tuesday, February 28, 2012
HowTo: Get the selected list index on Android Activity from context menu event?
Consider
the situation where you have an Activity displaying a list of items.
You have a context menu and a normal option menu. When pressing the
option menu button, you can get the selected item and index as
follows:
//the selected index
int index = listView.getSelectedItemPosition();
//selected item
Object selObj = listView.getSelectedItem();
It
is as simple as that. If you registered a context menu on the list,
then the situation changes slightly and the
"getSelectedItemPosition()" will fail. Instead, you have to
do something like this
AdapterView.AdapterContextMenuInfo menuInfo;
menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
int index = menuInfo.position;
Example for SensorListener in Android?
public
class
TestActivity
extends
Activity
implements
SensorEventListener
{
private
SensorManager
mgr;
private
Sensor
sensorAccelerometer;
private
long
lastUpdate;
private
float
lastX
= 0.5f;
private
float
lastY
= 0.5f;
private
float
lastZ
= 0.9f;
@Override
protected
void
onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setActionBarContentView(R.layout.testactivity);
mgr
= (SensorManager)getSystemService(SENSOR_SERVICE);
sensorAccelerometer
= mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
@Override
protected
void
onResume()
{
super.onResume();
mgr.registerListener(this,
sensorAccelerometer, SensorManager.SENSOR_DELAY_UI);
}
@Override
protected
void
onPause()
{
super.onPause();
mgr.unregisterListener(this);
}
@Override
public
void
onAccuracyChanged(Sensor
sensor, int
accuracy)
{
//
TODO Auto-generated method stub
}
@Override
public
void
onSensorChanged(SensorEvent
event) {
if(event.sensor.getType()
== Sensor.TYPE_ACCELEROMETER){
long
currTime
= System.currentTimeMillis();
if((currTime
- lastUpdate) > 1000){ //just scan every second
lastUpdate
= currTime;
float
xAxis
= event.values[SensorManager.DATA_X];
float
yAxis
= event.values[SensorManager.DATA_Y];
float
zAxis
= event.values[SensorManager.DATA_Z];
String
comment = inMovement(xAxis, yAxis, zAxis) ? "not moving"
:
"in movement";
publishValues(xAxis,
yAxis, zAxis, comment);
lastX
= xAxis;
lastY
= yAxis;
lastZ
= zAxis;
}
}
}
private
boolean
inMovement(float
xAxis,
float
yAxis,
float
zAxis)
{
final
float
tolerance
= 0.5f;
if(Math.abs(xAxis
- lastX) <= tolerance && Math.abs(yAxis - lastY) <=
tolerance && Math.abs(zAxis - lastZ)<=tolerance){
return
true;
}else
return
false;
}
private
void
publishValues(float
x,
float
y,
float
z,
String comment) {
TextView
mainText =(TextView)findViewById(R.id.textView);
mainText.setText("X:
"
+
x + " - Y: "
+
y + " - Z: "
+
z + "\nComment: "
+
comment);
}
}
Subscribe to:
Posts (Atom)