从notificationlistenerservice启动服务

zpgglvta  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(343)

我是一个新的android开发人员,我正在尝试制作一个应用程序,在收到特定通知时录制音频,但是,当我从notificationlistenerservice启动recorder服务时,在android.media.mediarecorder.start上出现了一个illegalstateexception,我用两个按钮在一个常规活动中测试了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)
cnjp1d6j

cnjp1d6j1#

启动服务窗体通知侦听器

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

关于录音服务

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

        }
       }

相关问题