【发布时间】:2016-11-08 23:35:02
【问题描述】:
我正在开发 Android M (6.0)。在我的服务中,我使用意图调用默认相机应用程序:
File photo = new File(Environment.getExternalStorageDirectory(), "myFile.jpg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
getApplication().startActivity(intent);
} catch (final ActivityNotFoundException e) {
e.printStackTrace();
} catch (final Exception e) {
e.printStackTrace();
}
在我的默认相机应用程序中,我可以按物理键ACTION_DOWN 拍照。我的问题是如何以编程方式调用密钥ACTION_DOWN。我尝试了一些方法,但它们不起作用(没有root)。另一方面,我们如何在不按 GUI 或手机物理按钮的情况下拍照?
拳法:
Runtime.getRuntime().exec("input keyevent " +
Integer.toString(KeyEvent.ACTION_DOWN));
第二种方法
BaseInputConnection mInputConnection = new BaseInputConnection(this, true);
KeyEvent down = new KeyEvent(now, now, KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_HOME, 0);
mInputConnection.sendKeyEvent(down);
第三种方法(不好,因为需要root手机)
Instrumentation m_Instrumentation = new Instrumentation();
m_Instrumentation.sendKeyDownUpSync( KeyEvent.ACTION_DOWN );
【问题讨论】:
-
“我的问题是如何以编程方式调用 ACTION_DOWN 键”——你不能。如果要拍照,请执行问题中第一个代码sn-p中的代码。
-
那么,您是说以编程方式插入 Key down 事件吗?
-
@uelordi:我想使用代码执行操作键。
-
@CommonsWare:有人跟我说,我们可以用代码控制拍照过程,而不用按GUI或手机上的任何按钮
-
“有人跟我说,我们可以用代码控制拍照过程”——嗯,不。出于明显的安全原因,您不能将关键事件注入其他应用程序。
标签: android android-service android-6.0-marshmallow keyevent