Khizar Khan Khizar Khan - 3 months ago 7
Android Question

inform user if internet is not present to connect it to do the further process

I am developing an app in which i have to inform user that you are not connected to internet for further process.
I have two button accept and reject when we click on either button it should notify to connect to internet

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();
}
}
});


Now i am putting my device internet status code

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;
}

Answer

You need to create a constructor passing the object of OnItemClickListener

like this,

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