Android P FreeForm自由窗口模式简单实现
16lz
2021-01-23
开启系统FreeForm模式。
adb shell settings put global enable_freeform_support 1
adb shell settings put global force_resizable_activities 1
/frameworks/native/data/etc/android.software.freeform_window_management.xml
push到目录system/etc/permissions/
代码如下:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(this, FreeFormActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT | Intent.FLAG_ACTIVITY_NEW_TASK); //ActivityOptions activityOptions = ActivityOptions.makeBasic(); //activityOptions.setLaunchWindowingMode(5); ActivityOptions activityOptions = getActivityOptions(this); int left = 200; int top = 100; int right = 600; int bottom = 200; activityOptions.setLaunchBounds(new Rect(left,top,right,bottom)); Bundle bundle = activityOptions.toBundle(); startActivity(intent,bundle); } public static ActivityOptions getActivityOptions(Context context) { ActivityOptions options = ActivityOptions.makeBasic(); int freeform_stackId = 5; try { Method method = ActivityOptions.class.getMethod("setLaunchWindowingMode", int.class); method.invoke(options, freeform_stackId); Log.i("FreeForm","sunxiaolin,getActivityOptions freeform_stackId = " + freeform_stackId); } catch (Exception e) { /* Gracefully fail */ Log.i("FreeForm","sunxiaolin,getActivityOptions e = " + e); } return options; }}
FreeFormActivity是一个只显示文本“Hello FreeForm!”的Activity.
效果图: