Contact for Android Technical support, Free lancing and online training +919133711199, Email id: rams4android@gmail.com
Monday, 30 December 2013
Forward GeoCoding Example
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="enter address to find location"
android:inputType="textPersonName" />
<Button
android:id="@+id/button_find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find Location"
android:layout_gravity="center"
android:layout_marginTop="20dp" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location is :"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="enter address to find location"
android:inputType="textPersonName" />
<Button
android:id="@+id/button_find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find Location"
android:layout_gravity="center"
android:layout_marginTop="20dp" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location is :"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
MainActivity.java:
package com.ram.forwardgeocoding;
import java.io.IOException;
import java.util.List;
import android.app.Activity;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText edit;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.editText);
text = (TextView) findViewById(R.id.textView);
Button findBtn = (Button) findViewById(R.id.button_find);
findBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String address = edit.getText().toString();
Geocoder geoCoder = new Geocoder(getApplicationContext());
try {
List<Address> locations = geoCoder.getFromLocationName(
address, 1);
for (Address a : locations) {
double latti = a.getLatitude();
double longi = a.getLongitude();
text.append(latti + "," + longi);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
import java.io.IOException;
import java.util.List;
import android.app.Activity;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText edit;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.editText);
text = (TextView) findViewById(R.id.textView);
Button findBtn = (Button) findViewById(R.id.button_find);
findBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String address = edit.getText().toString();
Geocoder geoCoder = new Geocoder(getApplicationContext());
try {
List<Address> locations = geoCoder.getFromLocationName(
address, 1);
for (Address a : locations) {
double latti = a.getLatitude();
double longi = a.getLongitude();
text.append(latti + "," + longi);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ram.forwardgeocoding"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.ram.forwardgeocoding.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ram.forwardgeocoding"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.ram.forwardgeocoding.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Wednesday, 25 December 2013
IntentService with WebService Example
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="getData"
android:text="get data" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</ScrollView>
</LinearLayout>
MyWebService.java:
package com.ram.intentservice;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.IntentService;
import android.content.Intent;
public class MyWebService extends IntentService {
public MyWebService() {
super("DownloadService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://www.cheesejedi.com/rest_services/get_big_cheese.php?puzzle=");
try {
HttpResponse response = httpclient.execute(httppost);
String jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
if (jsonResult != null) {
sendResultBroadcast(jsonResult);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader rd = new BufferedReader(isr);
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
e.printStackTrace();
}
return answer;
}
void sendResultBroadcast(String jsonResult) {
Intent in = new Intent();
in.putExtra("jsonresult", jsonResult);
in.setAction("com.ram.CUSTOM_BROADCAST");
sendBroadcast(in);
}
}
MainActivity.java:
package com.ram.intentservice;
import org.json.JSONArray;
import org.json.JSONException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView textView;
ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(new WebServiceBroadcast(), new IntentFilter(
"com.ram.CUSTOM_BROADCAST"));
textView = (TextView) findViewById(R.id.textView);
}
public void getData(View v) {
pd = ProgressDialog.show(MainActivity.this, "loading", "plz wait");
Intent intentService = new Intent(getApplicationContext(),
MyWebService.class);
startService(intentService);
}
class WebServiceBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
pd.cancel();
String jsonResult = intent.getStringExtra("jsonresult");
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(jsonResult);
for (int i = 0; i < jsonArray.length(); i++) {
textView.append("id: "
+ jsonArray.getJSONObject(i).getString("id")
.toString() + "\n");
textView.append("level: "
+ jsonArray.getJSONObject(i).getString("level")
.toString() + "\n");
textView.append("time_in_secs: "
+ jsonArray.getJSONObject(i)
.getString("time_in_secs").toString()
+ "\n");
textView.append("par: "
+ jsonArray.getJSONObject(i).getString("par")
.toString() + "\n");
textView.append("initials: "
+ jsonArray.getJSONObject(i).getString("initials")
.toString() + "\n");
textView.append("quote: "
+ jsonArray.getJSONObject(i).getString("quote")
.toString() + "\n");
textView.append("time_stamp: "
+ jsonArray.getJSONObject(i)
.getString("time_stamp").toString() + "\n");
textView.append("" + "\n");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ram.intentservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.ram.intentservice.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="MyWebService"></service>
</application>
</manifest>
Wednesday, 11 December 2013
Using HttpURLConnection class in Android
Download this project---->HttpUrlConnection.zip
ScreenShot:
ScreenShot:
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
MainActivity.java:
package com.ram.httpurlconnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.widget.TextView;
@SuppressLint("NewApi")
public class MainActivity extends Activity {
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.textView);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
// The URI class can be used to manipulate URLs of any protocol.
URL url = new URL("http://www.google.com");
// Obtain a new HttpURLConnection by calling URL.openConnection()
// and casting the result to HttpURLConnection.
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// Returns an InputStream for reading data from the resource pointed
// by this URLConnection
readStream(con.getInputStream());
} catch (Exception e) {
e.printStackTrace();
/*
* Toast.makeText(getApplicationContext(), e.getMessage(),
* Toast.LENGTH_LONG).show();
*/
text.setText(e.getMessage());
}
}
private void readStream(InputStream in) {
StringBuffer sb = new StringBuffer();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line);
}
text.setText(sb.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ram.httpurlconnection"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.ram.httpurlconnection.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Monday, 9 December 2013
Internal Storage Example in Android
Download this example here : InernalStorage.zip
Screen Shots:
you can check file creation in DDMS Perspective as follows:
Screen Shots:
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="enter file name:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText_filename"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="enter text to save"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="@+id/button_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Create file and save text" />
<Button
android:id="@+id/button_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="view text from saved file" />
<TextView
android:id="@+id/textView_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="enter file name:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText_filename"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="enter text to save"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="@+id/button_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Create file and save text" />
<Button
android:id="@+id/button_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="view text from saved file" />
<TextView
android:id="@+id/textView_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
MainActivity.java:
package com.ram.internalstorage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText editFile, editText;
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editFile = (EditText) findViewById(R.id.editText_filename);
editText = (EditText) findViewById(R.id.editText_text);
text = (TextView) findViewById(R.id.textView_display);
Button saveBtn = (Button) findViewById(R.id.button_save);
Button viewBtn = (Button) findViewById(R.id.button_view);
saveBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String fileName = editFile.getText().toString();
String textToSave = editText.getText().toString();
try {
FileOutputStream fos = openFileOutput(fileName + ".txt",
MODE_PRIVATE);
fos.write(textToSave.getBytes());
fos.close();
Toast.makeText(getApplicationContext(),
"file created and text saved", Toast.LENGTH_LONG)
.show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
viewBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
FileInputStream fis = openFileInput(editFile.getText()
.toString() + ".txt");
StringBuffer sb = new StringBuffer();
int i;
while ((i = fis.read()) != -1) {
sb.append((char) i);
}
fis.close();
text.setText(sb.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText editFile, editText;
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editFile = (EditText) findViewById(R.id.editText_filename);
editText = (EditText) findViewById(R.id.editText_text);
text = (TextView) findViewById(R.id.textView_display);
Button saveBtn = (Button) findViewById(R.id.button_save);
Button viewBtn = (Button) findViewById(R.id.button_view);
saveBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String fileName = editFile.getText().toString();
String textToSave = editText.getText().toString();
try {
FileOutputStream fos = openFileOutput(fileName + ".txt",
MODE_PRIVATE);
fos.write(textToSave.getBytes());
fos.close();
Toast.makeText(getApplicationContext(),
"file created and text saved", Toast.LENGTH_LONG)
.show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
viewBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
FileInputStream fis = openFileInput(editFile.getText()
.toString() + ".txt");
StringBuffer sb = new StringBuffer();
int i;
while ((i = fis.read()) != -1) {
sb.append((char) i);
}
fis.close();
text.setText(sb.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
Thursday, 5 December 2013
REST+JSON Web service Example in Android.
Download this project-->REST+JSONWebserviceExample.zip
output:
output:
activity_main.xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ScrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</ScrollView>
MainActivity.java:
package com.ram.restjsonwebserviceexample;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.widget.TextView;
@SuppressLint("NewApi")
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_main);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://www.cheesejedi.com/rest_services/get_big_cheese.php?puzzle=");
TextView textView = (TextView) findViewById(R.id.textView1);
try {
HttpResponse response = httpclient.execute(httppost);
String jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
JSONArray jsonArray = new JSONArray(jsonResult);
for (int i = 0; i < jsonArray.length(); i++) {
textView.append("id: "
+ jsonArray.getJSONObject(i).getString("id").toString()
+ "\n");
textView.append("level: "
+ jsonArray.getJSONObject(i).getString("level")
.toString() + "\n");
textView.append("time_in_secs: "
+ jsonArray.getJSONObject(i).getString("time_in_secs")
.toString() + "\n");
textView.append("par: "
+ jsonArray.getJSONObject(i).getString("par")
.toString() + "\n");
textView.append("initials: "
+ jsonArray.getJSONObject(i).getString("initials")
.toString() + "\n");
textView.append("quote: "
+ jsonArray.getJSONObject(i).getString("quote")
.toString() + "\n");
textView.append("time_stamp: "
+ jsonArray.getJSONObject(i).getString("time_stamp")
.toString() + "\n");
textView.append("-------------------------------------------------"
+ "\n");
}
// textView.setText(jsonResult);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader rd = new BufferedReader(isr);
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
e.printStackTrace();
}
return answer;
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ram.restjsonwebserviceexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.ram.restjsonwebserviceexample.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Tuesday, 3 December 2013
Custom BroadcastReceiver in Android
This example will show you how to create our own custom BroadCastReceiver in Android.
ScreenShots:
strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">CustomBroadcastReceiver</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="textview_title">Custom BroadCast Receiver example</string>
<string name="button_title">Custom BroadCast</string>
</resources>
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="108dp"
android:text="@string/button_title"
android:onClick="customBroadCast"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="@string/textview_title"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
CustomBroadCast.java:
package com.ram.custombroadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class CustomBroadCast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
Toast.makeText(context, "BroadCast Detected", Toast.LENGTH_LONG).show();
}
}
MainActivity.java:
package com.ram.custombroadcastreceiver;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(new CustomBroadCast(), new IntentFilter(
"com.ram.CUSTOM_BROADCAST"));
}
public void customBroadCast(View v) {
Intent intent = new Intent();
intent.setAction("com.ram.CUSTOM_BROADCAST");
sendBroadcast(intent);
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ram.custombroadcastreceiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.ram.custombroadcastreceiver.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--
<receiver android:name="CustomBroadCast" >
<intent-filter>
<action android:name="com.ram.CUSTOM_BROADCAST" >
</action>
</intent-filter>
</receiver>
-->
</application>
</manifest>
Subscribe to:
Posts (Atom)