我如何得到原生活动的回应?

时间:2016-05-05 21:36:59

标签: android android-intent

例如:我的应用程序通过

调用本机设置活动
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(myIntent, CODE);

然后当它完成时(例如按下后退按钮)我想知道。我试过这种方式,但是当原始活动最终确定时没有调用onActivityResult() ...所以我的应用程序没有得到通知......

我该怎么办?有人知道吗?

...谢谢

修改

谢谢大家。 完整的代码是:

public class MainActivity extends AppCompatActivity {

    public static final int DIALOG_CODE = 0x1;

    private TextView txtGps;
    private TextView txtNet;

    private LocationManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        first();
    }

    private void checkConfigs() {
        boolean gps = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean net = manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        setWidgets(gps, net);

        if (gps && net){
            Toast.makeText(this, "Configurações Ok!", Toast.LENGTH_SHORT).show();
        } else {
            DialogConfig.exibDialog(getFragmentManager());
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if ((requestCode == DIALOG_CODE) && (resultCode == RESULT_OK)){
            checkConfigs();
        } else {
            Toast.makeText(this, "As config não foram alteradas!", Toast.LENGTH_SHORT).show();
        }
    }

    public void setWidgets(boolean gsp, boolean net){
        if (gsp){
            txtGps.setText(R.string.gps_hab);
        } else {
            txtGps.setText(R.string.gps_n_hab);
        }

        if (net){
            txtNet.setText(R.string.wifi_hab);
        } else {
            txtNet.setText(R.string.wifi_n_hab);
        }
    }

    private View.OnClickListener onClickCheck() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkConfigs();
            }
        };
    }

    @SuppressWarnings("ConstantConditions")
    private void first() {
        txtGps = (TextView) findViewById(R.id.txt_gps);
        txtNet = (TextView) findViewById(R.id.txt_net);

        manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        findViewById(R.id.btn_check).setOnClickListener(onClickCheck());
    }

    public static class DialogConfig extends DialogFragment {

        public static void exibDialog(FragmentManager fm){
            new DialogConfig().show(fm, "dialog");
        }

        @Override
        public android.app.Dialog onCreateDialog(Bundle savedInstanceState) {
            return new AlertDialog.Builder(getActivity())
                    .setTitle("As conf não estão corretas! Alt configs?")
                    .setPositiveButton("Ok", onOk())
                    .setNegativeButton("Cancel", onCancel())
                    .create();
        }

        private DialogInterface.OnClickListener onCancel() {
            return new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dismiss();
                }
            };
        }

        private DialogInterface.OnClickListener onOk() {
            return new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivityForResult(myIntent, DIALOG_CODE);
                }
            };
        }
    }
}

[解决]

再次感谢每一个,我再次尝试,在对话框中创建一个回调接口,这样可以通知活动并自己调用其他本机设置活动,并在onActivityResult()中得到响应,但只比较一个代码(不是RESULT_OK),这种方式有效。但是还有另一种方式(就像被建议的那样)使用生命周期,对我来说似乎更容易。

1 个答案:

答案 0 :(得分:0)

我基本上尝试了你尝试过的东西,它对我有用。

startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), 1);

onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d(PREFIX, "onActivityResult");
}

当我回到应用程序时,onActivityResult被调用了。 不确定你在做什么不同。你能展示你的onActivityResult代码吗?