How I can use startActivity method from service in python kivy/jnius?

白昼怎懂夜的黑 提交于 2021-02-08 04:29:34

问题


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.

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

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

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.


回答1:


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)


来源:https://stackoverflow.com/questions/42253775/how-i-can-use-startactivity-method-from-service-in-python-kivy-jnius

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!