2

如何将关键事件发送到我刚开始的活动?

我想发送我可以使用:

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

4

2 回答 2

2

在这个网站上有人解决了这个问题(向外部活动发送关键事件): http ://www.pocketmagic.net/2012/04/injecting-events-programatically-on-android/#.Ui81ddyg2f0

简而言之:使用 Android API(官方和内部),操作系统禁止向其他应用程序的活动发送关键事件。但是,您可以在 Linux 操作系统级别执行此操作。不过,要使其正常工作,您将需要 root 访问权限。

于 2013-09-10T16:16:44.370 回答
0
// try this
public void openWhatsappConversation(String whatsappid) {

        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                 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(YourActivityName.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);
                    }

                }

        });
    }
于 2013-09-10T06:29:54.443 回答