简体   繁体   English

如何使广播接收器监听Android中特定应用的事件?

[英]How to make broadcast receiver to listen event for particular app in android?

I am developing an app which contain list of app, when user click of particular app from list shown to him he will directed to website from which he can download that app to listen app is successfully download I add a broadcast receiver in android menifest.xml.The problem is that broadcast receiver listen if any app downloaded in system as well as from my app also.what I want broadcast receiver should listen only events from app only when my app is open as well as closed. 我正在开发一个包含应用程序列表的应用程序,当用户从显示给他的列表中单击特定应用程序时,他将定向到可以从该网站下载该应用程序以收听应用程序的网站。我在android menifest.xml中添加了广播接收器问题是广播接收器是否会在系统以及从我的应用程序中下载的任何应用程序都监听。我要广播接收器仅在打开和关闭我的应用程序时才应该监听来自应用程序的事件。

here is my java code:- 这是我的Java代码:-

public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    // Check if the application is install or uninstall and display the message accordingly
    if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
        // Application Install
        Log.e("Package Added:-", intent.getData().toString());

    } else if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {

        Log.e("Package Removed:-", intent.getData().toString());
    } else if (intent.getAction().equals("android.intent.action.PACKAGE_REPLACED")) {
        Log.e("Package Replaced:-", intent.getData().toString());
    }


}

} }

here is my xml code:- 这是我的xml代码:-

<receiver android:name=".Receiver">
        <intent-filter android:priority="100">
            <action android:name="android.intent.action.PACKAGE_INSTALL"/>
            <action android:name="android.intent.action.PACKAGE_ADDED"/>
            <action android:name="android.intent.action.PACKAGE_REMOVED"/>
            <data android:scheme="package"/>
        </intent-filter>
    </receiver>

kinldy help me I am trying from a week plsss. kinldy帮我,我从一周开始尝试。

There is no way for you to know whether the package is installed by your app just with the information in the Intent received by your BroadcastReceiver . Intent BroadcastReceiver收到的Intent的信息,您无法知道应用程序是否安装了该软件包。 Technically, your app isn't installing anything, the system does that (third party apps do not have the privilege to install other apps). 从技术上讲,您的应用没有安装任何东西,系统会这样做(第三方应用没有特权安装其他应用)。

You will have to check the package name from the Intent data and compare it to apps the user downloaded with your app. 您将必须从Intent数据中检查包名称,并将其与用户随您的应用下载的应用进行比较。 That's the best you can do. 那是你所能做的最好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM