Skip to main content

How to implement Interface in android


   When we implements an interface in a class, its abstract methods are implemented in the  class.Lets see the example.

First we created an interface ApplicationInterface 
ApplicationInterface .java

public interface ApplicationInterface {
 public void activityStarted();
 public void activityAborted();
 public void activityStopped();
 public void activityDone();
}

public class MainActivity extends Activity implements ApplicationInterface {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 }

 // these are the methods implemented using interface
 @Override
 public void activityStarted() {
  // TODO Auto-generated method stub
 }

 @Override
 public void activityAborted() {
  // TODO Auto-generated method stub
 }

 @Override
 public void activityStopped() {
  // TODO Auto-generated method stub
 }

 @Override
 public void activityDone() {
  // TODO Auto-generated method stub

 }

}

Comments

Popular posts from this blog

How to use Preference Activity in Android

Preference Activity is a base class mainly used for setting order of preferences of an user.It is mainly used for creating setting page of an user.In this example I am just showing how to set Preferences. First create an Activity MainActivity.java Here on clicking preference example button a page will be opened to set the preferences. On clicking the show values we can see the see the values that we have set. MainActivity.java import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) findViewById(R.id.btn_pref); Button btn1 = (Button) findViewById(R.id.btn_values); btn.setOnCli...

Server communication in Android

In Android we can communicate with server using HTTP request methods for getting some details from the server.We use RESTful service.Mainly we use two types of HTTP request HTTP GET(),HTTP POST() methods. In some case we have to give some values to the server for fetching some informations, then we use commonly HTTP POST() and if we simply want to request for some datas we use HTTP GET(). Lets see HTTP POST() method. public void getResponse(String a, String b) { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("URL"); //here write the url List < NameValuePair > nameValueList = new ArrayList < NameValuePair > (2); nameValueList.add(new BasicNameValuePair("keyword", a)); nameValueList.add(new BasicNameValuePair("keyword", b)); try { httppost.setEntity(new UrlEncodedFormEntity(nameValueList)); HttpResponse response = httpclient.execute(httppost); Log.i(...

Get List of All Installed Apps

For getting the list of all apps installed in the device and to show it on a listview we can use the following code: MainActivity.java import java.util.ArrayList; import java.util.List; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends ActionBarActivity { ArrayList results_sys_app = new ArrayList(); ArrayAdapter<applicationinfo>adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); final List<applicationinfo> pkgAppsList = this.getPackageManager().getInstalledApplications( PackageManager.GET_UNINSTALLED_PAC...