如果没有互联网,则告知用户连接它以进行进一步的处理

时间:2015-12-13 04:58:08

标签: java android eclipse

我正在开发一个应用程序,我必须通知用户您没有连接到互联网以进行进一步处理。 当我们点击任一按钮时,我有两个按钮接受和拒绝它应该通知连接到互联网

private void initializeClickListeners() {
    acceptB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final int checkedOrders = checkedCheckboxArray.size();
            Log.v(TAG,"size of the checked orders is: "+checkedOrders);

            String msgOrdersText = "Order";
            if(checkedOrders == 0)
            {
                Toast.makeText(getActivity(), "No Order Selected", Toast.LENGTH_SHORT).show();
            }
            else if(checkedOrders != 0 )
            {
                AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());

                if(checkedOrders==1)
                {
                    msgOrdersText = "Order";
                }
                if(checkedOrders>1)
                {
                    msgOrdersText = "Orders";
                }
                adb.setTitle("Accept "+msgOrdersText);
                adb.setMessage("Do you want to deliver "+checkedOrders+" "+msgOrdersText);
                adb.setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        //Log.v(TAG,"Adding orders to current list on Acceptance");

                        int oId;
                        DatabaseHelper db = new DatabaseHelper(getActivity());
                        db.getReadableDatabase();
                        Cursor cursor = db.getNewOrders();
                        Log.v(TAG,"Count: "+cursor.getCount());
                        StringBuilder orderIds = new StringBuilder();
                        for(int i:checkedCheckboxArray)
                        {
                            cursor.moveToPosition(i);
                            //oId = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.ORDER_ID));
                            oId = cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseHelper.ORDER_ID));
                            db.updateOrderStatus(oId, "A");
                            //Log.v(TAG,"ORDER id: "+oId);
                            orderIds.append(oId+"$");
                        }


                        Log.v(TAG,"Selected orders in string: "+orderIds);
                        UpdateSelectedOrdersStatusAsyncTask updateStatus = new UpdateSelectedOrdersStatusAsyncTask();
                        updateStatus.execute(orderIds.toString(),"1");

                        cursor.close();
                        db.close();
                        Toast.makeText(getActivity(), checkedOrders+" order accepted", Toast.LENGTH_SHORT).show();

                        // set the selected orders to none
                        checkedCheckboxArray.clear();

                        // set the state of checkbox for all the items in list to false
                        for(int i=0;i<checkedState.length;i++)
                        {
                            checkedState[i] = false;
                        }


                        // refill the list adapter
                        fillAdapter(0);
                        // update the order count notification in menu bar
                        setNotificationCounter();

                    }
                });
                adb.setNegativeButton("No",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.v(TAG,"Orders not added to the current list");
                        //checkedCheckboxArray.clear();
                    }
                });
                AlertDialog ad = adb.create();
                ad.show();
            }
        }
    });


    rejectB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final int checkedOrders = checkedCheckboxArray.size();
            Log.v(TAG,"size of the checked orders is: "+checkedOrders);
            String msgOrdersText = "Order";
            if(checkedOrders == 0)
            {
                Toast.makeText(getActivity(), "No Order Selected", Toast.LENGTH_SHORT).show();
            }
            else if(checkedOrders != 0 )
            {
                AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
                if(checkedOrders==1)
                {
                    msgOrdersText = "Order";
                }
                if(checkedOrders>1)
                {
                    msgOrdersText = "Orders";
                }
                adb.setTitle("Reject "+msgOrdersText);
                adb.setMessage("Do you want to Reject "+checkedOrders+" "+msgOrdersText);
                adb.setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.v(TAG,"Adding orders to current list");
                        int oId;

                        DatabaseHelper db = new DatabaseHelper(getActivity());
                        db.getReadableDatabase();

                        Cursor cursor = db.getNewOrders();
                        Log.v(TAG,"Count: "+cursor.getCount());
                        StringBuilder orderIds = new StringBuilder("");
                        for(int i:checkedCheckboxArray)
                        {
                            cursor.moveToPosition(i);
                            oId = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.ORDER_ID));
                            db.updateOrderStatus(oId, "R");
                            orderIds.append(oId+"$");
                        }
                        cursor.close();
                        db.close();
                        Log.v(TAG,"Selected orders in string: "+orderIds);
                        UpdateSelectedOrdersStatusAsyncTask updateStatus = new UpdateSelectedOrdersStatusAsyncTask();
                        updateStatus.execute(orderIds.toString(),"2");
                        Toast.makeText(getActivity(), checkedOrders+" order rejected/deleted", Toast.LENGTH_SHORT).show();

                        checkedCheckboxArray.clear();
                        // set the state of checkbox for all the items in list to false
                        for(int i=0;i<checkedState.length;i++)
                        {
                            checkedState[i] = false;
                        }
                        fillAdapter(0);
                        setNotificationCounter();
                    }
                });


                adb.setNegativeButton("No",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.v(TAG,"Orders not added to the current list");
                    }

                });

                AlertDialog ad = adb.create();
                ad.show();
            }
        }
    });

现在我正在设置我的设备互联网状态代码

    public class DeviceInternetStatus {

    private static final String TAG = "Buzz";
    private static DeviceInternetStatus instance = new DeviceInternetStatus();
    static Context context;
    ConnectivityManager connectivityManager;
    NetworkInfo wifiInfo, mobileInfo;
    boolean connected = false;


    public static DeviceInternetStatus getInstance(Context ctx) {
        context = ctx.getApplicationContext();
        return instance;
    }

    public boolean isOnline() {

        try {
            connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            Log.v(TAG,"Type of Network(Device Internet Status): "+networkInfo);
            connected = networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
            return connected;
        } catch (Exception e) {
            Log.v("Buzz","Connectivity Exception"+ e.toString());
        }
        return connected;
    }

1 个答案:

答案 0 :(得分:1)

您需要创建一个传递OnItemClickListener

对象的构造函数 像这样,

static Class<? extends OnItemClickListener> cls;
public static DeviceInternetStatus getInstance(OnItemClickListner listner) {
    cls=listener.getClass();
    return instance;
}