package com.toobaweb.co;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Toast;
public class Main extends Activity {
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new NetCheck().execute();
}
// چک کردن کانکشن اینترنت کاربر
private class NetCheck extends AsyncTask<String,String,Boolean>
{
private ProgressDialog nDialog;
@Override
protected void onPreExecute(){
super.onPreExecute();
nDialog = new ProgressDialog(Main.this);
nDialog.setTitle("چک کردن کانکشن اینترنت");
nDialog.setMessage("در حال بررسی ...");
nDialog.setIndeterminate(false);
nDialog.setCancelable(true);
nDialog.show();
}
/**
* Gets current device state and checks for working internet connection by trying Google.
**/
@Override
protected Boolean doInBackground(String... args){
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(3000);
urlc.connect();
if (urlc.getResponseCode() == 200) {
return true;
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
@Override
protected void onPostExecute(Boolean th){
if(th == true){
nDialog.dismiss();
Toast.makeText(getApplicationContext(), "اینترنت وصل می باشد .", Toast.LENGTH_LONG).show();
CreateNotification();
}
else{
nDialog.dismiss();
Toast.makeText(getApplicationContext(), "اینترنت قطع می باشد.", Toast.LENGTH_LONG).show();
}
}
}
private void CreateNotification(){
CharSequence renderText = "در حال ساخت نوتیفیکیشن جدید";
CharSequence title = "نوتیفیکیشن";
CharSequence text = "ساخت نوتیفیکیشن جدید";
long time = System.currentTimeMillis();
Context context = getApplicationContext();
Intent notifyIntent = new Intent(this , Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notifyIntent, 0);
final Notification notification = new Notification(R.drawable.ic_launcher , renderText , time);
notification.setLatestEventInfo(context, title, text, contentIntent);
String notyService = Context.NOTIFICATION_SERVICE;
final NotificationManager nManager = (NotificationManager) getSystemService(notyService);
nManager.notify(1, notification);
}
}