我正在创建一个应用程序,我需要找到用户的当前位置。
所以在这里我想做一个任务,比如当用户从该系统意图返回时,我的任务应该在那之后完成。(显示用户当前位置)
所以我打算使用OnActivityResult()
。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
但问题是我不知道如何在不扩展Activity的类中使用该方法。
请有人告诉我如何实现这一目标?
答案 0 :(得分:11)
您需要订购活动才能收到结果。
如果仅用于组织代码,则从Activty类调用其他类。
public class Result {
public static void activityResult(int requestCode, int resultCode, Intent data){
...
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Result.activityResult(requestCode,resultCode,data);
...
}
答案 1 :(得分:6)
在非Activity类中创建内部类,并在其中定义活动结果处理程序:
class singletonActivity extends Activity{
protected void onActivityResult(...){
// do whatever ..
}
}
将其实例化以调用startActivityForResult
Activity actv = new singletonActivity(..)
actv.startActivityForResult(intent ..)
将调用您的处理程序。 :)
ps:你可能需要包含一些覆盖。把它们留空。
pps:这是老派java mouseListenerAdapter样式 〜乌>答案 2 :(得分:3)
您无法将此方法称为超出范围。
protected void onActivityResult (int requestCode, int resultCode, Intent data)
如果方法是受保护,就像这种情况一样,您可以看到访问级别表格以了解如何继续。
|-----------------------------------------------------------|
| ACCESS LEVELS |
|------------------|---------|---------|----------|---------|
| Modifier | Class | Package | Subclass | World |
|------------------|---------|---------|----------|---------|
| public | Y | Y | Y | Y |
|------------------|---------|---------|----------|---------|
| protected | Y | Y | Y | N |
|------------------|---------|---------|----------|---------|
| no modifier | Y | Y | N | N |
|------------------|---------|---------|----------|---------|
| private | Y | N | N | N |
|------------------|---------|---------|----------|---------|
如您所见,此方法只能从android.app.*
包,Activity
及其子类调用。
<强> SOLUTION:强>
你需要做这样的事情:
我们有一个班级ImagePicker
,用于从图库或相机或删除中选择图片。如果用户想要删除图片,则此类需要调用onActivityResult
(我们不需要为我们已经知道的结果启动Activity
。)
public class ImagePicker {
private ImagePickerDelegate delegate;
public ImagePicker (ImagePickerDelegate delegate) {
this.delegate = delegate;
}
//Will explain this two methods later
public void show() {
//Some code to show AlertDialog
}
public void handleResponse(Intent data) {
//Some code to handle onActivityResult
}
//Our interface to delegate some behavior
public interface ImagePickerDelegate {
void onImageHandled(Bitmap image);
void onImageError();
void onImageDeleted();
}
}
要在Activity
中使用此类,我们需要实现委托方法并将我们的活动作为ImagePicker
的委托传递:
public class MyActivity extends Activity implements ImagePicker.ImagePickerDelegate {
ImagePicker imagePicker;
@OnClick(R.id.image_edit)
public void selectImage () {
imagePicker = new ImagePicker(this);
imagePicker.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ImagePicker.REQUEST_IMAGE_PICKER && resultCode == RESULT_OK) {
imagePicker.handleResponse(data);
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onImageHandled(Bitmap image) {
//handle image resized
imageView.setImageBitmap(image);
}
@Override
public void onImageError() {
//handle image error
Toast.makeText(this, "Whoops - unexpected error!", Toast.LENGTH_SHORT).show();
}
@Override
public void onImageDeleted() {
//handle image deleted
groupImageView.setImageBitmap(null);
groupImageView.setImageResource(R.drawable.ic_pick_picture);
}
}
最后,我们需要调用thous委托方法,这种方法发生在show()
和handleResponse(Intent data)
中:
//The show method create and dialog with 3 options,
//the important thing here, is when an option is selected
public void show() {
//Inflating some views and creating dialog...
NavigationView navView = (NavigationView)viewInflated.findViewById(R.id.navigation_menu);
navView.setNavigationItemSelectedListener( new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.action_select_image:
Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
activity.startActivityForResult(pickPhoto , REQUEST_IMAGE_PICKER);
break;
case R.id.action_take_picture:
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
activity.startActivityForResult(takePicture, REQUEST_IMAGE_PICKER);
break;
case R.id.action_delete_image:
delegate.onImageDeleted(); //send response to activity
break;
}
alertDialog.dismiss();
return true;
}
});
//Show dialog...
}
//this method is called from onActivityResult
public void handleResponse(Intent data) {
try {
//Retrieve and resize image...
delegate.onImageHandled(image); //send the image to activity
} catch (Exception e) {
e.printStackTrace();
delegate.onImageError(); //send error to activity
}
}
最后,我们拥有class
,可以在Activity
而不是onActivityResult
中调用方法,但是当您在onActivityResult
中获得结果时,你需要在class
答案 3 :(得分:1)
您需要在此课程中注册Activity
,然后使用OnActivityResult()
进行该活动。
答案 4 :(得分:1)
当您使用活动中的startActivityForResult方法启动活动时,只有调用者才会返回结果。
因此,您可以处理结果并将其传递给任务或更新该活动的UI:
int MY_REQUEST_ID = 1;
public void onClick(){
//Select a contact.
startActivityForResult(
new Intent(Intent.ACTION_PICK,
new Uri("content://contacts")),
MY_REQUEST_ID);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCose == MY_REQUEST_ID && resultCode == SUCCESS) {
MyAsyncTask task = new AsyncTask(requestCode, resultCode, data);
task.execute();
// or update the UI
textView.setText("Hi, activity result: "+ resultCode);
}
}
答案 5 :(得分:0)
我正在这样使用它,这可能对其他人有帮助
我的片段中有
// Upload Cover Photo On Button Click
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Start The Image Cropper And Go To onActivityResult
Intent intent = ImageManager.startImageCropper(getContext());
startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
然后在片段中调用这样的结果
// On Activity Result for Start Activity For Result
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Get The Image From Image Cropper
Uri resultUri = ImageManager.activityResult(requestCode, resultCode, data, getContext());
}
支持这些的公共类/功能是
public class ImageManager {
// Start Image Cropper
public static Intent startImageCropper(Context context) {
// Crop Image
Intent intent = CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setActivityTitle("Title")
.setCropMenuCropButtonTitle("Save")
.setAutoZoomEnabled(true)
.setAspectRatio(1, 1)
.getIntent(context);
return intent;
}
public static Uri activityResult(int requestCode, int resultCode, Intent data, Context context) {
// Handle Cropped Image
Uri resultUri = null;
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == Activity.RESULT_OK) {
resultUri = result.getUri();
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
Toast.makeText(context, (CharSequence) error, Toast.LENGTH_SHORT).show();
}
}
return resultUri;
}
}
答案 6 :(得分:-2)
最后,我得到了我需要的东西以及这个问题的解决方案。
Activity con;
Intent intent_= new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
con.startActivityForResult(intent_, 0);
现在我可以在这里创建一个方法,
public void startActivityForResult(Intent i, int code)
{
Log.e("", "insede method startActivityForResult()");
}
通过使用此系统不会为我的意图创建子活动,因此,只有在用户从意图返回后才会调用此方法。