【问题标题】:start service from NotificationListenerService从 NotificationListenerService 启动服务
【发布时间】:2015-04-28 12:10:59
【问题描述】:

我是一个新的 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)

【问题讨论】:

    标签: java android call record audio-recording


    【解决方案1】:

    启动服务表单通知监听器

    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
    
            }
           }
    

    【讨论】:

    • 我的 RecordService 中有这个方法 - public void onStart(Intent intent, int startId),我不需要从意图中获取任何信息,所以根据 stackoverflow.com/questions/14182014/… 我不需要 onStartCommand 和 onStart 需要好好工作..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-21
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多