我正在尝试在我的应用程序中获得网络连接。我的编译版本是 23,目标版本是 19。当我在 api 版本 23 的设备上运行我的应用程序时,一切正常。但是当我运行我的应用程序时,我收到错误“找不到方法 android.content.Context.getSystemService”具有 api 版本 19 的设备。
public class LoadingActivity extends AppCompatActivity {
private String TAG = LoadingActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading);
Log.d(TAG,"onCreate()");
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG,"onStart()");
ConnectivityManager cm = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
Boolean isConnected = (activeNetwork != null && activeNetwork.isConnectedOrConnecting());
}
}
部分堆栈跟踪
06-03 00:23:36.439 1479-1479/com.example.andonsystem D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
06-03 00:23:36.447 1479-1479/com.example.andonsystem D/LoadingActivity: onCreate()
06-03 00:23:36.447 1479-1479/com.example.andonsystem D/LoadingActivity: onStart()
06-03 00:23:36.499 1479-1484/com.example.andonsystem D/dalvikvm: GC_CONCURRENT freed 271K, 10% free 2976K/3292K, paused 2ms+1ms, total 8ms
06-03 00:23:36.503 522-851/system_process I/ActivityManager: START u0 {cmp=com.example.andonsystem/.LoginActivity} from pid 1479
06-03 00:23:37.015 1479-1479/com.example.andonsystem I/dalvikvm: Could not find method android.content.Context.getSystemService, referenced from method com.example.andonsystem.LoginActivity.access$super
06-03 00:23:37.015 1479-1479/com.example.andonsystem W/dalvikvm: VFY: unable to resolve virtual method 473: Landroid/content/Context;.getSystemService (Ljava/lang/Class;)Ljava/lang/Object;
06-03 00:23:37.015 1479-1479/com.example.andonsystem D/dalvikvm: VFY: replacing opcode 0x6f at 0x004b
提前感谢您的帮助...