Text To Speech not working in service - android

Following are the classes that i'm trying to implement, but i dont know where should i speak the name that i got from brodcast receiver.Can anyoneone help.
SERVICE CLASS
public class SMSTalk extends Service implements OnInitListener, OnUtteranceCompletedListener {
public static TextToSpeech mTts;
private String spokenText;
public String msg=null;
int flag=0;
#Override
public void onCreate() {
mTts = new TextToSpeech(this, this);
// This is a good place to set spokenText
}
public void readName(String temp)
{
msg=temp;
System.out.println("HHHHHHHHHHHHHHHHHHH"+msg);
// mTts.speak(msg, 0, null);
}
#Override
public void onInit(int status) {
SMSReceiver smsReceiver=new SMSReceiver();
if (status == TextToSpeech.SUCCESS) {
int result = mTts.setLanguage(Locale.UK);
if (result != TextToSpeech.LANG_MISSING_DATA && result != TextToSpeech.LANG_NOT_SUPPORTED) {
System.out.println("####"+msg);
Toast.makeText(getApplicationContext(), "SUCCESS",Toast.LENGTH_LONG ).show();
mTts.speak("Hello", 0, null);
flag=1;
}
}
if(flag==1)
{
System.out.println("######"+msg);
mTts.speak(msg, 0, null);
}
}
#Override
public void onUtteranceCompleted(String uttId) {
stopSelf();
System.out.println("onUtteranceCompleted"+msg);
}
#Override
public void onDestroy() {
if (mTts != null) {
mTts.stop();
mTts.shutdown();
}
super.onDestroy();
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
RECEIVER CLASS
public class SMSReceiver extends BroadcastReceiver
{
String name=null;
private Context mContext;
#Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
int n;
Bundle bundle = intent.getExtras();
Object pdus[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[pdus.length];
for (n = 0; n < pdus.length; n++)
{
smsMessage[n] = SmsMessage.createFromPdu((byte[]) pdus[n]);
}
// show first message
String sms1 = smsMessage[0].getMessageBody();
String from = smsMessage[0].getOriginatingAddress();
//String name = getDisplayNameFromPhoneNo( from);
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(from));
Cursor c = context.getContentResolver().query(lookupUri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
while(c.moveToNext()){
/* If we find a match we put it in a String.*/
name = c.getString(c.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
}
Toast toast = Toast.makeText(context, "SMS Received from: " + from, Toast.LENGTH_LONG);
toast.show();
System.out.println("!!!!"+name);
Toast.makeText(context, "name: " + name, Toast.LENGTH_LONG).show();
//smsTalk.speakSMS(name);
//SMSTalk.mTts.speak("You have an SMS from "+name, 0, null);
context.startService(new Intent(context,SMSTalk.class));
SMSTalk smsTalk = new SMSTalk();
smsTalk.readName(name);
}
}

Your answer is probably there :
http://developer.android.com/guide/topics/fundamentals/services.html
Citations from the webpage :
Caution:
A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application's main thread can remain dedicated to user interaction with your activities.
Should you use a service or a thread?
A service is simply a component that can run in the background even when the user is not interacting with your application. Thus, you should create a service only if that is what you need.
If you need to perform work outside your main thread, but only while the user is interacting with your application, then you should probably instead create a new thread and not a service. For example, if you want to play some music, but only while your activity is running, you might create a thread in onCreate(), start running it in onStart(), then stop it in onStop(). Also consider using AsyncTask or HandlerThread, instead of the traditional Thread class. See the Processes and Threading document for more information about threads.
Remember that if you do use a service, it still runs in your application's main thread by default, so you should still create a new thread within the service if it performs intensive or blocking operations.

There is a statement in service class "mTts.speak(msg, 0, null);" .It is giving null pointer exception, but here :
public void readName(String temp)
{
msg=temp;
System.out.println("HHHHHHHHHHHHHHHHHHH"+msg);
// mTts.speak(msg, 0, null);
}
it displays the value that i want.So the problem is with placing of "mTts.speak(msg, 0, null);".
PS:I have taken care of threading thing.

Related

How to communicate with HostApduService from an Activity

I have asked this question here but it was marked as duplicate -
however I didn't find any solution helpful mentioned in comments.
Here, I am asking again with more details ...
I am doing a sample app (PoC) on HCE and using HostApduService as per Android user guide. I have created two apps
1) ReaderApp - acting as card reader
2) HCEApp - emulating a card
In HCEApp, I have created a class 'MyService' extending HostApduService
public class MyService extends HostApduService {
private int messageCounter;
private final String TAG = "MyService";
Intent mIntent;
#Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate");
mIntent = new Intent(this, MyActivity.class);
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mIntent);
}
/**
* returned bytes will be sent as response. This method runs in Main thread
* so return ASAP.
*/
#Override
public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
if (selectAidApdu(apdu)) {
Log.i(TAG, "Application selected");
return getWelcomeMessage();
} else {
Log.i(TAG, "Received: " + new String(apdu));
return getNextMessage();
}
}
private byte[] getWelcomeMessage() {
return "Hello Desktop!".getBytes();
}
private byte[] getNextMessage() {
return ("Message from android: " + messageCounter++).getBytes();
}
private boolean selectAidApdu(byte[] apdu) {
if (apdu != null) {
for (byte b : apdu) {
System.out.printf("0x%02X", b);
}
}
return apdu.length >= 2 && apdu[0] == (byte) 0
&& apdu[1] == (byte) 0xa4;
}
#Override
public void onDeactivated(int reason) {
Log.i(TAG, "Deactivated: " + reason);
}
#Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
}
As you can see in onCreate(), I am launching MyActivity provides user to enter some information and needs to be sent back to MyService.
I think I can not use binding as 'onBind()' is declared final in HostApduService as below
#Override
public final IBinder onBind(Intent intent) {
return mMessenger.getBinder();
}
Please let me know if I am understading it correctly. Appreciate any help.
Thanks
iuq
Whether you can use onBind or not I do not know, but I recently worked with a BroadcastReceiver from which I had to start a Service. You cannot bind a Service from a BroadcastReceiver according to docs, you can only start it. I needed to send some data to the Service from my BroadcastReceiver at some later point, and since the binder techniques was not available to me, I had to find a different way to communicate with the Service, much like your case where you don't have a reference to it.
I did some research but could not find any solution, but then I remembered that you can pass intent data with the startService(intent) call. I start my Service work in onCreate instead, as onCreate is only called once when the Service is created.
In your Activity
public void sendDataToService(){
Intent intent = new Intent(context, MyService.class);
intent.putExtra("message", SOME_DATA);
context.startService(intent);
}
In your Service
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Check if intent has extras
if(intent.getExtras() != null){
// Get message
int message = intent.getExtras().getInt("message");
}
return START_NOT_STICKY;
}
This may be some sort what of a hack since "startService" does not sound like it should be used to send messages, and am not sure if this is exactly what you need, but it worked for me, so I hope it works for you. Cheers
Edit: BTW. I use it to tell a LocationService that a particular activity no longer want location updates.
I ended up taking a different approach to solving this same problem. When I bind to my HostApduService subclass, I grab a handle to the Messenger interface returned by the HostApduService onBind implementation.
Here's some sample code. This would all go in your activity implementation (calling it MyActivity here, communicating with MyHostApduServiceSubclass). Here's what MyActivity would need to include:
private Messenger mAPDUMessenger;
...
#Override
protected void onStart() {
super.onStart();
Context context = getApplicationContext();
Intent apduIntent = new Intent(montext, ContactlessApduService.class);
context.bindService(apduIntent, mAPDUConnection, Context.BIND_AUTO_CREATE);
}
...
private ServiceConnection mAPDUConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className, IBinder service) {
// The HostApduService has a final override on the onBind() service method that returns
// an IMessageHandler interface that we can grab and use to send messages back to the
// terminal - would be better to get a handle to the running instance of the service so
// that we could make use of the HostApduService#sendResponseApdu public method
mAPDUMessenger = new Messenger(service);
registerAPDUMessengerIntentFilters();
// ^ This method sets up my handlers for local broadcast messages my BroadcastReceiver processes.
}
...
}
...
private void registerAPDUMessengerIntentFilters() {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(MyActivity.this);
IntentFilter intentFilter = new IntentFilter(MyHostApduServiceSubclass.ACTION_PPSE_APDU_SELECT);
lbm.registerReceiver(apduMessageBroadcastReceiver, intentFilter);
}
...
BroadcastReceiver apduMessageBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(MyHostApduServiceSubclass.ACTION_PPSE_APDU_SELECT)) {
sendResponseApdu(MyActivity.PPSE_APDU_SELECT_RESPONSE_BYTES);
}
}
};
...
public final void sendResponseApdu(byte[] responseApdu) {
Message responseMsg = Message.obtain(null, MyHostApduServiceSubclass.MSG_RESPONSE_APDU);
// ^ Note here that because MSG_RESPONSE_APDU is the message type
// defined in the abstract HostApduService class, I had to override
// the definition in my subclass to expose it for use from MyActivity.
// Same with the KEY_DATA constant value below.
Bundle dataBundle = new Bundle();
dataBundle.putByteArray(MyHostApduServiceSubclass.KEY_DATA, responseApdu);
responseMsg.setData(dataBundle);
try {
mAPDUMessenger.send(responseMsg);
} catch (RemoteException e) {
// Do something with the failed message
}
}
And then your HostApduService subclass would just need to send a broadcast to your activity indicating what APDU command was received. Here is what would need to be included in MyHostApduServiceSubclass:
public static final String ACTION_PPSE_APDU_SELECT = "ACTION_PPSE_APDU_SELECT";
// Abstract super class constant overrides
public static final String KEY_DATA = "data";
public static final int MSG_RESPONSE_APDU = 1;
#Override
public byte[] processCommandApdu(byte[] commandApdu, Bundle extras) {
Context context = getApplicationContext();
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
if (Arrays.equals(MyHostApduServiceSubclass.PPSE_APDU_SELECT_BYTES, commandApdu)) {
lbm.sendBroadcast(new Intent(ACTION_PPSE_APDU_SELECT));
}
return null;
// ^ Note the need to return null so that the other end waits for the
// activity to send the response via the Messenger handle
}

Android, prevent to kill service/thread

how can i prevent this service with thread to dont be killed from android, i need this notifications always runnig, but when is mobile locked, nothing will happen. I think android kill service or thread or something like that
MainActivity in onCreate
startService(new Intent(this, NotifyService.class));
My service
public class NotifyService extends Service {
private DatabaseOp mDbHelper;
public Vibrator vibrator;
String username;
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate ()
{
mDbHelper = new DatabaseOp(this);
final boolean cyklus = true;
Thread vlakno = new Thread (new Runnable()
{
#Override
public void run()
{
while (cyklus)
{
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String sysDate = getSysDate();
String sysDate2 = getSysDate2();
String time = getSysTime();
mDbHelper.open();
Log.v( "sysDate", sysDate );
Cursor cursorU = mDbHelper.fetchUlohaS(0, sysDate);
if (cursorU.getCount() > 0)
{
String idU = cursorU.getString(cursorU.getColumnIndexOrThrow(DatabaseOp.KEY_ID));
String dbDateU = cursorU.getString(cursorU.getColumnIndexOrThrow(DatabaseOp.KEY_DATE));
String menoU = cursorU.getString(cursorU.getColumnIndexOrThrow(DatabaseOp.KEY_NAZOV));
String mHodina = getResources().getString(R.string.cas)+" "+cursorU.getString(cursorU.getColumnIndexOrThrow(DatabaseOp.KEY_HODINA));
Log.v( "task", dbDateU+"/"+sysDate );
if (dbDateU.equals(sysDate))
{
Notify(menoU, mHodina, idU, 0);
}
}
Cursor cursorS = mDbHelper.fetchSviatokS(3, sysDate2);
if (cursorS.getCount() > 0)
{
String idS = cursorS.getString(cursorS.getColumnIndexOrThrow(DatabaseOp.KEY_ID));
String dbDateS = cursorS.getString(cursorS.getColumnIndexOrThrow(DatabaseOp.KEY_DATUM));
String menoS = cursorS.getString(cursorS.getColumnIndexOrThrow(DatabaseOp.KEY_NAZOV));
if (dbDateS.equals(sysDate2) && time.equals("09:00"))
{
Notify(menoS,getResources().getString(R.string.title_section4), idS, 3);
}
}
mDbHelper.close();
}
}
});
vlakno.start();
}
}
Have you tried to use ForgroundService?
Checkout this repo for an example - https://github.com/supercurio/foreground-service-sample-app
I think you should consider AlarmManager. See http://developer.android.com/reference/android/app/AlarmManager.html.
To tip the system to keep your Service alive as long as possible (i.e. before RAM is very short or user kills the service by hand through application info screen), you need to run it as a foreground service -- by using startForeground() method.
If you're looking for a way to run when the device is turned off, read the Keeping The Device Awake training page and consider using AlarmManager instead as suggested by #khris if your task is not very critical in terms of timing precision.

Launching an intent without context

What is the best way to deal with the following situation:
I have a IntentService which does synchronisation with the server (this is triggered by either an Activity coming to the foreground, or a GCM message, so onoy occasional). Sometimes there is a user action needed as a result, and the given command/request is part of the response XML.
There are basically two options, it is either a yes/no question, or a full Activity to for example select the desired language.
How can I do this, or what would be the best way? If I try to launch the Activity with the context of the IntentService nothing happens. I could write a abstract Activity, which I extends in all my Activities and sent a broadcast message which those receive and subsequent start the Activity form the activity which is active, but don't know if that is the best way to do it in Android.
Any suggestions would be appreciated!
[EDIT: as suggested some code]
public class SyncService extends IntentService{
public SyncService(){
super("SyncService");
}
#Override
protected void onHandleIntent(Intent intent) {
iDomsAndroidApp app = ((iDomsAndroidApp) getApplicationContext());
DataManager manager = app.getDataManager();
manager.updateData(this);
}
}
public class DataManager {
// For brevity, this is called with the DOM.Document with the actions to be preformed
private void checkForActions(Document doc, SyncUpdateInterface syncInterface){
NodeList objects = null;
NodeList rootNodes = doc.getElementsByTagName("actions");
for (int j = 0; j < rootNodes.getLength(); j++) {
Element rootElement = (Element) rootNodes.item(j);
if (!rootElement.getParentNode().getNodeName().equals("iDoms")) {
continue;
}
objects = ((Element) rootNodes.item(j)).getElementsByTagName("action");
break;
}
if(objects == null || objects.getLength() == 0){
Log.d(iDomsAndroidApp.TAG, "No actions");
return;
}
for (int j = 0; j < objects.getLength(); j++) {
Element element = (Element) objects.item(j);
String action = ((Element) element.getElementsByTagName("command").item(0)).getTextContent();
if(action == null) return;
Log.d(iDomsAndroidApp.TAG, "Action: " + action);
try{
if(action.equalsIgnoreCase("selectLanguage")){
if(syncInterface == null || syncInterface.getContext() == null) throw new Exception("No context, so cannot perform action");
iDomsAndroidApp app = ((iDomsAndroidApp) iDomsAndroidApp.getAppContext());
// The app.actionIntent is just a central function to pick the right intent for an action.
syncInterface.getContext().startActivity(app.actionIntent("settings", iDomsAndroidApp.context));
} else if (action.equalsIgnoreCase("markAllAsRead")) {
if(syncInterface == null | syncInterface.getContext() == null) throw new Exception("No context, so cannot perform action");
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(syncInterface.getContext());
alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// User clicked OK, so save the result somewhere
// or return them to the component that opened the dialog
iDomsAndroidApp app = ((iDomsAndroidApp) iDomsAndroidApp.getAppContext());
app.getDataManager().markAllAsRead(null);
}
});
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
});
alertDialogBuilder.setTitle(iDomsAndroidApp.context.getString(R.string.markAllAsRead));
alertDialogBuilder.setMessage(iDomsAndroidApp.context.getString(R.string.markAllAsReadText));
alertDialogBuilder.show();
}
} catch (Exception e){
Log.w(iDomsAndroidApp.TAG, "Problem performing the action " + element.getTextContent(), e);
sentCrashReport("Problem performing the action " + element.getTextContent(), e);
}
}
}
I tried using the my SyncInterface, as it gives the context of the IntentService, but think it is a but clumsy and doesn't work:
public interface SyncUpdateInterface {
public void doProgress(String message, int increment, int total);
public void doProgress(String message, int increment);
public void doProgress(String message);
public Context getContext();
}
You might have to rethink your approach. The intentservice only lives for the duration of the onHandleIntent() method. That is to say, once the last line of code of the onHandleIntent() method is reached, the IntentService stops itself.
Try EventBus. It provides a solution to similar problems by making communication between components (Activities, Services, Standalone classes) of an application.
Use Gradle to import library
compile 'org.greenrobot:eventbus:3.1.1'
Define an event
public class MessageEvent { /* Additional fields if needed */ }
Launch Event using
EventBus.getDefault().post(new MessageEvent());
Register component to receive event
#Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
#Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
Receive launched even by declaring this method
#Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {/* Do something */};
For more information visit https://github.com/greenrobot/EventBus

Accessing SQLite from Activity, Async and Service

I am reading first 100 SMS from inbox and store it in local database. I am doing this with the help on AsyncTask. Afterwards I am reading those SMS from local database and display it. In onPostExecute of AysncTask after reading & storing first 100 SMS, I am invoking the service to read the remaining SMS and storing it in local database. Till before invoking the service, UI is responsive but after invoking the service, the UI became unresponsive. Is it due to database lock or something else. Please help to solve this problem...
My AsyncTask code
private class InitialSetup extends AsyncTask<String, Integer, Long> {
#Override
protected Long doInBackground(String... urls) {
fetchSMS();
updateDatabase();
return 0L;
}
#Override
protected void onPostExecute(Long result) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
populateUI(getApplicationContext());
Intent intent = new Intent(getApplicationContext(), SMSService.class);
getApplication().startService(intent);
}
}
Service code
public class SMSService extends Service {
Long inboxSMSID, localSMSID;
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onStart(Intent intent, int startid) {
if (intent.hasExtra("InboxSMS") && intent.hasExtra("SMSID")) {
inboxSMSID = intent.getLongExtra("InboxSMS", 0);
localSMSID = intent.getLongExtra("SMSID", 0);
globalToLocalSMSDB(inboxSMSID);
}
}
private void globalToLocalSMSDB(Long id) {
SMSTable smsObj = new SMSTable(getApplicationContext());
smsObj.open();
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor smscur = getContentResolver().query(uriSMSURI, null,
"_id < " + id, null, "_id DESC");
while (smscur.moveToNext()) {
String sender = SMSManagement.getSender(smscur);
String message = SMSManagement.getMessage(smscur);
String timeStamp = SMSManagement.getTime(smscur);
smsObj.insertIntoSMSTable(sender, message, timeStamp);
}
smscur.close();
smsObj.close();
}
}
The service is running in UI thread as well, you have to create worker threads there manually unless it's an IntentService.

Make sure that my code is thread safe

I am doing an Android service that gives content to other apps that can register as callback.
I am not 100% sure about how the Android Handler class works, so can someone confirm me that this code is thread safe?
public class MyService extends Service {
private static final String MESSAGE = "message";
private final RemoteCallbackList<IMyCallback> readerCallbacks = new RemoteCallbackList<IMyCallback>();
private static final int REPORT_MSG = 1;
private Thread readerThread;
#Override
public void onCreate() {
readerThread = new Thread(readerRunnable);
readerThread.setDaemon(true);
readerThread.start();
}
private Runnable readerRunnable = new Runnable() {
#Override
public void run() {
while (!Thread.interrupted()) {
// Blocking call
byte[] message = JniCommunicator.readMessage();
if (message == null || message.length == 0) {
continue;
}
Bundle b = new Bundle();
b.putByteArray(MESSAGE, message);
Message m = readHandler.obtainMessage(REPORT_MSG);
m.setData(b);
readHandler.sendMessage(m);
}
}
};
private final Handler readHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case REPORT_MSG:
byte[] message = msg.getData().getByteArray(MESSAGE);
// Broadcast the new message to all clients
final int N = readerCallbacks.beginBroadcast();
for (int i = 0; i < N; i++) {
try {
readerCallbacks.getBroadcastItem(i).newMessage(message);
} catch (RemoteException e) {
// The RemoteCallbackList will take care of removing
// the dead object for us.
}
}
readerCallbacks.finishBroadcast();
break;
}
}
};
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private final IService.Stub mBinder = new IService.Stub() {
public void registerCallback(IMyCallback cb) {
if (cb != null)
readerCallbacks.register(cb);
}
public void unregisterCallback(IMyCallback cb) {
if (cb != null)
readerCallbacks.unregister(cb);
}
};
}
In particular, if someone calls unregisterCallback() while the Handler is in the for loop, will it crash?
From my understanding, the Handler run in the same thread, so it is thread safe, but I am not sure.
Thanks
Handlers are thread safe, that is their entire purpose.
I'll agree that the documentation on the thread safety of handlers isn't the best but it would be very ironic if a class designed to communicate between thread weren't thread safe.
About the remote callbacks, they are also designed to be thread safe, you should read the documentation on this, it states clearly:
Performs locking of the underlying list of interfaces to deal with multithreaded incoming calls, and a thread-safe way to iterate over a snapshot of the list without holding its lock
All you have to make sure is that all variables multiple thread access are thread safe (which they are in your case) and that they aren't being changed (yours are final so no worries there either)

Categories

Resources