限制广播接收器应用接收器

2023-09-03 20:53:25 作者:少年未老爱已荒凉

我工作的广播接收器,并停留在一个问题。

I am working on broadcast receiver and stuck in a problem.

我收到清单文件广播接收器。

I am receiving a broadcast receiver in Manifest file.

<receiver class=".MyClass" android:name=".MyClass">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
            </intent-filter>
    </receiver>

这是工作的罚款,这是调用MyClass的,每当有变化的连通性。

this is working fine and it is calling MyClass whenever there is change in connectivity.

现在的问题是,每当我的应用程序没有运行还是这个类将接收广播接收器。我想,只要在​​应用程序运行它来接收。

Now the problem is whenever my application is not running still this class will receive broadcast receiver. I want it to receive whenever the application is running.

我试了一下在该类文件扩展BroadcastReceiver的登记和注销广播和它的作品。但我想达到同样的舱单文件。

I tried it by extending BroadcastReceiver registering and unregistering broadcast in that class file and it works. But i want to achieve the same by Manifest file.

我的问题将会解决,如果它没有接收到应用时不会打开任何东西。

My problem will solve if it is not receiving anything when application is not opened.

推荐答案

你在说什么是不可能的。有清单中的意图过滤器的整个目的是能够接受,无论您的应用程序正在运行的意图。做你想要的唯一方法是通过注册/从code注销接收器[registerReceiver]

What you are talking is not possible. The whole purpose of having the intent filter in the manifest is being able to receive the intent whether or not your application is running. The only way to do what you want to is by registering/unregistering the receiver from the code [registerReceiver]