Question

How can I send a key event to an Activity I just started?

I figured for sending I can use:

Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyCode);

But it gives me (when running on UI thread or in new thread):

java.lang.RuntimeException: This method can not be called from the main application thread

On which thread do I have to call sendKeyDownUpSync()?

Here is how I launch the activity:

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);
    }

}

Requires permission: android.permission.READ_CONTACTS

Was it helpful?

Solution

On this site someone solved this problem (sending key events to an external activity): http://www.pocketmagic.net/2012/04/injecting-events-programatically-on-android/#.Ui81ddyg2f0

In short: Using Android API (official and internal) the OS forbids sending key events to activities of other apps. However, you can do it at Linux OS level. For this to work, you will need root access, though.

OTHER TIPS

// 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);
                    }

                }

        });
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top