Android:如何在小窗口中运行其他应用

时间:2019-05-15 10:11:52

标签: android multi-window

我看到许多可以在小窗口中运行任何已安装应用程序的应用程序。 我自己写。

现在,我可以在多屏模式下运行任何应用了:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.graphics.Rect;
import android.app.ActivityOptions;


public class MainActivity extends AppCompatActivity {

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

        startService(new Intent(this, ExampleAccessService.class));
        //wait for completion, unfortunately waitForIdle doesn't applicable here
        try {
            Thread.sleep(1000);
        }
        catch (Exception e) {}
        Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("APP_NAME");
        LaunchIntent.setFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        ActivityOptions ao = ActivityOptions.makeBasic();
        Rect rect = new Rect(0,0 ,100,100);
        ActivityOptions bounds = ao.setLaunchBounds(rect);
        startActivity(LaunchIntent, bounds.toBundle());
        finish();
    }
}

启用多窗口的服务:

import  android.accessibilityservice.AccessibilityService;
import android.util.Log;
import android.content.Intent;
import      android.view.accessibility.AccessibilityEvent;
public class ExampleAccessService extends AccessibilityService{

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Boolean res = performGlobalAction(AccessibilityService.GLOBAL_ACTION_TOGGLE_SPLIT_SCREEN);
        Log.i("GLOBAL_ACTION_TOGGLE_SPLIT_SCREEN", res.toString());
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
    }

    @Override
    public void onInterrupt() {

    }

    @Override
    protected void onServiceConnected() {
        // TODO Auto-generated method stub
        super.onServiceConnected();
    }

}

但是现在它可以运行NEAR其他应用程序,而不是在新窗口中运行。 我知道可以在任何安装的小窗口中运行。 我想念的是什么?

哦,我解决了! 只是不需要ExampleAccessService

1 个答案:

答案 0 :(得分:0)

哦,我解决了!只是不需要ExampleAccessService