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)