Android custom launcher (Like toddler lock)

2019-06-09 03:38发布

I am making a children's app and I would like to add a child lock to it, just like the one on the app called "Toddler lock". Toddler lock asks you to set the default launcher when the lock is turned on which allows the home button to be disabled. How would I do this?

1条回答
▲ chillily
2楼-- · 2019-06-09 04:29

Ok so ive got a rough working example here:

  1. Create two activities and two layouts, in the main layout place a normal button. The other layout can just have a blank <LinearLayout>.

  2. This is what my manifest looks like:

            <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main"
            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=".secondact"
            android:label="@string/secondtitle">
            <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>
    

Notice <category android:name="android.intent.category.HOME" /> in second activity.

  1. Here is the code from my main activity:

           @Override
           public void onResume()
            {
           super.onResume();
           Button btn = (Button)findViewById(R.id.startBtn);
           btn.setOnClickListener(new View.OnClickListener() {
    
    public void onClick(View v) {
        Intent startMain = new Intent(Intent.ACTION_MAIN, null);
        startMain.addCategory(Intent.CATEGORY_HOME);
        startActivity(startMain);
    
    
    }
       });
            }
    
  2. And in my second activity i have:

    @Override
    public void startActivity(Intent intent)
    {
        super.startActivity(intent);
    
    }
    
  3. When i click the startbtn i get the dialog that you are talking about and you get to chooose launcher or the secondactivity, and select always use. The back button still gets you home, but hopefully this helps. This question might help you further: Disable home button in android toddler app?

查看更多
登录 后发表回答