从NotificationListenerService启动服务

时间:2015-04-28 12:10:59

标签: java android call record audio-recording

我是一个新的Android开发人员,我正在尝试制作一个应用程序,当我收到特定通知时录制音频,但是,当我从NotificationListenerService启动录音机服务时,我在android处获得IllegalStateException。 media.MediaRecorder.start, 我使用2个按钮在常规活动中测试了我的Recorderservice,它工作得很完美,只有当它从通知服务开始时才会出现错误。 我不知道该怎么做..请帮忙吗?

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);

    }
}

}

和错误 -

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)

1 个答案:

答案 0 :(得分:0)

启动服务表单通知监听器

@ManagedBean
@RequestScoped
public class TestBacking implements Serializable {
    private static final long serialVersionUID = 1L;

    private Integer dlgwidth=500;

    public void setWidth(){
        System.out.println("TestBacking : dlgwidth = "+dlgwidth);
    }
    public Integer getDlgwidth() {
        return dlgwidth;
    }

    public void setDlgwidth(Integer dlgwidth) {
        this.dlgwidth = dlgwidth;
    }
}

在录制服务上

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