0

I'm really new with java but was playing a little around in Android Studio and have create a small app (my own Launcher).

My Question:

On Andorid OS - In terminal i use this command to deactive the Installed Launcher

pm disable com.android.launcher3

Is it possible, to create an App that can do the same? I need that app on system startup. I can only use my own launcher after i have deactive the launcher3.

I have put this code inside my apk i have created. But the launcher i will disable still working. What did i wrong?

Have i to create a new app with this code:

Process su = Runtime.getRuntime().exec("pm ...your cmd");
su.waitFor();

or can i integrate this code inside my own app like i tried?

This is my MainActivity.xnml

package com.example.max.col2boot;

        import android.Manifest;
        import android.content.Intent;
        import android.content.pm.ActivityInfo;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.view.View;
public class MainActivity extends AppCompatActivity {

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

    public void openCOL(View view) {
        Intent openCOLapp = getPackageManager().getLaunchIntentForPackage("com.igg.clashoflords2"); //open col2
        startActivity(openCOLapp);
        moveTaskToBack(true);
    }
    public void openCOLapp() {
        this.finish(); //exit app col2 to start game
    }

    public void exitCOL(View view) {
        moveTaskToBack(true);
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(1); //close app col2boot
    }

    private void runShellCommand(String command) throws Exception {
        Process process = Runtime.getRuntime().exec("pm disable com.android.launcher3"); //disable Launcher3
        process.waitFor();
    }
}

This is my Android Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.max.col2boot">
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <receiver
            android:name=".BootReceiver"
            android:enabled="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

This is my BootReceiver.java

package com.example.max.col2boot;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BootReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}
Max
  • 41
  • 7

1 Answers1

0

To start your activity on start, you can place a receiver in your Manifest (there are other questions how to launch your app on system start). To execute a shell command, just use

Process su = Runtime.getRuntime().exec("pm ...your cmd");
su.waitFor();
larsaars
  • 2,065
  • 3
  • 21
  • 32
  • Thank you. It works. I added your code to my app and gave it uses permissons for write / read system settings. – Max Aug 14 '18 at 12:47