Wednesday, October 5, 2011

How to call Activity's Method from Service/Adapter or any java class

Some time we need to call the Activity's method from outside the Activity like any Service/Adapter or any java class. So for that i have created this very simple example.

we need to do two steps.
  1. get the instance of Activity in Service.
  2. call Activity class method from Service
so lets try this small app..

-------------------------------------------
App Name: CallActivityMthodFromService
Package Name: com.rdc
Android SDK: Android SDK 2.3.3 / API 10
Default Activity Name: MyActivity
-------------------------------------------

 MyActivity.java
package com.rdc;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class MyActivity extends Activity {

	static MyActivity instance;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		instance = this;
		Intent myIntent = new Intent(getBaseContext(), MyService.class);
		startService(myIntent);
	}

	public void showToast() {
		Toast.makeText(getBaseContext(), "called from ervice", 1).show();

	}
}

MyService.java
package com.rdc;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {

	@Override
	public IBinder onBind(Intent i) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		Log.v("Debug", "Service has been Created..");
		// code to execute when the service is first created
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.v("Debug", "Service has been Started..");
		Toast.makeText(getBaseContext(), "Service has been Started..",
				Toast.LENGTH_SHORT).show();

		// getting the static instance of activity
		MyActivity activity = MyActivity.instance;

		if (activity != null) {
			// we are calling here activity's method
			activity.showToast();
		}

		return START_STICKY;
	}

}


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="@string/hello" />
</LinearLayout>


and Manifest file will be
<?xml version="1.0" encoding="utf-8"?>
<manifest
	xmlns:android="http://schemas.android.com/apk/res/android"
	package="com.rdc"
	android:versionCode="1"
	android:versionName="1.0">
	<uses-sdk android:minSdkVersion="10" />

	<application
		android:icon="@drawable/icon"
		android:label="@string/app_name">
		<activity
			android:name=".MyActivity"
			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:enabled="true"
			android:name=".MyService" />

	</application>
</manifest>


The output Screen will be like this..


You can download the complete source code zip file here : CallActivityMthodFromService 

 cheers!!

 I'd love to hear your thoughts!

No comments:

Post a Comment