質問

I've used the search functions and i've read and tried a lot of examples, but I can't find a solution for my problem... So, here we go:

I want to send Intents to only 1 BroadcastReceiver. Therefore I need to create a new Intent and send it to the packageName and the packageClass:

intent.setComponent(new ComponentName(packageName, className));

To find out packageName and className i use this code:

String appInfo = list.get(myPlayer).activityInfo.toString();
Log.d("TAG", "app " + appInfo);

String packageName = list.get(myPlayer).activityInfo.packageName.toString();    
Log.d("TAG", "package " + packageName);

String className = list.get(myPlayer).activityInfo.getClass().getName().toString();
Log.d("TAG", "class " + className);

From logcat I get following results(example for Play Music):

app ActivityInfo{417bd5e0 com.google.android.music.MediaButtonIntentReceiver}
package com.google.android.music
class android.content.pm.ActivityInfo

Isn't this strange? For some reason, I'd expect for the class log only "MediaButtonIntentReceiver" and not "android.content.pm.ActivityInfo"!!!

Does anybody know how to fix this?

役に立ちましたか?

解決

If you're searching for packageName and className for filling ordered broadcast, I really suggest to check content of ResolveInfo object you're probably using

so in you case, if you need to complete content of intent, I may suggest

    ResolveInfo ri = ... // get where you need
    Intent intent = new Intent();
    intent.setComponent(new ComponentName(
        ri.activityInfo.applicationInfo.packageName,
        ri.activityInfo.applicationInfo.className)); 

this should work for you case

Edit: in cases, you obtained ResolveInfo for specific intent, className should be null. In these cases I successfully use ri.activityInfo.name which returns correct className because activityInfo is for one specific activity

他のヒント

Simply put: getClass() doesn't do what your expect it to do.
However using what you have you can get what you want like so:

String appInfo = list.get(myPlayer).activityInfo.toString();
String className = appInfo.substring(appInfo.lastIndexOf(".") + 1, appInfo.length() - 1);
Log.d("TAG", "class " + className);

Notice that packageName is a field of ActivityInfo, not a method. While getClass() is a method, but it's a method of Object, from it's documentation:

Returns the unique instance of Class that represents this object's class.

So you are asking for the name of activityInfo, which is (as you know know) ActivityInfo.

It's not strange at all. Here:

String className = list.get(myPlayer).activityInfo.getClass().getName().toString()

You're getting the class of the ActivityInfo object. I'm not sure exactly what you're trying to do, but that's why you're seeing what you're seeing.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top