Advertisement
Guest User

Untitled

a guest
Sep 1st, 2015
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. public class RealService extends Service{
  2.  
  3. private static final String TAG="RealService";
  4. private boolean isRunning=false;
  5. private IBinder mBinder=new MyBinder();
  6. private boolean intenetAccess=false;
  7.  
  8. Context context=this;
  9. private RequestQueue reQueue=null;;
  10.  
  11. private final String url="http://www.google.com";
  12.  
  13. public boolean SendRequest()
  14. {
  15. reQueue=Volley.newRequestQueue(this);
  16. StringRequest request=new StringRequest(com.android.volley.Request.Method.GET,
  17. url,
  18. new Response.Listener<String>() {
  19.  
  20. @Override
  21. public void onResponse(
  22. String response) {
  23.  
  24.  
  25. intenetAccess=true;
  26. Log.i(TAG,"intenetAccess=true");
  27. }
  28. },
  29.  
  30. new Response.ErrorListener() {
  31.  
  32. @Override
  33. public void onErrorResponse(
  34. VolleyError error) {
  35.  
  36. intenetAccess=false;
  37.  
  38. }
  39. });
  40.  
  41. try{
  42. reQueue.add(request);
  43. }
  44. catch(Exception e){}
  45.  
  46. return intenetAccess;
  47.  
  48. }
  49.  
  50. @Override
  51. public void onCreate() {
  52. super.onCreate();
  53. Log.i(TAG, "Service onCreate");
  54.  
  55. isRunning=true;
  56.  
  57. }
  58.  
  59.  
  60.  
  61. @Override
  62. public IBinder onBind(Intent intent) {
  63. Log.i(TAG, "Service onBind");
  64. return mBinder;
  65. }
  66.  
  67. @Override
  68. public void onRebind(Intent intent) {
  69. Log.i(TAG, "Service onRebind");
  70. super.onRebind(intent);
  71. }
  72.  
  73. @Override
  74. public boolean onUnbind(Intent intent) {
  75. Log.i(TAG, "Service onUnBind");
  76. return true;
  77. }
  78.  
  79. @Override
  80. public void onDestroy() {
  81.  
  82. isRunning=false;
  83. intenetAccess=false;
  84. Log.i(TAG, "Service onDestroy");
  85. super.onDestroy();
  86. }
  87.  
  88.  
  89.  
  90. public class MyBinder extends Binder
  91. {
  92. RealService getService()
  93. {
  94. return RealService.this;
  95. }
  96. }
  97. }
  98.  
  99. public class MainActivity extends AppCompatActivity{
  100.  
  101. private Button checkbtn;
  102. private Button start_service_btn;
  103. private Button stop_service_btn;
  104.  
  105. RealService realService=new RealService();
  106. boolean serviceBound=false;
  107. boolean internetPresent=false;
  108.  
  109. @Override
  110. protected void onCreate(Bundle savedInstanceState) {
  111. super.onCreate(savedInstanceState);
  112. setContentView(R.layout.activity_main);
  113.  
  114. start_service_btn=(Button) findViewById(R.id.start_service_btn);
  115.  
  116. start_service_btn.setOnClickListener(new OnClickListener() {
  117.  
  118. @Override
  119. public void onClick(View v) {
  120. internetPresent= realService.SendRequest();
  121. if(internetPresent)
  122. {
  123. showAlertDialog(MainActivity.this, "INTERNET ACCESS", "You have internet access");
  124.  
  125. }
  126. else
  127. {
  128. showAlertDialog(MainActivity.this, "NO INTERNET ACCESS", "You do not have internet access");
  129.  
  130. }
  131. }
  132. });
  133.  
  134. stop_service_btn=(Button) findViewById(R.id.stop_service_btn);
  135.  
  136. stop_service_btn.setOnClickListener(new OnClickListener() {
  137.  
  138. @Override
  139. public void onClick(View v) {
  140. Intent intent=new Intent(MainActivity.this,RealService.class);
  141. stopService(intent);
  142. internetPresent=false;
  143. }
  144. });
  145.  
  146.  
  147. public void showAlertDialog(Context context, String title,String message) {
  148.  
  149. AlertDialog alertDialog=new AlertDialog.Builder(context).create();
  150. alertDialog.setTitle(title);
  151. alertDialog.setMessage(message);
  152.  
  153. alertDialog.show();
  154.  
  155. }
  156.  
  157. @Override
  158. public boolean onCreateOptionsMenu(Menu menu) {
  159.  
  160. getMenuInflater().inflate(R.menu.main, menu);
  161. return true;
  162. }
  163.  
  164. @Override
  165. public boolean onOptionsItemSelected(MenuItem item) {
  166.  
  167. int id = item.getItemId();
  168. if (id == R.id.action_settings) {
  169. return true;
  170. }
  171. return super.onOptionsItemSelected(item);
  172. }
  173.  
  174. @Override
  175. protected void onStart() {
  176. super.onStart();
  177. Intent intent=new Intent(this,RealService.class);
  178. startService(intent);
  179. bindService(intent, realServiceConnection, Context.BIND_AUTO_CREATE);
  180. }
  181.  
  182. @Override
  183. protected void onStop() {
  184. super.onStop();
  185. if(serviceBound)
  186. {
  187. unbindService(realServiceConnection);
  188. serviceBound=false;
  189. }
  190. }
  191.  
  192. private ServiceConnection realServiceConnection=new ServiceConnection()
  193. {
  194.  
  195. @Override
  196. public void onServiceDisconnected(ComponentName name) {
  197.  
  198. serviceBound=false;
  199. }
  200.  
  201. @Override
  202. public void onServiceConnected(ComponentName name, IBinder service) {
  203.  
  204. MyBinder myBinder=(MyBinder) service;
  205. realService=myBinder.getService();
  206. serviceBound=true;
  207. }
  208. };
  209.  
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement