如何将关键事件发送到我刚开始的活动?
我想发送我可以使用:
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyCode);
但它给了我(在 UI 线程或新线程上运行时):
java.lang.RuntimeException:无法从主应用程序线程调用此方法
我必须在哪个线程上调用sendKeyDownUpSync()
?
这是我启动活动的方式:
public void openWhatsappConversation(String whatsappid) {
whatsappid = "4917012345678@s.whatsapp.net";
Cursor c = getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
new String[] { ContactsContract.Contacts.Data._ID },
ContactsContract.Data.DATA1 + "=?",
new String[] { whatsappid }, null);
c.moveToFirst();
Intent whatsapp = new Intent(Intent.ACTION_VIEW,
Uri.parse("content://com.android.contacts/data/"
+ c.getString(0)));
whatsapp.putExtra(Intent.EXTRA_TEXT, "whatsapp");//
c.close();
if (whatsapp != null) {
startActivity(whatsapp);
} else {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
// download for example after dialog
Uri uri = Uri.parse("market://details?id=com.whatsapp");
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
}
}
需要权限:android.permission.READ_CONTACTS