Home » Android » android – how to open a system page and then close it in code?-Exceptionshub

android – how to open a system page and then close it in code?-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I meet a use case which would show a system setting page for 3 seconds then close it automatically.. My plan is to start the system setting activity with my PreferenceFragment, and in it’s onCreate(), use getActivity() to finish() it after 3 second:

public class MyPreferenceFragment extends PreferenceFragment{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new Timer().schedule(new TimerTask() {
            public void run() {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() { getActivity().finish(); }
                });
            }
        }, 3000);
    }
}

And start a setting page:

Intent intent = new Intent();
intent.setClassName("com.android.settings", 
    "com.android.settings.Settings$AccessibilitySettingsActivity");
intent.setAction(Intent.ACTION_MAIN);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
    | Intent.FLAG_ACTIVITY_CLEAR_TASK
    | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
    MyPreferenceFragment.class.getName());
startActivity(intent);

But it seems not run into MyPreferenceFragment.onCreate().

I’d ever seen another app, which is downloaded from play market, can open a system setting page, perform click then close it. I allow the accessibility for it before. My device is not rooted. How can I do like that?

How to&Answers: