Using a Speech Recognizer and TTS manager class - android

I'm trying to have this central manager class that handles the speech recognition and output voice data. So far, I have failed. This is what the class looks like but my app crashes when I try implementing it in other classes. Can someone please help me out?
Cheers!
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
public class speechEngineR extends Activity {
SpeechRecognizer ears;
TextToSpeech tts;
Intent i;
Context mCon = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ears = SpeechRecognizer.createSpeechRecognizer(mCon);
i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, mCon.getPackageName());
tts = new TextToSpeech(mCon, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
// TODO Auto-generated method stub
if(status != TextToSpeech.ERROR){
tts.setLanguage(Locale.US);
}
}
});
ears.setRecognitionListener(new RecognitionListener() {
#Override
public void onRmsChanged(float rmsdB) {
// TODO Auto-generated method stub
}
#Override
public void onResults(Bundle results) {
// TODO Auto-generated method stub
}
#Override
public void onReadyForSpeech(Bundle params) {
// TODO Auto-generated method stub
}
#Override
public void onPartialResults(Bundle partialResults) {
// TODO Auto-generated method stub
}
#Override
public void onEvent(int eventType, Bundle params) {
// TODO Auto-generated method stub
}
#Override
public void onError(int error) {
// TODO Auto-generated method stub
}
#Override
public void onEndOfSpeech() {
// TODO Auto-generated method stub
}
#Override
public void onBufferReceived(byte[] buffer) {
// TODO Auto-generated method stub
}
#Override
public void onBeginningOfSpeech() {
// TODO Auto-generated method stub
}
});
}
public speechEngineR(Context c){
mCon = c;
}
public void outSpeech(String out){
tts.speak(out, TextToSpeech.QUEUE_FLUSH, null);
}
}
And yes, I have I added the following permission in the Manifest.xml
<uses-permission android:name="android.permission.RECORD_AUDIO" />

Your question and code does not help.
In your code it seems you are trying to use Speech to Text(Audio recorder), and you are calling it using TTS (Text to Speech) which does exactly the opposite of what you are trying to accomplish i.e. speech to text.
Can you make it a bit more clear what you are trying to do??

Related

I am getting these errors while compiling

package com.prgguru.example;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;
public class MainActivity extends ActionBarActivity{
//DB Class to perform DB related operations
DBController controller = new DBController(this);
//Progress Dialog Object
ProgressDialog prgDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Get User records from SQLite DB
ArrayList<HashMap<String, String>> userList = controller.getAllUsers();
//
if(userList.size()!=0){
//Set the User Array list in ListView
ListAdapter adapter = new SimpleAdapter( MainActivity.this,userList, R.layout.view_user_entry, new String[] { "userId","userName"}, new int[] {R.id.userId, R.id.userName});
ListView myList=(ListView)findViewById(android.R.id.list);
myList.setAdapter(adapter);
//Display Sync status of SQLite DB
Toast.makeText(getApplicationContext(), controller.getSyncStatus(), Toast.LENGTH_LONG).show();
}
//Initialize Progress Dialog properties
prgDialog = new ProgressDialog(this);
prgDialog.setMessage("Synching SQLite Data with Remote MySQL DB. Please wait...");
prgDialog.setCancelable(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//When Sync action button is clicked
if (id == R.id.refresh) {
//Sync SQLite DB data to remote MySQL DB
syncSQLiteMySQLDB();
return true;
}
return super.onOptionsItemSelected(item);
}
//Add User method getting called on clicking (+) button
public void addUser(View view) {
Intent objIntent = new Intent(getApplicationContext(), NewUser.class);
startActivity(objIntent);
}
public void syncSQLiteMySQLDB(){
//Create AsycHttpClient object
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
ArrayList<HashMap<String, String>> userList = controller.getAllUsers();
if(userList.size()!=0){
if(controller.dbSyncCount() != 0){
prgDialog.show();
params.put("usersJSON", controller.composeJSONfromSQLite());
client.post("http://192.168.65.1/sqlitemysqlsync/insertuser.php",params ,new AsyncHttpResponseHandler() {
#Override
public void onSuccess(String response) {
System.out.println(response);
prgDialog.hide();
try {
JSONArray arr = new JSONArray(response);
System.out.println(arr.length());
for(int i=0; i<arr.length();i++){
JSONObject obj = (JSONObject)arr.get(i);
System.out.println(obj.get("id"));
System.out.println(obj.get("status"));
controller.updateSyncStatus(obj.get("id").toString(),obj.get("status").toString());
}
Toast.makeText(getApplicationContext(), "DB Sync completed!", Toast.LENGTH_LONG).show();
} catch (JSONException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
#Override
public String getCharset() {
// TODO Auto-generated method stub
return super.getCharset();
}
#Override
public Header[] getRequestHeaders() {
// TODO Auto-generated method stub
return super.getRequestHeaders();
}
#Override
public URI getRequestURI() {
// TODO Auto-generated method stub
return super.getRequestURI();
}
#Override
public boolean getUseSynchronousMode() {
// TODO Auto-generated method stub
return super.getUseSynchronousMode();
}
#Override
protected void handleMessage(Message arg0) {
// TODO Auto-generated method stub
super.handleMessage(arg0);
}
#Override
protected Message obtainMessage(int responseMessageId,
Object responseMessageData) {
// TODO Auto-generated method stub
return super.obtainMessage(responseMessageId, responseMessageData);
}
#Override
public void onCancel() {
// TODO Auto-generated method stub
super.onCancel();
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
super.onFinish();
}
#Override
public void onPostProcessResponse(
ResponseHandlerInterface instance,
HttpResponse response) {
// TODO Auto-generated method stub
super.onPostProcessResponse(instance, response);
}
#Override
public void onPreProcessResponse(
ResponseHandlerInterface instance,
HttpResponse response) {
// TODO Auto-generated method stub
super.onPreProcessResponse(instance, response);
}
#Override
public void onProgress(int bytesWritten, int totalSize) {
// TODO Auto-generated method stub
super.onProgress(bytesWritten, totalSize);
}
//#Override
public void onRetry(int retryNo) {
// TODO Auto-generated method stub
super.onRetry(retryNo);
}
#Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
#Override
protected void postRunnable(Runnable runnable) {
// TODO Auto-generated method stub
super.postRunnable(runnable);
}
#Override
protected void sendMessage(Message msg) {
// TODO Auto-generated method stub
super.sendMessage(msg);
}
#Override
public void sendResponseMessage(HttpResponse arg0)
throws IOException {
// TODO Auto-generated method stub
super.sendResponseMessage(arg0);
}
#Override
public void setCharset(String charset) {
// TODO Auto-generated method stub
super.setCharset(charset);
}
#Override
public void setRequestHeaders(Header[] requestHeaders) {
// TODO Auto-generated method stub
super.setRequestHeaders(requestHeaders);
}
#Override
public void setRequestURI(URI requestURI) {
// TODO Auto-generated method stub
super.setRequestURI(requestURI);
}
#Override
public void setUseSynchronousMode(boolean sync) {
// TODO Auto-generated method stub
super.setUseSynchronousMode(sync);
}
#Override
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
}
#Override
public boolean equals(Object o) {
// TODO Auto-generated method stub
return super.equals(o);
}
#Override
protected void finalize() throws Throwable {
// TODO Auto-generated method stub
super.finalize();
}
#Override
public int hashCode() {
// TODO Auto-generated method stub
return super.hashCode();
}
#Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
#Override
public void onFailure(int statusCode, Throwable error,
String content){
// TODO Auto-generated method stub
prgDialog.hide();
if(statusCode == 404){
Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
}else if(statusCode == 500){
Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet]", Toast.LENGTH_LONG).show();
}
}
#Override
public void onFailure(int arg0, Header[] arg1, byte[] arg2,
Throwable arg3) {
// TODO Auto-generated method stub
}
#Override
public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
// TODO Auto-generated method stub
}
});
}else{
Toast.makeText(getApplicationContext(), "SQLite and Remote MySQL DBs are in Sync!", Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(getApplicationContext(), "No data in SQLite DB, please do enter User name to perform Sync action", Toast.LENGTH_LONG).show();
}
}
}
The errors during compiling are:
Description Resource Path Location Type The method onSuccess(String)
of type new AsyncHttpResponseHandler(){} must override or implement a
supertype method
The method onFailure(int, Throwable, String) of type new
AsyncHttpResponseHandler(){} must override or implement a supertype
method
Please help!!!
IMHO, you need to define the methods onSuccess and onFailure using different signature. Check which version of the library you are using but shoudn't it be something like this?
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
// Successfully got a response
}
#Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
// Response failed :(
}
You don't respect the original class method prototypes
onSuccess:
onSuccess(int statusCode, org.apache.http.Header[] headers, byte[] responseBody)
onFailure:
onFailure(int statusCode, org.apache.http.Header[] headers, byte[] responseBody, java.lang.Throwable error)

How to keep voice recognition while device is asleep?

I was wondering, how can I keep the device listening for voice commands with voice recognition while the device is asleep? The idea I have is that I would like the device to respond to my voice, even if I have the screen locked or the screen has timed out.
Is this possible? I have tried using this as a service and an interface and it stops listening once the screen locks. Can I receive any help with this? This is my class.
public class VoiceEngineService extends Activity {
private boolean isSpeakingDone = false; // default setting
private SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(this);
private AudioManager mAudioManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.wait_for_speech);
// mute beep sound
mAudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
sr.setRecognitionListener(new listener());
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); // LANGUAGE_MODEL_WEB_SEARCH
i.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getApplication()
.getClass().getName());
i.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 6);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "");
i.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 5500);
sr.startListening(i);
}
class listener implements RecognitionListener {
#Override
public void onReadyForSpeech(Bundle params) {
// TODO Auto-generated method stub
}
#Override
public void onBeginningOfSpeech() {
// TODO Auto-generated method stub
}
#Override
public void onRmsChanged(float rmsdB) {
// TODO Auto-generated method stub
}
#Override
public void onBufferReceived(byte[] buffer) {
// TODO Auto-generated method stub
}
#Override
public void onEndOfSpeech() {
// TODO Auto-generated method stub
}
#Override
public void onError(int error) {
// TODO Auto-generated method stub
if (SharedPref.getVoiceController() == false) {
sr.cancel();
Intent i = new Intent();
sr.startListening(i);
} else {
sr.stopListening();
sr.destroy();
finish();
}
}
#Override
public void onResults(Bundle results) {
// TODO Auto-generated method stub
isSpeakingDone = true;
ArrayList<String> mDataList = results
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
Intent i = new Intent();
i.putStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS, mDataList);
setResult(RESULT_OK, i);
finish();
}
#Override
public void onPartialResults(Bundle partialResults) {
// TODO Auto-generated method stub
}
#Override
public void onEvent(int eventType, Bundle params) {
// TODO Auto-generated method stub
}
} // end listener class
#Override
protected void onPause() {
if ((isSpeakingDone == false)) {
finish();
}
super.onPause();
}
#Override
protected void onStop() {
// when speaking is true finish() has already been called
if (isSpeakingDone == false) {
finish();
}
super.onStop();
}
#Override
protected void onDestroy() {
sr.stopListening();
sr.destroy();
super.onDestroy();
}
}
You have to implement your voice listener in a service. Create a class that extends 'Service' and make up some logic to take care of recording.
If you tried already the service, then it might be that you tried to redirect commands to an activity which most likely has been stopped by Android OS. Generally when talking about doing stuff when phone is in lock mode, you only can hope to accomplish tasks in one or more services coupled togethe.
When you are in Activity, of course wen the activity goes out of scope it will be shut down by Android OS. but services can still run in background unless shut dow mm explicitly by your own code or in rare cases that Android will recognize that it needs memory and processor power for other tasks.

How to keep playing YouTubePlayer in Android

Now I'm creating an Android application witch use YouTube api to play Video.
But the player stop when I change Activity.
I want to keep Playing like a Music Player even when Activity will change.
I try to solve that by adding Static on YouTubePlayer.(But cannot solve.)
I mean I want to Keep it's playing when the Activity or the Application itself move to Background.
So if you have any ideas, please share with me :)
-----------------Code-----------------
public class PlayActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener,YouTubePlayer.PlayerStateChangeListener{
public static final String DEVELOPER_KEY = "My_Dev_Key";
private LinearLayout yPos;
private static YouTubePlayer yt;
private YouTubePlayerView ytp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
yPos = (LinearLayout)findViewById(R.id.youtube_pos);
try{
ytp = new YouTubePlayerView(this);
ytp.initialize(DEVELOPER_KEY, this);
}catch(Exception e){
finish();
}
yPos.addView(ytp);
}
#Override
public void onAdStarted() {
// TODO Auto-generated method stub
}
#Override
public void onError(ErrorReason arg0) {
// TODO Auto-generated method stub
}
#Override
public void onLoaded(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onLoading() {
// TODO Auto-generated method stub
}
#Override
public void onVideoEnded() {
// TODO Auto-generated method stub
}
#Override
public void onVideoStarted() {
// TODO Auto-generated method stub
}
#Override
public void onInitializationFailure(Provider arg0,
YouTubeInitializationResult arg1) {
// TODO Auto-generated method stub
}
#Override
public void onInitializationSuccess(Provider arg0, YouTubePlayer arg1,
boolean arg2) {
// TODO Auto-generated method stub
if(!arg2){
if(yt == null){
yt = arg1;
yt.setPlayerStateChangeListener(this);
yt.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
yt.loadVideo("u0v5A6cQOhs");
}
}
}
}

how to load activity layout in android while the video is playing in background

how to load activity layout in android while the video is playing in background in android emulator???
the video demo you can see here
sample video
till now i have only animated the layout but i am unable to do like the one in video and when i am trying to play only video in emulator it gives error saying "can't play video"
actually i don't have an android device so i have to test my application in emulator.
any help is highly appreciated.
here is a code of animation :
package com.example.test;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.VideoView;
public class CorporateProfileActivity extends Activity implements OnPreparedListener, OnCompletionListener{
public ImageView bbhome, bbmap, bbprojects, bbcontact, bbprofile;
private VideoView video;
private ImageView ivcp1, ivcp2, ivcp3, ivcp4, ivcp5, ivcp6;
Animation forcp1, forcp2, forcp3, forcp4, forcp5, forcp6, animation;
View view, view1, view2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
//below bar variables
bbhome = (ImageView) findViewById(R.id.bbhome);
bbmap = (ImageView) findViewById(R.id.bbmap);
bbprojects = (ImageView) findViewById(R.id.bbprojects);
bbcontact = (ImageView) findViewById(R.id.bbcontact);
bbprofile = (ImageView) findViewById(R.id.bbprofile);
animation = AnimationUtils.loadAnimation(this, R.anim.intro_scale);
//animation.setFillAfter(true);
video=(VideoView) findViewById(R.id.VideoView1);
//images in order from left to right
ivcp1 = (ImageView)findViewById(R.id.cp1);
ivcp2 = (ImageView)findViewById(R.id.cp2);
ivcp3 = (ImageView)findViewById(R.id.cp3);
ivcp4 = (ImageView)findViewById(R.id.cp4);
ivcp5 = (ImageView)findViewById(R.id.cp5);
ivcp6 = (ImageView)findViewById(R.id.cp6);
forcp1 = new TranslateAnimation(-1500, 0, 0, 0);
forcp1.setDuration(700);
forcp2 = new TranslateAnimation(-1200, 0, 0, 0);
forcp2.setDuration(800);
forcp2.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
ivcp1.setVisibility(1);
ivcp1.startAnimation(forcp1);
}
});
forcp3 = new TranslateAnimation(-1000, 0, 0, 0);
forcp3.setDuration(800);
forcp3.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
ivcp2.setVisibility(1);
ivcp2.startAnimation(forcp2);
}
});
forcp4 = new TranslateAnimation(-500, 0, 0, 0);
forcp4.setDuration(800);
forcp4.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
ivcp3.setVisibility(1);
ivcp3.startAnimation(forcp3);
}
});
forcp5 = new TranslateAnimation(-500, 0, 0, 0);
forcp5.setDuration(800);
forcp5.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
ivcp4.setVisibility(1);
ivcp4.startAnimation(forcp4);
}
});
forcp6 = new TranslateAnimation(-500, 0, 0, 0);
forcp6.setDuration(800);
ivcp6.setVisibility(1);
ivcp6.startAnimation(forcp6);
forcp6.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
ivcp5.setVisibility(1);
ivcp5.startAnimation(forcp5);
}
});
//image click events
ivcp1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ivcp1.startAnimation(animation);
openIntro(view1);
}
});
ivcp2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ivcp2.startAnimation(animation);
openWhoWeAre(view);
}
});
ivcp3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ivcp3.startAnimation(animation);
openThwWayWeWork(v);
}
});
ivcp4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ivcp4.startAnimation(animation);
openMisionVision(v);
}
});
ivcp5.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ivcp5.startAnimation(animation);
openOurPrinciples(v);
}
});
ivcp6.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ivcp6.startAnimation(animation);
openPromoter(v);
}
});
//onclick event for below bar menus
bbhome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
bbhome.setImageResource(R.drawable.home_active);
openMainActivity(v);
}
});
bbmap.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
bbprojects.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
bbcontact.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
bbprofile.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
//open methods to open activity
public void openIntro(View view) {
// Do something in response to button
Intent intent = new Intent(this, IntroductionActivity.class);
startActivity(intent);
}
public void openWhoWeAre(View view) {
// Do something in response to button
Intent intent = new Intent(this, WhoWeAreActivity.class);
startActivity(intent);
}
public void openThwWayWeWork(View view) {
// Do something in response to button
Intent intent = new Intent(this, WhoWeAreActivity.class);
startActivity(intent);
}
public void openMisionVision(View view) {
// Do something in response to button
Intent intent = new Intent(this, WhoWeAreActivity.class);
startActivity(intent);
}
public void openOurPrinciples(View view) {
// Do something in response to button
Intent intent = new Intent(this, WhoWeAreActivity.class);
startActivity(intent);
}
public void openPromoter(View view) {
// Do something in response to button
Intent intent = new Intent(this, WhoWeAreActivity.class);
startActivity(intent);
}
/*
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
*/
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
System.out.println("video is complete...");
//imageView.setVisibility(View.VISIBLE);
//imageView.setScaleType(ImageView.ScaleType.FIT_XY);
}
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
}
public void openMainActivity(View view) {
// Do something in response to button
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}

how to send data in service based android app

I want to send data from one application A to other application B using AIDL file. My appl A is like below,
public class LibValue {
public static native int intFromJNI(int n);
static {
System.loadLibrary("hello");
System.out.println("LibValue : Loading library");
}
I am getting value from JNI file to above class. The data from above class to send another app B using AIDL service is like below.
IEventService.aidl
interface IEventService {
int intFromJNI(in int n);
}
for this I written IEventImpl.java class
public class IEventImpl extends IEventService.Stub{
int result;
#Override
public int intFromJNI(int n) throws RemoteException {
// TODO Auto-generated method stub
System.out.println("IEventImpl"+LibValue.intFromJNI(n));
return LibValue.intFromJNI(n);
}
}
To access above class I writtn service class like below
public class EventService extends Service {
public IEventImpl iservice;
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
Log.i("EventService", "indside onBind");
return this.iservice;
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
this.iservice = new IEventImpl();
Log.i("EventService", "indside OncREATE");
}
#Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
return super.onUnbind(intent);
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
this.iservice = null;
super.onDestroy();
}
All above classes are server side. The below class is Client(app) class to access data.
public class EventClient extends Activity implements OnClickListener, ServiceConnection{
public IEventService myService;
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_client);
button = (Button)findViewById(R.id.button1);
this.button.setOnClickListener(this);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (!super.bindService(new Intent(IEventService.class.getName()),
this, BIND_AUTO_CREATE)) {
Log.w("EventClient", "Failed to bind to service");
System.out.println("inside on resume");
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
super.unbindService(this);
}
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
this.myService = IEventService.Stub.asInterface(service);
Log.i("EventClient", "ServiceConnected");
}
#Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
Log.d("EventClient", "onServiceDisconnected()'ed to " + name);
// our IFibonacciService service is no longer connected
this.myService = null;
}
I am trying to access data from service class but not able to find out the way. can anybody tell how to access data from service to client application ?
Thanks

Categories

Resources