Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检查外部活动是否已成功打开?_Java_Android_Android Activity - Fatal编程技术网

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

Java 检查外部活动是否已成功打开?,java,android,android-activity,Java,Android,Android Activity,如何检查设置中的外部活动是否已成功打开?在这种情况下,我想检查设置。操作\u位置\u源\u设置是否成功打开 这是我的密码: Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(intent); 也许,您可以使用一些布尔值标志变量来检查我们是否应该侦听当前活动的onPause()生命周期事件。 然后,我们可以在onPause()中检查该标志变

如何检查设置中的外部活动是否已成功打开?在这种情况下,我想检查
设置。操作\u位置\u源\u设置
是否成功打开

这是我的密码:

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

也许,您可以使用一些
布尔值
标志变量来检查我们是否应该侦听当前
活动
onPause()
生命周期事件。 然后,我们可以在
onPause()
中检查该标志变量的值。如果为真,则更有可能调用它,因为下一个
活动
已成功打开。示例
活动
可能如下所示:

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();
        }
    }
}

有类似于
startActivityForResult
Yes的内容,但是如何在我的案例中使用它呢?您可以向
Activity
lifecycle寻求帮助。如果成功打开另一个
活动
,则将调用第一个活动中的
onPause()
。@cgb\u pandey您能给我一个cas实现作为答案吗?我已添加答案。不过这只是一个解决办法。有时活动存在,但由于某种原因它根本无法打开。这在华为的一些设备中发生。我已经测试过你的代码了。很遗憾,您的代码没有解决这个问题。在这种情况下,对
startActivity()
的调用是否会引发
ActivityNotFoundException
?您在日志中看到了什么吗?我的用户没有遇到任何崩溃,因此可能不是ActivityNotFoundException。此外,该活动确实存在,但在华为的一些设备上根本无法打开。听起来,您正试图破解并修复华为EMUI可能存在的错误。即使你能检测到活动没有打开,你会怎么做?@Sharp我会弹出一个窗口,解释在设置中需要执行哪些步骤。不幸的是,这种方法很容易失败。我更喜欢另一种更好的方式。也许你可以尝试用
startActivityForResult(intent,requestCode)
启动
intent
,然后看看它是否以
onActivityResult()
或更新的
ActivityContracts
方式成功返回。请通过编辑答案详细解释
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();
        }
    }
}