Turn on/off airplane mode in Android using Device Assistant

272 views Asked by At

I am trying to turn on/off airplane mode in Android 11.

I have researched and found that it can be done with VoiceInteractionService.

Below is my code:

MyInteractionService.class

public class MyInteractionService extends VoiceInteractionService {
    @Override
    public void onReady() {
        super.onReady();
    }
}

MyInteractionSessionService.class

public class MyInteractionSessionService extends VoiceInteractionSessionService {
    @Override
    public VoiceInteractionSession onNewSession(Bundle bundle) {
        return new MyInteractionSession(this);
    }
}

MyInteractionSession.class

public class MyInteractionSession extends VoiceInteractionSession {

MyInteractionSession(Context context) {
    super(context);
}

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public void onHandleAssist(Bundle data,
                           AssistStructure structure,
                           AssistContent content) {
        super.onHandleAssist(data, structure, content);
        Intent intent = new Intent("android.settings.VOICE_CONTROL_AIRPLANE_MODE");
        intent.putExtra("airplane_mode_enabled", true);
        startVoiceActivity(intent);
    }
}

and interaction_service.xml

<?xml version="1.0" encoding="utf-8"?>
    <voice-interaction-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:sessionService="com.android.test.voiceinteraction.MyInteractionSessionService"
    android:recognitionService="com.android.test.voiceinteraction.MyInteractionService"
    android:supportsAssist="true"
/>

My code seems to work fine, when I change my app to the default device assistant app and long press on the Home button, it will turn on airplane mode.

However, I can not simulate long pressing the Home button. I want to call a similar action by code to turn on/off airplane mode.

Hope someone will help me solve this problem. Thank you!

0

There are 0 answers