1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | package com.technicolor.wifitestapp; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.LinkProperties; import android.net.Network; import android.net.NetworkCapabilities; import android.net.NetworkRequest; import android.net.wifi.WifiManager; import android.os.Build; import android.os.IBinder; import android.util.Log; import android.widget.RemoteViews; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import androidx.core.app.NotificationCompat; public class WifiTestService extends Service { private static String LOG_TAG = "easy" ; private Context mContext; @Nullable @Override public IBinder onBind(Intent intent) { return null ; } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(LOG_TAG, "onStartCommand" ); printAllNetworks(); return START_STICKY; //super.onStartCommand(intent, flags, startId); } private ConnectivityManager.NetworkCallback mNetworkCallback; @Override public void onCreate() { Log.d(LOG_TAG, "onCreate" ); registerNetworkCallback(); mContext=getApplicationContext(); super .onCreate(); startFgService(); } @Override public void onDestroy() { Log.d(LOG_TAG, "onDestory" ); super .onDestroy(); unregisterNetworkCallback(); } void startFgService(){ Intent intent = new Intent( this , MainActivity. class ); PendingIntent pi = PendingIntent.getActivity( this , 0 , intent, 0 ); String CHANNEL_ID= "test" ; NotificationCompat.Builder builder; NotificationChannel ch = new NotificationChannel(CHANNEL_ID, "test channel" , NotificationManager.IMPORTANCE_DEFAULT); ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(ch); builder = new NotificationCompat.Builder( this ,CHANNEL_ID ); builder.setContentIntent(pi); startForeground( 1 , builder.build()); } private void printAllNetworks(){ ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); Network[] networks = cm.getAllNetworks(); for ( Network n : networks){ Log.d(LOG_TAG, "print all networks" ); NetworkCapabilities np = cm.getNetworkCapabilities(n); Log.d(LOG_TAG, np.toString()); } } @RequiresApi (api = Build.VERSION_CODES.Q) private void workaround(){ WifiManager mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); Network[] networks = cm.getAllNetworks(); for ( Network n : networks){ Log.d(LOG_TAG, "print all networks" ); LinkProperties lp = cm.getLinkProperties(n); String iName = lp.getInterfaceName(); Log.d(LOG_TAG, iName); if (iName.equals( "eth0" )){ Log.d(LOG_TAG, iName + "found don't workaround...." ); return ; } } if (mWifiManager.isWifiEnabled()) { Log.d(LOG_TAG, "wifi manager disconnect and recoonect" ); Toast.makeText(mContext, "wifi manager disconnect and reconnect" , Toast.LENGTH_LONG).show(); mWifiManager.disconnect(); mWifiManager.reconnect(); } else { Log.e(LOG_TAG, "wifi disabled" ); } } private void registerNetworkCallback() { ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); NetworkRequest.Builder builder = new NetworkRequest.Builder(); mNetworkCallback= new ConnectivityManager.NetworkCallback(){ @Override public void onAvailable( @NonNull Network network) { super .onAvailable(network); Log.d(LOG_TAG, "onAvailable : " +network.toString()); printAllNetworks(); } @RequiresApi (api = Build.VERSION_CODES.Q) @Override public void onLost( @NonNull Network network) { super .onLost(network); Log.d(LOG_TAG, "onLost try to workaround" ); workaround(); } }; cm.registerNetworkCallback(builder.build(), mNetworkCallback); } private void unregisterNetworkCallback(){ ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); cm.unregisterNetworkCallback(mNetworkCallback); } } |
1 2 3 4 5 6 7 8 9 | protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext=getApplicationContext(); //startService(new Intent(mContext, WifiTestService.class)); startForegroundService( new Intent(mContext, WifiTestService. class )); } |
'android' 카테고리의 다른 글
Android X란? (0) | 2020.07.23 |
---|---|
native service를 aidl로 binding (0) | 2020.07.17 |
wake lock example (0) | 2020.07.03 |
Xlint error in Android studio (0) | 2020.04.24 |
Keystore file not set for signing config release 에러 발생 (0) | 2020.04.24 |