我试图为新的图像捕捉制作广播接收器。
specifyIntentFilterForBroadCastRecivers = new IntentFilter(MediaStore.ACTION_IMAGE_CAPTURE);
startService(specifyIntentFilterForBroadCastRecivers,CameraImageCaptureBroadCastReciver.class);
break;
我的startService方法:
private void startService(IntentFilter specifyIntentFilterForBroadCastRecivers, Class broadCastReciverClass){
Intent serviceIntent = new Intent(context, BroadCastServices.class);
serviceIntent.putExtra("IntentFilter", specifyIntentFilterForBroadCastRecivers);
serviceIntent.putExtra("Class", broadCastReciverClass);
context.startService(serviceIntent);
}
service onStartCommand:
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle extras = intent.getExtras();
IntentFilter intentFilter = (IntentFilter) extras.get("IntentFilter");
Class broadCastReciverClass = (Class) extras.get("Class");
Object broadCastReciver = null;
try {
broadCastReciver = broadCastReciverClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
registerReceiver((BroadcastReceiver) broadCastReciver, intentFilter);
return super.onStartCommand(intent, flags, startId);
}
并播放onreceive
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Image Captured", Toast.LENGTH_SHORT).show();
}
但似乎MediaStore.ACTION_IMAGE_CAPTURE不正确。我也有其他的intentfilters,如intent.battery_low / battery_ok,它的工作原理。 服务正常。 问题是我使用的是正确的意图吗?