为什么BroadcastReceiver的工作,即使应用程序在后台?应用程序、后台、工作、BroadcastReceiver

2023-09-05 06:36:53 作者:滿足征服慾

我在我的应用程序中使用的BroadcastReceiver检查网络连接和我展示一个警告对话框,如果连接丢失。它工作正常。但我的问题是,BroadcastReceiver的工程,即使我的应用程序中的backgroung。因此,对话框弹出时,即使用户在其他应用程序丢失网络连接。这完全毁了我的应用程序。

I am checking Internet connectivity in my app using BroadcastReceiver and I show an alert dialog if the connection is lost. It works fine. But my problem is that BroadcastReceiver works even if my app is in backgroung. So dialog pops up when internet connection is lost even if user is in some other app. This is totally ruining my app.

有没有人有任何想法如何限制广播接收器只在应用程序?

Has anyone got any idea how to restrict Broadcast receiver in the app only?

下面是我的BroadcastReceiver的:

Here is my BroadcastReceiver :

 public class ConnectivityChangedReceiver extends BroadcastReceiver{

    @Override
    public void onReceive( Context context, Intent intent )
     {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
    } else {
        try{
            Intent i=new Intent(context, InternetDialogActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        } catch(Exception e){
            e.printStackTrace();
        }
    }
  }
}

和活动是:

 public class InternetDialogActivity extends Activity implements OnClickListener{
   @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.internet_dialog_box);
      getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
      Button retryButton = (Button) findViewById(R.id.retryInternetButton);
      retryButton.setOnClickListener(this);
    }

   public boolean checkConnectivity(){
       ConnectivityManager connectivityManager   = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
           NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

       if (networkInfo!=null && networkInfo.isConnected()) {
           finish();
           return true;
       } else {
           Intent intent = getIntent();
           finish();
           startActivity(intent);
           return false; 
      }
   }

   @Override
   public void onClick(View view) {
      switch(view.getId()){
         case R.id.retryInternetButton:
             try{
                 checkConnectivity();
             } catch(Exception e){
                 e.printStackTrace();
             }
             break;
       }
   }
   }

下面是我如何申报接收器和活动清单:

Here is how I declared receiver and activity in manifest:

  <receiver android:name="com.lisnx.service.ConnectivityChangedReceiver"
        android:label="NetworkConnection">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
        </intent-filter>
  </receiver>

  <activity android:name="com.lisnx.activity.InternetDialogActivity"
        android:configChanges="orientation|keyboardHidden"
        android:theme="@android:style/Theme.Dialog" />

我已阅读,我们可以限制的BroadcastReceiver通过不宣布其清单中的应用程序中工作。但我不知道如何将接收器的工作呢?请帮我。我被困在是很糟糕。感谢名单提前。

I have read that we can restrict BroadcastReceiver to work within the app by not declaring it in the manifest. But I don't know how will receiver work then? Please help me. I am stuck on it badly. Thanx in advance.

推荐答案

A BroadcastReceiver工作时,应用程序在后台,因为该接收器接收到该事件是全局发送,并且每个应用程序被登记到对这些收听,不管它是否正在运行。

A BroadcastReceiver works when the app is in the background because the event that the receiver picks up are sent globally, and each app is registered to listen in on these, regardless of whether or not it is running.

要解决这个问题,在你的的BroadcastReceiver 的onReceive code,请检查您的应用程序是在前台。

To deal with this, in your BroadcastReceiver's onReceive code, check if your app is in the foreground.

有一个 - 且只有一个,我知道 - 始终有效的方法来做到这一点。你需要保持你的停顿跟踪/恢复对您的应用程序的行为。确保您在每活动检查。

There is one--and only one that I know of--consistently effective method to do this. You need to keep track of your pause/resume actions for your application. Ensure that you check this in every activity.

目前在这个答案一些示例code (溶液#1)。在你的情况,你会想检查 MyApplication.isActivityVisible()==真从你的的BroadcastReceiver 。

There is some sample code in this answer (solution #1). In your case, you would want to check MyApplication.isActivityVisible() == true as a validation before doing anything from your BroadcastReceiver.