Android custom launcher (Like toddler lock)

懵懂的女人 提交于 2019-12-23 04:58:07

问题


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:


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?



来源:https://stackoverflow.com/questions/12048135/android-custom-launcher-like-toddler-lock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!