Hello I'm currently using Radius Network Beacon SDK but unable to detect my beacons.
In didRangeBeaconsInRegion(Collection beacons, Region region) collection object size is 0
Please help me!!
RangingActivity Code-
import java.util.Collection;
import android.app.Activity;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.widget.EditText;
import org.altbeacon.beacon.AltBeacon;
import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;
public class RangingActivity extends Activity implements BeaconConsumer {
protected static final String TAG = "RangingActivity";
private BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ranging);
beaconManager.bind(this);
beaconManager.debug = true;
}
#Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
}
#Override
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
#Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
EditText editText = (EditText)RangingActivity.this
.findViewById(R.id.rangingText);
Beacon firstBeacon = beacons.iterator().next();
logToDisplay("The first beacon "+firstBeacon.toString()+" is about "+firstBeacon.getDistance()+" meters away.");
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
//beaconManager.updateScanPeriods();
} catch (RemoteException e) { }
}
private void logToDisplay(final String line) {
runOnUiThread(new Runnable() {
public void run() {
EditText editText = (EditText)RangingActivity.this
.findViewById(R.id.rangingText);
editText.append(line+"\n");
}
});
}
}
You need a custom Parser to properly recognize that beacons.
Take a look at this answer:
Is this the correct layout to detect iBeacons with AltBeacon's Android Beacon Library?
You need to call the onBeaconConnect() in your onCreate function
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ranging);
beaconManager.bind(this);
beaconManager.debug = true;
onBeaconConnect()
}
Related
I tried to calculate my ibeacon distance using android studio , i followed a video tutorial in http://www.software7.com/blog/creating-a-beacon-app-for-android-in-less-than-10-minutes-from-scratch/ , i did exactly the same as he did , however he didn't use android studio instead IntelliJ IDEA . The result should be appear at android monitor - logcat , but mine didn't
import android.os.RemoteException;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.Identifier;
import org.altbeacon.beacon.MonitorNotifier;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;
import java.util.Collection;
public class MainActivity extends AppCompatActivity implements
BeaconConsumer {
public static final String TAG = "BeaconsEverywhere";
private BeaconManager beaconManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser()
.setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-
24,d:25-25"));
beaconManager.bind(this);
}
#Override
public void onBeaconServiceConnect() {
final Region region = new Region("myBeaons", Identifier.parse("2173E519-
9155-4862-AB64-7953AB146156"),null,null);
beaconManager.addMonitorNotifier(new MonitorNotifier() {
#Override
public void didEnterRegion(Region region) {
try {
Log.d(TAG,"didEnterRegion");
beaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
#Override
public void didExitRegion(Region region) {
try {
Log.d(TAG,"didExitRegion");
beaconManager.stopRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
#Override
public void didDetermineStateForRegion(int i, Region region) {
}
});
beaconManager.addRangeNotifier(new RangeNotifier() {
#Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons,
Region region) {
for(Beacon oneBeacon : beacons){
Log.d(TAG, "distance: " + oneBeacon.getDistance() + " id:" +
oneBeacon.getId1() + "/" + oneBeacon.getId2() + "/" +
oneBeacon.getId3());
}
}
});
try {
beaconManager.startMonitoringBeaconsInRegion(region);
}catch (RemoteException e){
e.printStackTrace();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
}
The first Image is my expectation , second is my result
would you guys help me to figure out what the problem is ?
I would add new debug lines like Log.d(TAG,"onCreate"); and Log.d(TAG,"onBeaconServiceConnect"); in each of those methods so you can tell what is going on, then look for those lines when you run your app. This will tell you how far your program is getting in the process.
I also suspect that you will need to use a different beacon parser expression than is listed here:
beaconManager.getBeaconParsers().add(new BeaconParser()
.setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-
24,d:25-25"));
If you are using an off-the-shelf iBeacon, you will need to replace that string with the iBeacon layout as can be found here:
https://beaconlayout.wordpress.com/
Don't worry about Android Studio vs. IntelliJ IDEA. The code shown should work fine using either IDE.
I want to upload the result from the barcode scanner to the edit text, but it is not displaying in the edit text. May I know what the problem is and how do I solve it?
This is my code
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class ScannerActivity extends AppCompatActivity {
private ZXingScannerView scannerView;
private EditText ScanBarcode;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
ScanBarcode = (EditText)findViewById(R.id.editscanbar);
}
public void scanCode(View view){
scannerView = new ZXingScannerView(this);
scannerView.setResultHandler(new ZXingScannerResultHandler());
setContentView(scannerView);
scannerView.startCamera();
}
#Override
public void onPause(){
super.onPause();
scannerView.stopCamera();
}
class ZXingScannerResultHandler implements ZXingScannerView.ResultHandler{
#Override
public void handleResult(Result result){
String resultCode = result.getText().toString();
ScanBarcode.setText(resultCode);
Toast.makeText(ScannerActivity.this, resultCode,
Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_scanner);
scannerView.stopCamera();
}
}
}
I've changed it and there is still nothing showing up in the edit text
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class ScannerActivity extends AppCompatActivity implements
ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
private EditText ScanBarcode;
#Override
public void onCreate(Bundle State) {
super.onCreate(State);
mScannerView = new ZXingScannerView(this);
setContentView(R.layout.activity_scanner);
ScanBarcode = (EditText)findViewById(R.id.editscanbar);
}
public void scanCode(View view){
mScannerView.setResultHandler(this);
setContentView(mScannerView);
mScannerView.startCamera();
}
#Override
public void onResume(){
super.onResume();
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
#Override
public void onPause(){
super.onPause();
mScannerView.stopCamera();
}
#Override
public void handleResult(Result rawResult){
ScanBarcode.setText(rawResult.getText());
setContentView(R.layout.activity_scanner);
mScannerView.stopCamera();
}
}
you should implement the ResultHandler in your Activity, override the callback method and there set the result of the scan in your EditText, example:
public class SimpleScannerActivity extends Activity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
private EditText editText;
#Override
public void onCreate(Bundle state) {
super.onCreate(state);
mScannerView = new ZXingScannerView(this); // Programmatically initialize the scanner view
setContentView(mScannerView); // Set the scanner view as the content view
EditText editText = (EditText)findViewById(R.id.myedittext);
}
#Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on resume
}
#Override
public void onPause() {
super.onPause();
mScannerView.stopCamera(); // Stop camera on pause
}
#Override
public void handleResult(Result rawResult) {
// Do something with the result here
editText.setText(rawResult.getText());
Log.v(TAG, rawResult.getText()); // Prints scan results
Log.v(TAG, rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.)
// If you would like to resume scanning, call this method below:
mScannerView.resumeCameraPreview(this);
}
}
Hope it helps!
I'm using the Altbeacon library (stable release 2.1.4) to detect beacons. If I do it in an Activity, I have no problems detecting them. However I can't get this to work from a service. Here's what I've got:
package com.ibeacontest.android;
import java.util.Collection;
import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.Identifier;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class TestBestzBeaconService extends Service implements BeaconConsumer
{
private BeaconManager beaconManager;
private final String BEACON_UUID = "11687109-915f-4136-a1f8-e60ff514f96d";
private final int BEACON_MAJOR = 3;
#Override
public void onCreate() {
super.onCreate();
L.p("In TestBestzBeaconService onCreate()");
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand (Intent intent, int flags, int startId) {
L.p("in TestBestzBeaconService onStartCommand()");
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.bind(this);
//iBeacons ?
BeaconParser bp0 = new BeaconParser();
bp0.setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24");
beaconManager.getBeaconParsers().add(bp0);
//Bluecats?
BeaconParser bp1 = new BeaconParser();
bp1.setBeaconLayout("m:2-3=0201,i:28-29,p:24-24");
beaconManager.getBeaconParsers().add(bp1);
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onDestroy() {
L.p("In TestBestzBeaconService onDestroy()");
beaconManager.unbind(this);
}
#Override
public void onBeaconServiceConnect() {
L.p("In TestBestzBeaconService onBeaconServiceConnect()");
beaconManager.setRangeNotifier(new RangeNotifier() {
#Override
public void didRangeBeaconsInRegion(Collection<Beacon> arg0, Region arg1) {
L.p("In TestBestzBeaconService - anonymous didRangeBeaconsInRegion()");
}
});
Region region = new Region("myregion", Identifier.parse(BEACON_UUID), Identifier.fromInt(BEACON_MAJOR), null); //
try {
beaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
L.p("In TestBestzBeaconService onBeaconServiceConnect(), REMOTEEXCEPTION!");
}
}
private static class L
{
public static void p(String s) {
Log.i("beacon", s);
}
}
}
I'm calling this from an Activity as so:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(this, TestBestzBeaconService.class));
}
}
The log output I'm getting is:
03-19 09:56:40.233: I/beacon(25210): In TestBestzBeaconService onCreate()
03-19 09:56:40.233: I/beacon(25210): in TestBestzBeaconService onStartCommand()
03-19 09:56:40.566: I/beacon(25210): In TestBestzBeaconService onBeaconServiceConnect()
Parts added to the AndroidManifest:
<!-- Needed for AltBeacon SDK -->
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
and inside the Application tag:
<!-- Needed for AltBeacon SDK -->
<service android:name="org.altbeacon.beacon.service.BeaconService"/>
<service android:name="com.ibeacontest.android.TestBestzBeaconService" />
...but no signs of beacons or the didRangeBeaconsInRegion log output. Any pointers?
As i know Monitoring only works in background while Ranging do not. Try firstly starting a Monitoring and then immediately when you enter Monitoring start Ranging.
Something like:
//Set Monitoring
mBeaconManager.setMonitorNotifier(new
MonitorNotifier() {
#Override
public void didEnterRegion (Region region){
Log.d("TEST", "ENTERED beacon region");
//Start Raning as soon as you detect a beacon
try {
mBeaconManager.startRangingBeaconsInRegion(mRegion);
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
//Set Ranging
mBeaconManager.setRangeNotifier(new
RangeNotifier() {
#Override
public void didRangeBeaconsInRegion ( final Collection<Beacon> beacons, Region region){
if (beacons.size() > 0) {
Log.i(TAG, p("In TestBestzBeaconService - anonymous
}
}
});
try
{
//Start Monitoring
mBeaconManager.startMonitoringBeaconsInRegion(mRegion);
}
catch(RemoteException e)
{
e.printStackTrace();
}
Don't forget to add beacon service to Manifest:
<service
android:name="org.altbeacon.beacon.service.BeaconService"
android:enabled="true"
android:exported="true"
android:isolatedProcess="false"
android:label="beacon"></service>
<service
android:name="org.altbeacon.beacon.BeaconIntentProcessor"
android:enabled="true"></service>
note: In meantime check also lib project
The posted code is in a library project and contained the correct inserts in the AndroidManifest file, however the actual app project was missing this.
I believe normally this would cause the app to crash, but in this case it just wouldn't show beacons.
Adding this made it work.
Is it possible to integrate video ads from Flurry in an android app? I tried some things, but it didn't work. The Flurry Android SDK has the onVideoCompleted function, and in the Android Flurry SDK Release Notes, the following can be found: verified support for clips in AdUnity (http://support.flurry.com/index.php?title=Analytics/Code/ReleaseNotes/Android).
I tried this code, but it didn't work for me:
package com.test.flurrytest;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import com.flurry.android.FlurryAdType;
import com.flurry.android.FlurryAds;
import com.flurry.android.FlurryAdSize;
import com.flurry.android.FlurryAgent;
import com.flurry.android.FlurryAdListener;
public class MainActivity extends Activity implements AdCallbackListener, FlurryAdListener {
FrameLayout mBanner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBanner = new FrameLayout(this);
FlurryAds.setAdListener(this);
FlurryAds.enableTestAds(true);
Button watchvideo = (Button) findViewById(R.id.watchvideo);
watchvideo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
FlurryAds.fetchAd(MainActivity.this, "TestAdspace", mBanner, FlurryAdSize.FULLSCREEN);
FlurryAds.displayAd(MainActivity.this, "TestAdspace", mBanner);
}
});
}
// Flurry
#Override
public void onStart() {
super.onStart();
FlurryAgent.onStartSession(this, "****");
}
public void spaceDidReceiveAd(String adSpace) {
FlurryAds.displayAd(this, adSpace, mBanner);
}
public void onVideoCompleted(String adSpace) {
// The user get some points now
}
public boolean shouldDisplayAd(String adSpaceName, FlurryAdType type) {
return true;
}
public void onAdClosed(String adSpaceName) {
}
public void onRenderFailed(String adSpaceName) {
Toast.makeText(getApplicationContext(), "The video has failed to render. Try again.", Toast.LENGTH_LONG).show();
}
public void onApplicationExit(String adSpaceName) {
}
public void spaceDidFailToReceiveAd(String adSpaceName) {
Toast.makeText(getApplicationContext(), "Failed to receive ad. Try again.", Toast.LENGTH_LONG).show();
}
public void onAdClicked(String adSpaceName) {
}
public void onAdOpened(String adSpaceName) {
}
#Override
public void onStop() {
super.onStop();
FlurryAds.removeAd(this, "TestAdspace", mBanner);
FlurryAgent.onEndSession(this);
}
}
Is it possible with Android, and if so, how to integrate it?
Thanks!
I am trying to get the Android TTS API to read my "utterance" and then call the onUtteranceCompleted() listener unsuccessfully. I've registered my TTS object and it returns SUCCESS, so I can't figure out for the life of me why my callback isn't getting called.
I've tried searching for help, but it seems others have difficulty with this too. Am I missing something simple?
Thanks for any help you can offer.
package com.test.mytts;
import java.util.HashMap;
import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import android.widget.TextView;
import android.widget.Toast;
public class MyTTS extends Activity implements OnInitListener, OnUtteranceCompletedListener
{
TextView tv;
private TextToSpeech _tts;
#Override
public void onCreate(Bundle savedInstanceState)
{
tv = new TextView(this);
tv.setText("MyTTS: ");
super.onCreate(savedInstanceState);
setContentView(tv);
_tts = new TextToSpeech(this, this);
}
#Override
public void onInit(int status)
{
HashMap<String, String> myHashAlarm = new HashMap<String, String>();
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "test");
if (status == TextToSpeech.SUCCESS)
{
Toast.makeText(this, "Trying to speak...", Toast.LENGTH_SHORT).show();
int result = _tts.setOnUtteranceCompletedListener(this);
tv.append(String.valueOf(result));
_tts.setSpeechRate((float) .5);
_tts.speak("Testing one, two, three", TextToSpeech.QUEUE_ADD, myHashAlarm);
}
else
Toast.makeText(this, "Failed to initialize TTS.", Toast.LENGTH_SHORT).show();
}
#Override
public void onUtteranceCompleted(String utteranceId)
{
Toast.makeText(this, "onUtteranceCompleted", Toast.LENGTH_SHORT).show();
}
#Override
public void onDestroy()
{
super.onDestroy();
_tts.shutdown();
}
}
Call the setOnUtteranceCompletedListener inside the onInit function of the tts object.
If you want to make any changes to the UI on the call of the onUtteranceCompleted function, add the code inside a runOnUIThread method.
And do remember to add the Hashmap param value while calling the speak() function
Example :
TextToSpeech tts= new TextToSpeech(context, new OnInitListener() {
#Override
public void onInit(int status) {
mTts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {
#Override
public void onUtteranceCompleted(String utteranceId) {
runOnUiThread(new Runnable() {
#Override
public void run() {
//UI changes
}
});
}
});
}
});
HashMap<String, String> params = new HashMap<String, String>();
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"stringId");
tts.speak("Text to Speak",TextToSpeech.QUEUE_FLUSH, params);
I believe that unless you specify an utterance with an id, like:
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceid);
your utterance completed method will not be called.
in this case, map is the Hashmap you pass to the engine when you speak.
this will work for you on API Level >=15
import java.util.HashMap;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.UtteranceProgressListener;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnInitListener{
private static final int CHECK_TTS_DATA = 0X123;
protected static final String TAG = MainActivity.class.getSimpleName();
private TextToSpeech textToSpeech;
private Button buttonSayIt;
private EditText editTextTts;
String tts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonSayIt=(Button) findViewById(R.id.buttonSayIt);
editTextTts=(EditText) findViewById(R.id.editTextTts);
buttonSayIt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tts=editTextTts.getText().toString();
Log.d(TAG, tts);
speach(tts,"you_utterance_id");
}
});
//check for TTs data
Intent checkTtsDataIntent=new Intent();
checkTtsDataIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTtsDataIntent, CHECK_TTS_DATA);
}
protected void speach(String tts,String utteranceId) {
HashMap<String, String> params = new HashMap<String, String>();
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,utteranceId);
textToSpeech.speak(tts,TextToSpeech.QUEUE_FLUSH,params);
}
#Override
public void onInit(int status) {
if(status==TextToSpeech.SUCCESS){
if(textToSpeech.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE){
textToSpeech.setLanguage(Locale.US);
}
}else if(status==TextToSpeech.ERROR){
Toast.makeText(this, "Sorry Text To Speach faild", Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==CHECK_TTS_DATA){
if(resultCode==TextToSpeech.Engine.CHECK_VOICE_DATA_PASS){
textToSpeech=new TextToSpeech(this, this);
textToSpeech.setOnUtteranceProgressListener(utteranceProgressListener);
}else{
Intent installTtsIntent=new Intent();
installTtsIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTtsIntent);
}
}
}
UtteranceProgressListener utteranceProgressListener=new UtteranceProgressListener() {
#Override
public void onStart(String utteranceId) {
Log.d(TAG, "onStart ( utteranceId :"+utteranceId+" ) ");
}
#Override
public void onError(String utteranceId) {
Log.d(TAG, "onError ( utteranceId :"+utteranceId+" ) ");
}
#Override
public void onDone(String utteranceId) {
Log.d(TAG, "onDone ( utteranceId :"+utteranceId+" ) ");
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
in case anybody is still finding it difficult
Code Snippet
textToSpeech=new TextToSpeech(this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status==TextToSpeech.SUCCESS){
int result=textToSpeech.setLanguage(Locale.ENGLISH);
if (result==TextToSpeech.LANG_MISSING_DATA||result==TextToSpeech.LANG_NOT_SUPPORTED){
Log.i("TextToSpeech","Language Not Supported");
}
textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
#Override
public void onStart(String utteranceId) {
Log.i("TextToSpeech","On Start");
}
#Override
public void onDone(String utteranceId) {
Log.i("TextToSpeech","On Done");
}
#Override
public void onError(String utteranceId) {
Log.i("TextToSpeech","On Error");
}
});
}else {
Log.i("TextToSpeech","Initialization Failed");
}
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null,TextToSpeech.ACTION_TTS_QUEUE_PROCESSING_COMPLETED);
}