[JavaScript] 纯文本查看 复制代码 import android.content.Context;
import android.hardware.hdmi.HdmiControlManager;
import android.hardware.hdmi.HdmiPlaybackClient;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private HdmiControlManager hdmiControlManager;
private HdmiPlaybackClient hdmiPlaybackClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hdmiControlManager = (HdmiControlManager) getSystemService(Context.HDMI_CONTROL_SERVICE);
hdmiPlaybackClient = hdmiControlManager.getHdmiPlaybackClient();
Button standbyButton = findViewById(R.id.standby_button);
standbyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendStandbyCommand();
}
});
}
private void sendStandbyCommand() {
if (hdmiPlaybackClient != null) {
hdmiPlaybackClient.sendStandby(new HdmiPlaybackClient.OneTouchPlayCallback() {
@Override
public void onComplete(int result) {
if (result == HdmiControlManager.RESULT_SUCCESS) {
showToast("Standby command sent successfully");
} else {
showToast("Failed to send standby command");
}
}
});
} else {
showToast("HDMI playback client is null, make sure HDMI-CEC is supported on this device");
}
}
private void showToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
|