title: How to finish activity from service class in android?
body: <p>I am developing one application in that i want to get data from server in background.I am getting data in background using service.Now i want to handle network exception,if no network i want to show one alert dialog and finish application. Here i have problem that is finishing activity in service class
  shows exception ClassCastExcetption..Exception raised here(((Activity) mContext).finish())i tried so much but i unable get this please any one solve my problem</p>
  
  <pre><code>public class InitialRequestService extends Service{
  
      @Override
      public IBinder onBind(Intent intent) {
          // TODO Auto-generated method stub
          return null;
      }
      @Override
      public void onCreate() {
          // TODO Auto-generated method stub
          super.onCreate();   
          new InitialRequestData(InitialRequestService.this).execute();
      }   
  
      public class InitialRequestData extends AsyncTask&lt;String, Void, Void&gt; {
  
          public InitialRequestData(Context context) {
              this.mContext = context;
          }
  
          @Override
          protected void onPreExecute() {
              // TODO Auto-generated method stub
              super.onPreExecute();
              isNetworkAvailable = CheckNetWork.isConnectedToInternet(mContext);
          }
  
          @Override
          protected Void doInBackground(String... params) {  
             //
          }
  
          @Override
          protected void onPostExecute(FeatureResult result) {
              // TODO Auto-generated method stub
              super.onPostExecute(result);
  
              if(result==null || isNetworkAvailable==false){
  
                  /*String message="Unable to connect to the server, please try again";
                  FieldsValdations.AlertErrorMessage(mContext, message, "Network failure");*/
  
                  final AlertDialog alert = new AlertDialog.Builder(mContext).create();
                  alert.setTitle("Network failure"); 
                  alert.setMessage("Unable to connect to the server, please try again");
                  alert.setButton("OK", new DialogInterface.OnClickListener() {
  
                      public void onClick(DialogInterface dialog, int which) {
  
                          alert.dismiss();
                          ((Activity) mContext).finish(); 
                      }
                  });
  
              alert.setCancelable(false);
              alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
              alert.show();
              }               
          }
      }
  }
  </code></pre>