运行KitKat(19)的三星设备有一个“屏幕镜像”选项,可以找到并与附近的兼容显示器同步。我想以编程方式实现此功能。
我已经尝试过MediaRoute和DisplayManager类。两者都成功检测到附近的显示,但仅在我手动打开屏幕镜像之后。我希望他们自动检测显示。是否有一些我缺少的东西可以启动扫描或将WifiDisplayController置于齿轮状态?
这是我对MediaRouter的实现:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
output = (TextView) findViewById(R.id.output);
mMediaRouter = MediaRouter.getInstance(this);
mSelector = new MediaRouteSelector.Builder()
.addControlCategory(MediaControlIntent.CATEGORY_LIVE_VIDEO)
.addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
.build();
}
@Override
public void onResume() {
super.onResume();
mMediaRouter.addCallback(mSelector, mMRCallback,
MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);
}
@Override
public void onPause() {
super.onPause();
mMediaRouter.removeCallback(mMRCallback);
}
protected void onDestroy() {
super.onDestroy();
}
private MediaRouter.Callback mMRCallback = new MediaRouter.Callback() {
@Override
public void onRouteAdded(MediaRouter router, MediaRouter.RouteInfo route) {
super.onRouteAdded(router, route);
String outputText = router.getRoutes().size() + " Routes Detected\n";
List routes = router.getRoutes();
for (int i=0;i<routes.size(); i++) {
outputText = outputText + routes.get(i) + "\n";
}
output.setText(outputText);
}
@Override
public void onRouteRemoved(MediaRouter router, MediaRouter.RouteInfo route) {
super.onRouteRemoved(router, route);
output.setText("Route Removed: " + route + "\n"
+ router.getRoutes().size() + " routes detected.");
}
@Override
public void onProviderAdded(MediaRouter router, MediaRouter.ProviderInfo provider) {
super.onProviderAdded(router, provider);
String outputText = router.getProviders().size() + " Providers Detected\n";
List providers = router.getProviders();
for (int i=0;i<providers.size(); i++) {
outputText = outputText + providers.get(i) + "\n";
}
output.setText(outputText);
}
@Override
public void onProviderRemoved(MediaRouter router, MediaRouter.ProviderInfo provider) {
super.onProviderRemoved(router, provider);
output.setText("Provider removed: " + provider + "\n"
+ router.getProviders().size() + " providers detected.");
}
@Override
public void onProviderChanged(MediaRouter router, MediaRouter.ProviderInfo provider) {
super.onProviderChanged(router, provider);
}
};
这是我对DisplayManager的实现:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
displayManager = (DisplayManager)
getSystemService(DISPLAY_SERVICE); }
@Override
public void onResume() {
super.onResume();
displayManager.registerDisplayListener(displayListener,handler);
displayManager.getDisplays();
}
@Override
public void onPause() {
super.onPause();
displayManager.unregisterDisplayListener(displayListener);
}
private DisplayManager.DisplayListener displayListener = new DisplayManager.DisplayListener() {
@Override
public void onDisplayAdded(int displayId) {
output.setText("Display added: " + displayId);
}
@Override
public void onDisplayRemoved(int displayId) {
output.setText("Display removed: " + displayId);
}
@Override
public void onDisplayChanged(int displayId) {
}
};
答案 0 :(得分:4)
我希望这对您有帮助,使用演员设置完成屏幕镜像,它会使用您设备的演员服务。
public void enablingWiFiDisplay() {
try {
startActivity(new Intent("android.settings.WIFI_DISPLAY_SETTINGS"));
return;
} catch (ActivityNotFoundException activitynotfoundexception) {
activitynotfoundexception.printStackTrace();
}
try {
startActivity(new Intent("android.settings.CAST_SETTINGS"));
return;
} catch (Exception exception1) {
Toast.makeText(getApplicationContext(), "Device not supported", Toast.LENGTH_LONG).show();
}
}