如何在android中检查慢速网络连接

时间:2017-06-13 10:09:08

标签: android networking wifi android-connectivitymanager

如果网络连接缓慢,我必须显示一个页面

使用此代码检查网络

   ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    if (info.getType() == ConnectivityManager.TYPE_WIFI) {
        Toast.makeText(MainActivity.this,"wifi",Toast.LENGTH_LONG).show();


        // do something
    } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {


        // check NetworkInfo subtype
        if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS) {
            Toast.makeText(MainActivity.this,"mobile 100kbps",Toast.LENGTH_LONG).show();
            // Bandwidth between 100 kbps and below
        } else if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE) {
            Toast.makeText(MainActivity.this,"mobile 50-100kbps",Toast.LENGTH_LONG).show();

            // Bandwidth between 50-100 kbps
        } else if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_EVDO_0) {
            Toast.makeText(MainActivity.this,"mobile 400-1000kbps",Toast.LENGTH_LONG).show();

            // Bandwidth between 400-1000 kbps
        } else if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_EVDO_A) {
            Toast.makeText(MainActivity.this,"mobile 600-1400kbps",Toast.LENGTH_LONG).show();

            // Bandwidth between 600-1400 kbps
        }

显示wifi网络 但我需要慢速wifi网络的代码。 请帮帮我是否有任何代码来检查慢速wifi网络。

2 个答案:

答案 0 :(得分:2)

使用以下方法检查wifi级别:

Product:        Type:     CalculateFeild:

TV               Small
Computer         Big
HeadPhones       Medium

根据wifi级别或链接速度,您可以决定它是否具有低连接或高连接互联网。

答案 1 :(得分:1)

您可以使用以下代码检查wifi速度

public class TableEditorTest {

    public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        final TableViewer viewer = new TableViewer(shell);
        viewer.getTable().setHeaderVisible(true);
        viewer.getTable().setLinesVisible(true);
        viewer.setContentProvider(new ArrayContentProvider());
        ColumnViewerToolTipSupport.enableFor(viewer);
        viewer.getTable().setToolTipText("");
        TableColumn column = new TableColumn(viewer.getTable(), SWT.NONE);
        column.setText("First Name");
        column.setWidth(100);
        TableViewerColumn firstNameCol = new TableViewerColumn(viewer, column);
        firstNameCol.setLabelProvider(new ColumnLabelProvider(){
            @Override
            public String getText(Object element) {
                Person p = (Person)element;
                return p.getFirstName();
            }
        });
        column = new TableColumn(viewer.getTable(), SWT.NONE);
        column.setText("Last Name");
        column.setWidth(100);
        TableViewerColumn lastNameCol = new TableViewerColumn(viewer, column);
        lastNameCol.setLabelProvider(new ColumnLabelProvider(){
            @Override
            public String getText(Object element) {
                Person p = (Person)element;

                return p.getLastName();
            }
        });
        column = new TableColumn(viewer.getTable(), SWT.NONE);
        column.setText("Actions");
        column.setWidth(100);
        TableViewerColumn actionsNameCol = new TableViewerColumn(viewer, column);
        actionsNameCol.setLabelProvider(new ColumnLabelProvider(){
            @Override
            public String getText(Object element) {
                Person p = (Person)element;

                return p.getLastName();
            }
        });

        Person p1 = new Person();
        p1.setFirstName("George");
        p1.setLastName("Burne");

        Person p2 = new Person();
        p2.setFirstName("Adam");
        p2.setLastName("Silva");

        Person p3 = new Person();
        p3.setFirstName("Nathan");
        p3.setLastName("Cowl");

        List<Person> persons = new ArrayList<Person>();
        persons.add(p1);
        persons.add(p2);
        persons.add(p3);

        viewer.setInput(persons);

        Listener tooltipListener = new Listener() {
            Shell tip = null;
            ArrayList<Hyperlink> arrHypLnk=new ArrayList<Hyperlink>();
            //Label label = null;
            @Override
            public void handleEvent(Event event) {              

                switch (event.type) {
                case SWT.Dispose:
                case SWT.KeyDown:
                case SWT.MouseMove: {
                    if (tip == null) break;
                    tip.dispose ();
                    tip = null;
                    for(Hyperlink h: arrHypLnk){
                        h.dispose();
                    }
                    arrHypLnk.clear();
                    break;
                }
                case SWT.MouseHover: {
                    TableItem item = viewer.getTable().getItem (new Point (event.x, event.y));

                    if (item != null) {
                        if (tip != null  && !tip.isDisposed ()) tip.dispose ();
                        tip = new Shell (shell, SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL);
                        tip.setBackground (display.getSystemColor (SWT.COLOR_INFO_BACKGROUND));

                        GridLayout layout = new GridLayout();
                        layout.numColumns = 1;
                        layout.makeColumnsEqualWidth = true;
                        tip.setLayout (layout);
                        tip.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER));
                        TableItem itemm = (TableItem) event.item;
                        String text = item.getText(event.index);
                        String[] texts= new String[]{"Link1", "Link2", "Link3"};
                        for(String s: texts){
                            final Hyperlink h=new Hyperlink(tip, SWT.NONE);
                            h.setText(s);
                            h.setHref(s);
                            h.addHyperlinkListener(new HyperlinkAdapter(){
                                public void linkActivated(HyperlinkEvent e){
org.eclipse.swt.program.Program.launch(h.getHref().toString());
                                }
                            });
                            arrHypLnk.add(h);
                        }

                        Point size = tip.computeSize (SWT.DEFAULT, SWT.DEFAULT);
                        Rectangle rect = item.getBounds (0);
                        Point pt = viewer.getTable().toDisplay (rect.x, rect.y);
                        tip.setBounds (pt.x, pt.y, size.x, size.y);
                        tip.setVisible (true);
                    }
                }
            }
            }
        };
        viewer.getTable().addListener(SWT.MouseHover, tooltipListener);
        viewer.getTable().addListener(SWT.MouseMove, tooltipListener);
        viewer.getTable().addListener(SWT.KeyDown, tooltipListener);

        shell.open();
        while(!shell.isDisposed())
        {
            if(!display.readAndDispatch())
            {
                display.sleep();
            }
        }
        display.dispose();
    }


    private static class Person
    {
        String firstName;
        String lastName;

        Person()
        {

        }

        public String getFirstName() {
            return firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    }
}

我希望这对你有所帮助。