0

i'm a new android developer, and i'm trying to make an application that record audio when i get a specific notification, BUT, when i start the recorder service from my NotificationListenerService, i get a IllegalStateException at at android.media.MediaRecorder.start, i tested my Recorderservice in a regular activity with 2 buttons, and it works perfect, only when it start's from the notification service i get the ERROR.. i have no idea what to do.. some help please?

public class NotificationListener extends NotificationListenerService {
.......
.......
public void onNotificationPosted(StatusBarNotification sbn) {
       EXTRA_TEXT = (String) sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TEXT);
    Log.i("", sbn.toString());
  //  Log.i("","--------------------------");
    if(sbn.getPackageName().equals(PACKAGE_NAME))
    {
        Log.d(TAG, "our notification");

        if(Objects.equals(EXTRA_TEXT, getResources().getString(R.string.notificationText)))
        {
            Log.d("CallRecorder", "OUR NOTIFICATION HERE, starting recording");
            Intent callIntent = new Intent(this, RecordService.class);
            ComponentName name = this.startService(callIntent);
            if (null == name) {
                Log.e("CallRecorder", "startService for RecordService returned null ComponentName");
            } else {
                Log.i("CallRecorder", "startService returned " + name.flattenToString());
            }
        }
     }


    @Override
public void onNotificationRemoved(
        StatusBarNotification sbn) {
        Log.i("","---Notification Removed---");
        Log.i("","--------------------------");
        if ((sbn.getId() == id)) {
        Log.d("CallRecorder", "OUR NOTIFICATION REMOVED, stoping recording");
        Boolean stopped = this.stopService(new Intent(this, RecordService.class));
        Log.i("CallRecorder", "stopService for RecordService returned " + stopped);

    }
}

}

and the error -

RecordService::onStart caught unexpected exception
java.lang.IllegalStateException
        at android.media.MediaRecorder.start(Native Method)
        at com.didi.elections2015.RecordService.onStart(RecordService.java:176)
        at android.app.Service.onStartCommand(Service.java:458)
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2894)
        at android.app.ActivityThread.access$2100(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Didi78
  • 247
  • 1
  • 15

1 Answers1

0

start the service form notification listener

Intent send = new Intent(this, RecordService.class);
                    send.putExtra("RECORD", "start");
                    this.startService(send);

On the Recording Service

public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG,"onStartCommand");
        //TODO do something useful
        String data=intent.getStringExtra("RECORD");
        Log.i(TAG, "data==" + data + "");
        if(data!=null){
           // if data is equal to start.do your stuff

        }
       }
krishnan muthiah pillai
  • 2,711
  • 2
  • 29
  • 35
  • i have this methud in my RecordService - public void onStart(Intent intent, int startId), i dont need to get any information from the intent so accord to http://stackoverflow.com/questions/14182014/android-oncreate-or-onstartcommand-for-starting-service i dont need onStartCommand and onStart need to work well.. – Didi78 Apr 28 '15 at 15:23