检查外部活动是否成功打开?

huangapple go评论105阅读模式
英文:

Check if external activity opened successfully?

问题

如何检查设置中的外部活动是否成功打开?在这种情况下,我想要检查Settings.ACTION_LOCATION_SOURCE_SETTINGS是否成功打开。

这是我的代码:

Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);                    
startActivity(intent);
英文:

How do I check if an external activity in Settings has opened successfully? In this case, I want to check if Settings.ACTION_LOCATION_SOURCE_SETTINGS opened successfully.

Here's my code:

Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);                    
startActivity(intent);

答案1

得分: 1

如果(intent.resolveActivity(getPackageManager()) == null){
// 没有找到可以处理此意图的Activity。
}
else {
// 存在可以处理此意图的Activity。
}

英文:
if (intent.resolveActivity(getPackageManager()) == null) {
    // No Activity found that can handle this intent. 
}
else{
    // There is an activity which can handle this intent. 
}

答案2

得分: 0

或许,你可以使用一个boolean标志变量来检查是否应该监听当前ActivityonPause()生命周期事件。
然后,我们可以在onPause()内部检查这个标志变量的值。如果它为真,那么很可能是因为下一个Activity成功打开而调用了它。一个示例Activity可能如下所示:

public class MainActivity extends AppCompatActivity {

    // 用于检查是否已打开其他活动的布尔变量
    // 如果已打开,我们的这个活动将被暂停
    private boolean externalActivityOpened = false;

    // 用于过滤所有其他可能导致我们的活动暂停的情况的布尔变量
    private boolean startLookingIfCurrentActivityIsPaused = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        openLocationSettings();
    }

    private void openLocationSettings() {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        // 将变量设置为true...如果此时活动已暂停,那么很有可能是因为打开了位置设置
        startLookingIfCurrentActivityIsPaused = true;
        // 除了这一点发生干扰,如电话呼叫之外,此解决方案应该能正常工作
        startActivity(intent);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 我们检查是否在我们想要启动位置设置之后调用了onpause
        // 如果是的话,几乎可以肯定新的活动已经启动了
        if (startLookingIfCurrentActivityIsPaused) {
            externalActivityOpened = true;
            startLookingIfCurrentActivityIsPaused = false;
            Toast.makeText(this, "新的活动已经启动!", Toast.LENGTH_LONG).show();
        }
    }
}
英文:

Maybe, you can use some boolean flag variable to check whether or not we should be listening to onPause() lifecycle event of the current Activity.
Then, we can check the value of this flag variable inside the onPause(). If it's true then it's more likely that it is called because the next Activity is opened successfully. A sample Activity may look like this:

public class MainActivity extends AppCompatActivity {

    // a boolean variable to check whether or not another activity is opened or not
    // if opened our this activity will be paused
    private boolean externalActivityOpened = false;

    // a boolean variable to filter all other condition due to which our activity
    // can be paused
    private boolean startLookingIfCurrentActivityIsPaused = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        openLocationSettings();
    }

    private void openLocationSettings() {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        // set variable to true...if activity is paused now, there is high chance
        // that it is paused because the location settings has been opened
        startLookingIfCurrentActivityIsPaused = true;
        // this solution will fail if at this very point the interruption occurs such as a phone call
        // other than that it should work fine I guess xD
        startActivity(intent);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // we check to see if onpause is called after we wanted to start the location settings
        // if yes then it is almost certain that the new activity has been started
        if (startLookingIfCurrentActivityIsPaused) {
            externalActivityOpened = true;
            startLookingIfCurrentActivityIsPaused = false;
            Toast.makeText(this, "New activity has been started!", Toast.LENGTH_LONG).show();
        }
    }
}

huangapple
  • 本文由 发表于 2020年7月23日 02:14:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63040683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定