完成活动从广播接收器开始

时间:2017-10-03 06:38:15

标签: android broadcastreceiver

在我的Android应用程序中,我想使用广播接收器来检查网络状态。 到目前为止,我已经创建了一个广播接收器,

    public class NetworkChangeListener extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if(intent.getExtras() != null)
        {
            final ConnectivityManager connectivityManager=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
            final NetworkInfo ni=connectivityManager.getActiveNetworkInfo();

            if(ni!=null && ni.isConnectedOrConnecting())
            {
                //here i want to close the activity which is open from the else block
            }
            else
            {
                Intent mIntent = new Intent(context,NetworkDialog.class);
                mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(mIntent);

            }
        }
    }
}

正如您在上面的代码中看到的,我在else块中调用Networkdialog活动。 我想提一下, NetworkDialog.class是一个活动,但我已将其样式设置为Dialog,使用清单文件中的以下代码

<activity
            android:name=".NetworkDialog"
            android:excludeFromRecents="true"
            android:label="You are Offline"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.AppCompat.Light.Dialog.Alert"
            android:windowSoftInputMode="stateHidden" />

现在,问题是,当网络不可用时,此活动正在成功调用, 但是当用户连接到互联网时,我无法完成此活动。

(注意 - 众所周知,广播接收器全局工作,意味着它即使您的应用程序不活动也能正常工作,所以为了防止这种情况,我没有在清单文件中注册接收器,我正在注册并取消注册它在我想要使用那个接收器的活动中)

所以我只想问一下,如何在第一个if块中完成该对话框活动,这是从else块中的intent调用的

以下是我的更新代码

NetworkDialog.java

    public class NetworkDialog extends AppCompatActivity {
    public IntentFilter filter;

    BroadcastReceiver receiver=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

                Toast.makeText(context,"Finish Activity here",Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.network_dialog);
        setFinishOnTouchOutside(false);

        filter=new IntentFilter();
        filter.addAction("com.hello.action");
        registerReceiver(receiver,filter);

    }
}

广播接收器类

    public class NetworkChangeListener extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if(intent.getExtras() != null)
        {
            final ConnectivityManager connectivityManager=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
            final NetworkInfo ni=connectivityManager.getActiveNetworkInfo();

            if(ni!=null && ni.isConnectedOrConnecting())
            {
                Intent local=new Intent();
                local.setAction("com.hello.action");
                context.sendBroadcast(local);
            }
            else
            {
                Intent mIntent = new Intent(context,NetworkDialog.class);
                mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(mIntent);

            }
        }
    }
}

我没有更改任何其他文件

1 个答案:

答案 0 :(得分:0)

   public class NetworkChangeListener extends BroadcastReceiver
 {
    @Override
   public void onReceive(Context context, Intent intent)
 {
    if(intent.getExtras() != null)
    {
        final ConnectivityManager connectivityManager=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo ni=connectivityManager.getActiveNetworkInfo();

        if(ni!=null && ni.isConnectedOrConnecting())
        {
            //here You need to call local broadcast receiver
            // Like this 
               Intent local = new Intent();
               local.setAction("com.hello.action");
               sendBroadcast(local);


        }
        else
        {
            Intent mIntent = new Intent(context,NetworkDialog.class);
            mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(mIntent);

        }
      }
   }
}

代码用于对话活动

   public class YourDialogActivity extends Activity {
     ArrayAdapter<String> adapter;

     @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

      IntentFilter filter = new IntentFilter();

      filter.addAction("com.hello.action");
      registerReceiver(receiver, filter);

    }
        BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
       finish();

      }
    };

    public void finish() {
        super.finish();
        };
   }