如何检测在android

时间:2016-03-05 07:57:57

标签: android keyboard

我正在开发一个Android自定义键盘,我想发送胶粘物给我的朋友,例如我的键盘上的环聊。我为此添加了一些png。

当我使用android的share api时,我必须首先选择要与之分享的应用程序。是否可以从键盘检测当前打开的应用程序?例如,如果我正在与环聊聊天,那么我想调用意图告诉它与“com.something.hangouts”分享

如果我在FB messenger,然后告诉它“com.something.facebook.messenger”等...

我试着这样做:

fun getOpenedApplication() {
    var am = this.getSystemService(ACTIVITY_SERVICE) as ActivityManager;

    var l = am.getRecentTasks(1,
            ActivityManager.RECENT_WITH_EXCLUDED);
    var i = l.iterator();

    var pm = this.packageManager;

    while (i.hasNext()) {
        try {
            var intent = i.next().baseIntent;
            var list = pm.queryIntentActivities(intent,
                    PackageManager.MATCH_DEFAULT_ONLY);

            var c = pm.getApplicationLabel(pm.getApplicationInfo(
                    list[0].activityInfo.packageName,
                    PackageManager.GET_META_DATA));

            Toast.makeText(this, "Application name: " + c.toString(),
                    Toast.LENGTH_LONG).show();

        } catch (e: Exception) {
            Toast.makeText(this,
                    "Application name not found: " + e.toString(),
                    Toast.LENGTH_LONG).show();
        }
    }
}

但是效果不好......它会发回启动器或键盘。 (随机)

我尝试从最近的应用列表中获取第一个应用,但它无法正常工作

6 个答案:

答案 0 :(得分:9)

这是非常容易的任务。

- [x] some item
- [ ] another item with meta info [//]: # (attempt at meta info as inline comment)
- [ ] using @ig0774's recomendation [](with an inline comment hidden inside an empty link)

[//]: # (This may be the most platform independent comment)
[//]: # (https://stackoverflow.com/questions/4823468/comments-in-markdown)
[](and another comment down here too using the empty link method)

InputMethodService类的方法。

答案 1 :(得分:4)

对于Android L及更高版本,请使用:

ActivityManager activityManager = (ActivityManager) newContext.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runAppProcessesList = activityManager.getRunningAppProcesses();
for (RunningAppProcessInfo runAppProcess : runAppProcessesList) {
    if (runAppProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
        Log.d("current foreground App", runAppProcess.processName);
    }
}

答案 2 :(得分:4)

要获取当前正在运行的应用程序,请使用以下函数为Android版本直到kitkat

public String getRunningAppPackageName()
{
    ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = activityManager.getRunningTasks(1);
    RunningTaskInfo p = (RunningTaskInfo) tasks.get(0);

    return p.baseActivity.getPackageName();}

要获取当前正在运行的应用程序,请在kitkat

之上使用以下Android版本的功能
     public String getRunningAppPackageName()
{
    ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = activityManager.getRunningAppProcesses();


    return activityManager.getRunnningAppProcesses().get(0).processName;}

答案 3 :(得分:1)

您也可以使用辅助功能服务,但为此您需要将用户重定向到辅助功能设置以将其启用。示例代码如下所示,以获取刚刚打开的应用程序包。

 public class MyAccessibilityService extends AccessibilityService {

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
         String packageName = event.getPackageName();
         if(packageName.equals("com.sample"))
            //Do whatever you want  
    }}

您可以在此处详细了解Accessibility Services

答案 4 :(得分:1)

检查此代码

 /**
     * @param context
     * @return
     * @throws PackageManager.NameNotFoundException
     */
    public String getRunningPackageName(Context context) throws PackageManager.NameNotFoundException {
        ActivityManager mgr = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        String packagename = "";
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            List<ActivityManager.AppTask> tasks = mgr.getAppTasks();
            for (ActivityManager.AppTask task : tasks) {
                packagename = task.getTaskInfo().baseIntent.getComponent().getPackageName();
                if (packagename.equalsIgnoreCase(context.getPackageName().toString())) {
                    break;
                }
            }
        } else {
            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningTaskInfo> alltasks = activityManager.getRunningTasks(1);
            packagename = alltasks.get(0).topActivity.getPackageName();
        }
        return packagename;
    }


    private void yourMethodName(Context context, Bundle notifyBundle)  { 
            boolean isActivityFound = false;
            String packageName = "";
            try {
                packageName = getRunningPackageName(context);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
     if (packageName.equalsIgnoreCase(context.getPackageName().toString())) {
            isActivityFound = true;
        }
    }

答案 5 :(得分:0)

您可以使用来自activitymanager的getRunningAppProcesses api,但是当您的结果超过1MB时(基本上在超过30-35个应用程序正在运行的情况下),您将最终得到'TransactionTooLargeException'。这是Android实现无法将绑定数据传输超过1 MB。