我如何从python kivy/jnius中的服务中使用startActivity方法? [英] How I can use startActivity method from service in python kivy/jnius?

查看:203
本文介绍了我如何从python kivy/jnius中的服务中使用startActivity方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的android应用程序(python 2.7和kivy)中的服务开始一项活动.我为此使用了startActivity方法,但是它不起作用.

I want to start an activity from a service in my android application (python 2.7 & kivy). I use startActivity method for it but it's not work.

当我运行应用程序并键入"buildozer android logcat" 时,我看到了:

When I run the app and type "buildozer android logcat", I see this:

文件"jnius_export_class.pxi",行900,在jnius.jnius.JavaMultipleMethod .__调用__(jnius/jnius.c:24581)中 JavaException:没有与您的参数匹配的方法

File "jnius_export_class.pxi", line 900, in jnius.jnius.JavaMultipleMethod.__ call__ (jnius/jnius.c:24581) JavaException: No methods matching your arguments

我的服务代码的一部分:

Part of my service code:

    from jnius import autoclass, cast


    PythonService = autoclass("org.renpy.android.PythonService")
    activity = cast("android.app.Service", PythonService.mService)
    manager = activity.getPackageManager()
    Intent = autoclass("android.content.Intent")
    intent = manager.getLaunchIntentForPackage("com.MyTest.AndroidTest")
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    activity.startActivity(intent) ### Exception in this line

如果我将其移至主要活动并使用PythonActivity.mActivity,它将起作用.但是我需要精确地从 service 运行此代码. 请帮忙.

If I move it to the main activity and use PythonActivity.mActivity, it works. But I need to run this code precisely from service. Please help.

推荐答案

首先,对于kivy,它使用org.kivy.android.PythonActivity而不是renpy. (您正确启动了活动;)) 从网上某处拍摄.我只是不记得在哪里.学分应该归其他人所有.无论如何,这是示例代码.

Firstly for kivy it goes org.kivy.android.PythonActivity instead of renpy. (You launching activity right ;) ) Taken from somewhere online. I just can't remember where. Credits should go to other person. Anyways, here it is a sample code.

PythonActivity =  autoclass("org.kivy.android.PythonActivity") 
Intent = autoclass('android.content.Intent')
pm = autoclass('android.content.pm.PackageManager')
activity = PythonActivity.mActivity
pm_ = activity.getPackageManager()
array_pkg = pm_.getInstalledApplications(pm.GET_META_DATA).toArray()

print "\ninstalled app:"
selected_pkg = []
list_exitsting = []
for i in array_pkg:
    if "/data/app/" not in getattr(i, "publicSourceDir"):
        continue
    selected_pkg.append(i)
    print "packageName = " + getattr(i, "packageName")
    list_exitsting.append(getattr(i, "packageName"))
print "\nget app intent"

app_to_launch = "com.google.android.youtube"

for i in selected_pkg:
    if app_to_launch == getattr(i, "packageName"):
        app_intent = pm_.getLaunchIntentForPackage(getattr(i, "packageName"))
        app_intent.setAction(Intent.ACTION_VIEW)
        app_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        print "launch app: " + app_to_launch
        activity.startActivity(app_intent)

这篇关于我如何从python kivy/jnius中的服务中使用startActivity方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆