简体   繁体   中英

Checking first run of Activity, and suppressing another activity in Android

I'm making a launcher application with another settings activity to tweak the launcher.

Now, i don't want the launcher to be displayed as elligible upon pressing the home button until the user has not set it up up first (they will be asked to do that once the app downloads via notifications) through the settings activity.

So, can i suppress my launcher activity from running until after first run of application , and if not, then how to know first run of an activity .

PS: I already know how to implement first run of application.

This is based on the "Settings Activity" project created using Android Studio's "Start a new Android Studio project" template. After the project is successfully created, add a new activity class (that would be your launcher activity in your current project); for this example, it is a plain empty activity.

public class HomeScreenActivity extends AppCompatActivity {
}

Then add AndroidManifest.xml entries for that activity:

<activity
    android:name=".HomeScreenActivity"
    android:enabled="false"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.HOME" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

Pay attention to android:enabled="false" , that is the important part here. That way your launcher activity will be disabled by default. You will change it's state after user goes through the setup process.

In this example, I simply added a SwitchPreference and changed HomeScreenActivity 's state based on the user click.

private SwitchPreference prefEnableDisableHomeScreen;
private PackageManager packageManager;
private ComponentName homeScreenComponent;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.pref_general);
    setHasOptionsMenu(true);

    packageManager = getActivity().getPackageManager();
    homeScreenComponent = new ComponentName(getActivity().getApplicationContext(),
            HomeScreenActivity.class);

    prefEnableDisableHomeScreen = (SwitchPreference) findPreference("enable_disable_home_screen");
    prefEnableDisableHomeScreen.setChecked(getIsComponentEnabled(homeScreenComponent));

    prefEnableDisableHomeScreen.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object o) {
            boolean previousState = prefEnableDisableHomeScreen.isChecked();
            setComponentEnabledSetting(homeScreenComponent, previousState
                    ? PackageManager.COMPONENT_ENABLED_STATE_DISABLED
                    : PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
            prefEnableDisableHomeScreen.setChecked(getIsComponentEnabled(homeScreenComponent));

            return false;
        }
    });
}

private boolean getIsComponentEnabled(ComponentName componentName) {
    int state = packageManager.getComponentEnabledSetting(componentName);
    return PackageManager.COMPONENT_ENABLED_STATE_ENABLED == state;
}

private void setComponentEnabledSetting(ComponentName componentName, int newState) {
    packageManager.setComponentEnabledSetting(componentName, newState, PackageManager.DONT_KILL_APP);
}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM