Java 如何修复Android上服务中的Intent.getAction()错误
在我的应用程序中,我想在录制设备屏幕上使用一个服务,这个名称是Java 如何修复Android上服务中的Intent.getAction()错误,java,android,android-service,Java,Android,Android Service,在我的应用程序中,我想在录制设备屏幕上使用一个服务,这个名称是RecorderService 我写了下面的代码,但在某些设备上运行服务时显示ForceClose错误,应用程序已停止 我的服务代码: public class RecorderService extends Service { private static final SparseIntArray ORIENTATIONS = new SparseIntArray(); private int WIDTH, HEIG
RecorderService
我写了下面的代码,但在某些设备上运行服务时显示ForceClose错误,应用程序已停止 我的服务代码:
public class RecorderService extends Service {
private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
private int WIDTH, HEIGHT, FPS, DENSITY_DPI;
private int BITRATE;
private boolean isBound = false;
private String audioRecSource;
private String SAVEPATH, videoName;
private AudioManager mAudioManager;
private FloatingControlService floatingControlService;
static {
ORIENTATIONS.append(Surface.ROTATION_0, 0);
ORIENTATIONS.append(Surface.ROTATION_90, 90);
ORIENTATIONS.append(Surface.ROTATION_180, 180);
ORIENTATIONS.append(Surface.ROTATION_270, 270);
}
private int screenOrientation;
private String saveLocation;
private boolean isRecording;
private NotificationManager mNotificationManager;
Handler mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message message) {
Toast.makeText(RecorderService.this, "Service stoped", Toast.LENGTH_SHORT).show();
}
};
private Intent data;
private int result;
private WindowManager window;
private MediaProjection mMediaProjection;
private VirtualDisplay mVirtualDisplay;
private MediaProjectionCallback mMediaProjectionCallback;
private MediaRecorder mMediaRecorder;
//Service connection to manage the connection state between this service and the bounded service
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//Get the service instance
FloatingControlService.ServiceBinder binder = (FloatingControlService.ServiceBinder) service;
floatingControlService = binder.getService();
isBound = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
floatingControlService = null;
isBound = false;
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
videoName = GoodPrefs.getInstance().getString(ConstKeys.TEST_NAME, "Testady");
//return super.onStartCommand(intent, flags, startId);
//Find the action to perform from intent
switch (Objects.requireNonNull(intent.getAction())) {
case ConstKeys.SCREEN_RECORDING_START:
if (!isRecording) {
screenOrientation = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
data = intent.getParcelableExtra(ConstKeys.RECORDER_INTENT_DATA);
result = intent.getIntExtra(ConstKeys.RECORDER_INTENT_RESULT, Activity.RESULT_OK);
getValues();
startRecording();
openTestApp();
} else {
Toast.makeText(this, "Service has running", Toast.LENGTH_SHORT).show();
}
break;
case ConstKeys.SCREEN_RECORDING_PAUSE:
break;
case ConstKeys.SCREEN_RECORDING_RESUME:
break;
case ConstKeys.SCREEN_RECORDING_STOP:
stopRecording();
break;
}
return START_STICKY;
}
}
logcat错误(从firebase获取此错误):
显示此错误的错误:开关(Objects.requirennoull(intent.getAction())
我使用以下代码启动服务:
Intent recorderService = new Intent(this, RecorderService.class);
recorderService.setAction(ConstKeys.SCREEN_RECORDING_START);
recorderService.putExtra(ConstKeys.RECORDER_INTENT_DATA, data);
recorderService.putExtra(ConstKeys.RECORDER_INTENT_RESULT, resultCode);
startService(recorderService);
finish();
如何修复它?
NullPointerException
奇怪的是,intent参数为null。的确无论如何如果(intent==null){Toast(…sorry intent为null);stopSelf();return;},则可以执行检查。所有这些都是为了确保你的应用程序不会崩溃。@blackapps,谢谢你发送代码,但是在哪里使用它呢?你能用我上面的代码给我发送代码吗?你不清楚是哪条语句导致了异常吗?如果不是的话,你应该告诉我。如果是,那么在执行该语句之前。在你使用intent参数之前。@blackapps,真的不知道:(你能帮我吗?
Intent recorderService = new Intent(this, RecorderService.class);
recorderService.setAction(ConstKeys.SCREEN_RECORDING_START);
recorderService.putExtra(ConstKeys.RECORDER_INTENT_DATA, data);
recorderService.putExtra(ConstKeys.RECORDER_INTENT_RESULT, resultCode);
startService(recorderService);
finish();