开发者

How to obtain package name of an application in Android?

I want to display package name of an application in a BroadcastReceiver whose intent is receiv开发者_如何学运维ed in that receiver. I used the intent.getPackage() method but it is not working. I also tried to display that package name using context object which is a parameter of onReceive() method to display package name but it is not working.

Please help me. Thanks in advance.


This way :

ApplicationInfo packageInfo = this.getApplicationContext().getApplicationInfo();
packageName = packageInfo.packageName;

EDIT : This is not working from a broadcast receiver, I misread the question.

Reading this page : http://developer.android.com/reference/android/content/Intent.html Intent.getPackage doesn't give you the package that called but the one that the intent is limited to.

If I understand this page clearly, Intents do not provide the package name or anything allowing you to identify it.

If you are the one sending the intent(but I believe you are not), you could send the package name in the intent in a parcelable that you'd be able to retrieve in your broadcast receiver. Otherwise, I don't think it is possible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜