尝试这个代码:

你的互联网丢失类的broadcastreceiver类:

public class InternetLostReceiver extends BroadcastReceiver{

@Override

public void onReceive(Context context, Intent intent) {

context.sendBroadcast(new Intent("INTERNET_LOST"));

}

}

在你的活动中添加这个呼叫广播:

public class TestActivity extends Activity{

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

registerReceiver(broadcastReceiver, new IntentFilter("INTERNET_LOST"));

}

BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

// internet lost alert dialog method call from here...

}

};

@Override

protected void onDestroy() {

super.onDestroy();

unregisterReceiver(broadcastReceiver);

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐