2014-04-22 2 views
2

응용 프로그램 클래스를 확장하는 클래스가 있습니다. 이 클래스에서는 인터넷 연결을 확인하고 웹 서비스를 호출합니다. 다음은 Android : 응용 프로그램 클래스에서 응용 프로그램을 강제 종료하십시오.

내가 확인하기 위해 사용하고있는 방법입니다 :

public static boolean isInternetConnected(Context mContext) { 
     ConnectivityManager connec = (ConnectivityManager) mContext 
       .getSystemService(Context.CONNECTIVITY_SERVICE); 

     if (connec != null 
       && (connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) 
       || (connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)) { 
      return true; 
     } 
     return false; 

    } 

내가 가까이 응용 프로그램을 강제하려면 인터넷 연결이 없습니다. 그것을하는 방법?

대체 솔루션이 있습니다. 인터넷에 연결되어 있지 않으면 api 프로세스를 호출하는 것을 건너 뛸 수 있으며, 첫 번째 활동이 시작되면 바로 시작하고 끝내기를 기다릴 수 있습니다.

Application 클래스에서 수행 할 수 있습니까?

+0

왜 응용 프로그램을 닫으시겠습니까 ?? 당신은 간단하게 응용 프로그램을 닫거나 종료 할 수 있습니다. – Sree

+0

응용 프로그램 클래스에서 응용 프로그램을 닫거나 종료 할 수 있습니까? –

+1

finish() 호출하여 – Sree

답변

1

왜 응용 프로그램을 강제 종료 하시겠습니까? 그러한 요청을 처음 듣습니다.

어떤 인터넷이없는 경우, 당신은 단지이 같은 활동을 닫을 수 있습니다 :

if (!isInternetConnected(context)){ 
    finish(); 
} 
+0

예 활동을 닫을 수는 있지만 저를 오해라고 생각합니다. 나는 현재 Application 클래스가 아니며 액티비티가 아니다. Application 클래스에서 finish() 메서드를 호출 할 수 없습니다. –

0

이이보다 효율적으로 사용이를 ...

public boolean isOnline() 
{ 
    ConnectivityManager cm = 
     (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
    if (netInfo != null && netInfo.isConnectedOrConnecting()) 
    { 
     return true; 
    } 


    // Toast.makeText(getBaseContext(), "Internet is not Connected", Toast.LENGTH_LONG).show(); 
    alertwifi(); 
    return false; 
} 





public void alertwifi() 
    { 
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
      context); 

     // set title 
     alertDialogBuilder.setTitle("NO Internet Connection!!"); 

     // set dialog message 
     alertDialogBuilder 
      .setMessage("Click below to turn on Wifi or Enable Data pack") 
      .setCancelable(false) 
      .setPositiveButton("Wifi",new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int id) { 
        // if this button is clicked, close 
        // current activity 
        wifi(); 
       } 
       }) 
       .setNeutralButton("GPRS",new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int id) { 
        // if this button is clicked, close 
        // current activity 
        datapack(); 
       } 
       }) 
      .setNegativeButton("Back",new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int id) { 
        // if this button is clicked, just close 
        // the dialog box and do nothing 

        exit(); 


       } 
      }); 





public void exit() 
{ 
    Intent intent = new Intent(Intent.ACTION_MAIN); 
    intent.addCategory(Intent.CATEGORY_HOME); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(intent); 
} 


// create alert dialog 
     AlertDialog alertDialog = alertDialogBuilder.create(); 

     // show it 
     alertDialog.show(); 
    } 
1

당신은 파생 클래스 응용 프로그램에서 호출 할 수 있습니다;

android.os.Process.killProcess(android.os.Process.myPid());