0

So if you have ever used the Bitmoji keyboard, you know that it shares your selected Bitmoji in whatever application you are using, be it messenger or SMS without the normal sharing intent pop up, where you choose what application to share it with. This is really my first Android project, as I have been developing for iOS. iOS makes it easy in this case as you just copy the image to the clipboard (pasteboard) and then the user pastes it wherever they want.

Now I'm developing a Android IME, and need to know of a way to know what application the user is currently typing in?

So can someone point me in the direction? I've learned a lot about Android development over the last week, and my head is kind of swimming from reading so much of the documentation, especially with ContentProvider and having images share to the Android SMS application correctly.

Paul Ratazzi
  • 6,289
  • 3
  • 38
  • 50
Jacob Boyd
  • 672
  • 3
  • 19

2 Answers2

0

Try the below code to get the package name:

ActivityManager mActivityManager = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
if(Build.VERSION.SDK_INT > 20){
     String mPackageName = mActivityManager.getRunningAppProcesses().get(0).processName;
}
else{ 
     String mPackageName = mActivityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
}
kgandroid
  • 5,507
  • 5
  • 39
  • 69
  • I was play with the Activity and Package managers, but I haven't gotten any information from them other than information about my own IME service in the debugger.....AND this makes sense to me from a security stand point and keeping the applications on a device sandboxed. I just can't figure out how they push, or share, data with whatever application is being used. If I am using SMS it shares the images with the SMS application, if I am using gmail it shares with gmail and etc. If I don't include a chooser with my intent it auto. shares to the SMS application. – Jacob Boyd Sep 15 '16 at 19:33
  • Ok, thanks to this [POST](http://stackoverflow.com/questions/6827407/how-to-customize-share-intent-in-android/9229654#9229654) I am able to get the list of applications, now I just need to think of a way to find the correct application from the list – Jacob Boyd Sep 15 '16 at 19:50
0

I wanted to post the answer to this question in case someone is in need of the solution. First you need to use the EditorInfo class of the InputMethod to get the packageName of the current InputConnetion using :

EditorInfo editorInfo = getCurrentInputEditorInfo();
String inputConnectionPackageName = editorInfo.packageName;

Then you need to query your intent activities to see what applications the user has that will handle the intent you are trying to send. Using :

PackageManager pManager = getPackageManager();
    List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();
    mApps = pManager.queryIntentActivities(intent,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED);

Then all you will need to do is create a For loop and compare:

for (int i = 0; i < mApps.size()-1; i++) {
        ResolveInfo info = mApps.get(i);

        if (info.activityInfo.packageName.equalsIgnoreCase(editorInfo.packageName)) {
            intent.setPackage(info.activityInfo.packageName);
            //send out the intent
            startActivity(intent);
        }
    }
Jacob Boyd
  • 672
  • 3
  • 19