How to set android app as home default and remove it programmatically

181 Views Asked by At

`I want to create android app using java and when install app it add to home default app list automatically. And add two buttons. when click button1 I want to set my app as the home default app by changing the phone settings. and when click button2 I want to deselect my app from the list and set default phone app. But without root access

I added below code to Manifest XML file

<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />

And added two buttons to main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start"
        android:onClick="setPreferred"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.266" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop"
        android:onClick="clearPreferred"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button1"
        app:layout_constraintVertical_bias="0.143" />


</androidx.constraintlayout.widget.ConstraintLayout>

**And created two methods in MainActivity.java **

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    private void setPreferred() {
        PackageManager pm = getPackageManager();
        IntentFilter f = new IntentFilter("android.intent.action.MAIN");
        f.addCategory("android.intent.category.HOME");
        f.addCategory("android.intent.category.DEFAULT");
        ComponentName cn = new ComponentName("com.rcs2.kiosktest", "com.rcs2.kiosktest.MainActivity");
        pm.addPreferredActivity(f, IntentFilter.MATCH_CATEGORY_EMPTY, null, cn);
    }

    private void clearPreferred() {
        PackageManager pm = getPackageManager();
        IntentFilter f = new IntentFilter("android.intent.action.MAIN");
        f.addCategory("android.intent.category.HOME");
        f.addCategory("android.intent.category.DEFAULT");
        ComponentName cn = new ComponentName("com.rcs2.kiosktest", "com.rcs2.kiosktest.MainActivity");
        pm.clearPackagePreferredActivities("com.rcs2.kiosktest");
    }
}

But didn't worked as I need`

0

There are 0 best solutions below