Intent 的 onActivityResult

发布于 2024-11-02 23:04:10 字数 1868 浏览 2 评论 0原文

else{ //this is part of some onclick listener of a button in the program
        slider.close();
        mapView.setClickable(false);
        Toast.makeText(getBaseContext(), "GPS cannot retrieve your location. GPS needs clear sky or service is off. Please turn on GPS service.", Toast.LENGTH_LONG).show();
        final Button bt = (Button)findViewById(R.id.turnGPS);
        bt.setVisibility(View.VISIBLE);
        bt.setOnTouchListener(new OnTouchListener(){

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Intent activateGps = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                //Map.this.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                Map.this.startActivityForResult(activateGps, RESULT_OK);
                //bt.setVisibility(View.INVISIBLE);
                return false;
            }

        });

    }
}

我想启动新意图,以便用户可以启用 GPS(如果之前未激活 GPS)。我使用 startActivityForResult 方法可以响应 Activity 是否成功。成功后,它必须对用户界面进行一些更改......我尝试启用 GPS,但 UI 上仍然没有任何更改(例如:按钮不隐藏)。我做错了什么?

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode==RESULT_OK)
    {
        if(resultCode == RESULT_OK && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
        {
            Button bt = (Button)findViewById(R.id.turnGPS);
            bt.setVisibility(View.INVISIBLE);
            this.finish();
        }
        else if(resultCode == RESULT_CANCELED)
        {
            Toast.makeText(getBaseContext(), "No GPS device or service enabled", Toast.LENGTH_LONG+Toast.LENGTH_LONG).show();
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

这是我的 onActivityResult 方法。 this.finish(); 是否执行?启用后会完成当前活动,因此会启动 OnResume()?

else{ //this is part of some onclick listener of a button in the program
        slider.close();
        mapView.setClickable(false);
        Toast.makeText(getBaseContext(), "GPS cannot retrieve your location. GPS needs clear sky or service is off. Please turn on GPS service.", Toast.LENGTH_LONG).show();
        final Button bt = (Button)findViewById(R.id.turnGPS);
        bt.setVisibility(View.VISIBLE);
        bt.setOnTouchListener(new OnTouchListener(){

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Intent activateGps = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                //Map.this.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                Map.this.startActivityForResult(activateGps, RESULT_OK);
                //bt.setVisibility(View.INVISIBLE);
                return false;
            }

        });

    }
}

I want to start new intent so user can enable GPS (if GPS is not activated before). I used startActivityForResult method that can respond whether Activity has resulted success or not. When success it has to do some changes on the user interface....I tried and I enable the GPS but still no changes on the UI (e.g: button does not hide). What I'm doing wrong?

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode==RESULT_OK)
    {
        if(resultCode == RESULT_OK && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
        {
            Button bt = (Button)findViewById(R.id.turnGPS);
            bt.setVisibility(View.INVISIBLE);
            this.finish();
        }
        else if(resultCode == RESULT_CANCELED)
        {
            Toast.makeText(getBaseContext(), "No GPS device or service enabled", Toast.LENGTH_LONG+Toast.LENGTH_LONG).show();
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

Here is my onActivityResult method. Does this.finish(); finishes the current activity when it is enabled, so it initiates OnResume()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

活雷疯 2024-11-09 23:04:10

PreferenceActivitystartActivityForResult() 一起使用时存在一些限制(其中一种讨论请参见 onActivityResult() 过早调用)。

我的建议是:通过 startActivity() 而不是 startActivityForResult() 启动 PreferenceActivity,然后放入需要重新启动的代码-读取onStart()onResume()方法中的首选项,当Activity位于堆栈顶部时将立即调用该方法。

There are some restrictions while using PreferenceActivity with startActivityForResult() (one of these kind of discussion see onActivityResult() called prematurely).

My suggestion is: start the PreferenceActivity by startActivity() instead of startActivityForResult(), and then put your code that needs to re-read the preferences in onStart() or onResume() method, which will be invoked immediately as the Activity is on the top of the stack.

鹤舞 2024-11-09 23:04:10
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main"
        android:alwaysRetainTaskState="True"
        android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main"
        android:alwaysRetainTaskState="True"
        android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
情深如许 2024-11-09 23:04:10

Android 中存在一个错误...将您的 Activity 更改

android:launchMode="singleInstance(or singleTasc)"

android:launchMode="singleTop"

调用 startActivityForResult() 方法并再次运行。它仍然会起作用。

There's a bug in Android... Change your

android:launchMode="singleInstance(or singleTasc)"

to

android:launchMode="singleTop"

in your Activity which is calling the startActivityForResult() method and run again. It still will be working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文