Development (Advanced)
Step 1: Run other apps on phone or DeX display in Samsung DeX dual mode
Dual mode is a new feature of Samsung DeX from Note 9. With DeX for PC support, Samsung DeX dual mode has become more useful. For example, the app makes an activity or another app launches at specific display. In this step, we implement to execute the email app at a specific display via button.
In the sample app, e-mail app can launch on a specific monitor.
-
Implement to run an e-mail app via
button1 (R.id.btn1)
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startEmail = (Button) findViewById(R.id.btn1); startEmail.setText("Start E-mail app"); startEmail.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btn1 : Intent intent = getPackageManager().getLaunchIntentForPackage("com.google.android.gm"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); break; } }
-
Make sample application decide which display the app will launch with a switch. Code snippet below can provide the information for target display.
// Samsung DeX display DisplayManager dm = (DisplayManager)getSystemService(Context.DISPLAY_SERVICE); Display[] displays = dm.getDisplays("com.samsung.android.hardware.display.category.DESKTOP"); Display targetDisplay = displays[0]; // Phone display DisplayManager dm = (DisplayManager)getSystemService(Context.DISPLAY_SERVICE); Display targetDisplay = dm.getDisplay(Display.DEFAULT_DISPLAY);
Application needs to execute
startActivity
with the display information to get throughActivityOptions setLaunchDisplayId
API. Finally, we need to change the code to run email app as seen below.@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startEmail = (Button) findViewById(R.id.btn1); startEmail.setText("Start E-mail app"); startEmail.setOnClickListener(this); sw = (Switch)findViewById(R.id.switch1); sw.setText("DeX display"); } @Override public void onClick(View view) { Display targetDisplay; DisplayManager dm = (DisplayManager)getSystemService(Context.DISPLAY_SERVICE); if(sw.isChecked()) { Display[] displays = dm.getDisplays("com.samsung.android.hardware.display.category.DESKTOP"); targetDisplay = displays[0]; }else { targetDisplay = dm.getDisplay(Display.DEFAULT_DISPLAY); } switch (view.getId()){ case R.id.btn1 : Intent intent = getPackageManager().getLaunchIntentForPackage("com.google.android.gm"); if(isDeXEnabled()){ ActivityOptions options = ActivityOptions.makeBasic(); options.setLaunchDisplayId(targetDisplay.getDisplayId()); startActivity(intent, options.toBundle()); } else { startActivity(intent); } break; } }
Learn more by watching the video
Step 2: Utilize both phone and DeX display together on Samsung DeX dual mode
In Samsung DeX Dual Mode, your app can run different activities on each phone and DeX display.
In this case, developers should consider some things. First, developers need to set target display like in previous step. Here, we use the targetDisplay
defined previously. Second, to start additional Activity on additional multi-window, we need to add Intent.FLAG_ACTIVITY_NEW_TASK
flag.
Intent intent2 = new Intent(MainActivity.this,SubActivity.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Lastly, below meta-data should be added at manifest.
<application>
…
<meta-data
android:name="com.samsung.android.multidisplay.keep_process_alive"
android:value="true" />
…
</application>
Samsung DeX (Framework) does NOT allow a process to have tasks on multiple displays and contexts such as activities and services if they have the same displayId
.
For this reason, only one instance of the app can run and the older instance of the app will be closed if a new instance is launched. However, some apps, including activity or service, needs multiple instances in certain situations. This meta-data keeps an application alive and capable of running multiple instances. App developer has a responsibility to use proper context to make proper UIs, since framework is not concerned about the process, application context, and package context.
In the sample app, SubActivity
can run on a specific monitor.
Implement to start SubActivity
on the targetDisplay
via button2(R.id.btn2)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startSubActivity = (Button) findViewById(R.id.btn2);
startSubActivity.setText("Start \nSubActivity");
startSubActivity.setOnClickListener(this);
}
case R.id.btn2 :
Intent intent2 = new Intent(MainActivity.this,SubActivity.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if(isDeXEnabled()){
ActivityOptions options = ActivityOptions.makeBasic();
options.setLaunchDisplayId(targetDisplay.getDisplayId());
startActivity(intent2, options.toBundle());
} else {
startActivity(intent2);
}
break;
Learn more by watching the video
You're done!
Congratulations! You have successfully achieved the goal of this Code Lab activity. Now, you can optimize your app to be compatible with DeX environment by yourself! Keep in you mind that you can modify your app to have a desktop-like experience without any restrictions and any SDK for Samsung DeX.
If you're having any trouble with the activity, you may check out the link below.