Android 当组件需要启动结果意图时,如何将其与活动隔离?
我需要创建一个组件,其任务将是提供来自摄影机意图的照片文件。 到目前为止,我在活动中通过启动另一个活动并等待结果来完成这些事情。 但现在我想从业务逻辑层使用该组件,在该层中无法访问UI层(活动)。 如何创建满足这些要求的组件Android 当组件需要启动结果意图时,如何将其与活动隔离?,android,android-intent,android-activity,xamarin,xamarin.android,Android,Android Intent,Android Activity,Xamarin,Xamarin.android,我需要创建一个组件,其任务将是提供来自摄影机意图的照片文件。 到目前为止,我在活动中通过启动另一个活动并等待结果来完成这些事情。 但现在我想从业务逻辑层使用该组件,在该层中无法访问UI层(活动)。 如何创建满足这些要求的组件 在自定义应用程序类中,通过创建静态getter,例如应用程序类,使上下文可用 public class App extends Application { private static App instance; @Override public
应用程序
类中,通过创建静态getter,例如应用程序
类,使上下文
可用
public class App extends Application {
private static App instance;
@Override
public void onCreate() {
instance = this;
}
public static App getInstance(){
return instance;
}
}
如果您使用依赖项注入,我强烈建议it提供App
类。takePhoto()
和另外两个用于添加和删除侦听器的函数
public interface PhotoTakerComponent {
void takePhoto();
void addListener(PhotoTakerListener listener);
void removeListener(PhotoTakerListener listener);
}
PhotoTakerComponent
的实现将通过侦听器-PhotoTakerListener
与我们的biusness层类进行通信
public interface PhotoTakerListener {
void onPhotoTaken(Boolean success, String path);
}
PhotoTakerActivity
。此活动将是没有布局的组件内部类。我们将仅在启动正确意图时启动它,并在activityresult方法上等待reslut。所有这些都将立即完成。用户将永远不会看到此活动,因为我们不会膨胀任何布局
public class PhotoTakerActivity extends AppCompatActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
Uri uri = Uri.parse(mFileName);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, **uri**);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, RequestImageCapture);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RequestImageCapture) {
if (resultCode == RESULT_CANCELED){
for (PhotoTakerListener listener: listeners) {
listener.onPhotoTaken(false, null);
}
} else if (resultCode == RESULT_OK){
for (PhotoTakerListener listener: listeners){
listener.onPhotoTaken(true, mFileName);
}
}
}
finish();
}
}
PhotoTakerComponent
接口的实现,在takePhoto()
方法中,我们启动内部PhotoTakerComponent
public class DefaultPhotoTakerComponent implements PhotoTakerComponent {
private Context mApplicationContext;
public DefaultPhotoTakerComponent() {
mApplicationContext = App.getInstance().getApplicationContext();
}
@Override
public void takePhoto() {
if (mApplicationContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
try {
...
Intent intent = new Intent(mApplicationContext, PhotoTakerActivity.class);
intent.putExtra(PhotoTakerActivity.ExtraFileName, Uri.fromFile(image).toString());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mApplicationContext.startActivity(intent);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void addListener(PhotoTakerListener listener){
PhotoTakerActivity.addListener(listener);
}
public void removeListener(PhotoTakerListener listener){
PhotoTakerActivity.removeListener(listener);
}
}
Java
Android:
对于C#
Xamarin.Android:
应用程序
类中,通过创建静态getter,例如应用程序
类,使上下文
可用
public class App extends Application {
private static App instance;
@Override
public void onCreate() {
instance = this;
}
public static App getInstance(){
return instance;
}
}
如果您使用依赖项注入,我强烈建议it提供App
类。takePhoto()
和另外两个用于添加和删除侦听器的函数
public interface PhotoTakerComponent {
void takePhoto();
void addListener(PhotoTakerListener listener);
void removeListener(PhotoTakerListener listener);
}
PhotoTakerComponent
的实现将通过侦听器-PhotoTakerListener
与我们的biusness层类进行通信
public interface PhotoTakerListener {
void onPhotoTaken(Boolean success, String path);
}
PhotoTakerActivity
。此活动将是没有布局的组件内部类。我们将仅在启动正确意图时启动它,并在activityresult方法上等待reslut。所有这些都将立即完成。用户将永远不会看到此活动,因为我们不会膨胀任何布局
public class PhotoTakerActivity extends AppCompatActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
Uri uri = Uri.parse(mFileName);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, **uri**);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, RequestImageCapture);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RequestImageCapture) {
if (resultCode == RESULT_CANCELED){
for (PhotoTakerListener listener: listeners) {
listener.onPhotoTaken(false, null);
}
} else if (resultCode == RESULT_OK){
for (PhotoTakerListener listener: listeners){
listener.onPhotoTaken(true, mFileName);
}
}
}
finish();
}
}
PhotoTakerComponent
接口的实现,在takePhoto()
方法中,我们启动内部PhotoTakerComponent
public class DefaultPhotoTakerComponent implements PhotoTakerComponent {
private Context mApplicationContext;
public DefaultPhotoTakerComponent() {
mApplicationContext = App.getInstance().getApplicationContext();
}
@Override
public void takePhoto() {
if (mApplicationContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
try {
...
Intent intent = new Intent(mApplicationContext, PhotoTakerActivity.class);
intent.putExtra(PhotoTakerActivity.ExtraFileName, Uri.fromFile(image).toString());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mApplicationContext.startActivity(intent);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void addListener(PhotoTakerListener listener){
PhotoTakerActivity.addListener(listener);
}
public void removeListener(PhotoTakerListener listener){
PhotoTakerActivity.removeListener(listener);
}
}
Java
Android:
对于C#
Xamarin.Android: