检查网络在Android中使用 [英] Check network available in android
本文介绍了检查网络在Android中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要制订一个android应用。
在这里,我要制订一个Twitter的整合Android应用程序。
我已经用下面code:
公共无效onClickTwitt(){
如果(isNetworkAvailable()){
Twitt twitt =新Twitt(getActivity(),CONSUMER_KEY,SECRET_KEY);
twitt.shareToTwitter(_title);
}其他{
showToast(没有可用的网络连接!!!);
}
}
公共布尔isNetworkAvailable(){
ConnectivityManager连接=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 如果(连接== NULL){
返回false;
}其他{
的NetworkInfo []信息= connectivity.getAllNetworkInfo();
如果(信息!= NULL){
的for(int i = 0; I< info.length;我++){
如果(资讯[I] .getState()== NetworkInfo.State.CONNECTED){
返回true;
}
}
}
}
返回false;
}
下面我得到以下错误:
的方法getSystemService(字符串)是未定义的类型
SubCate
请帮我...我怎样才能解决这些错误????
解决方案
公共无效onClickTwitt(){
如果(isNetworkAvailable(本)){
Twitt twitt =新Twitt(getActivity(),CONSUMER_KEY,SECRET_KEY);
twitt.shareToTwitter(_title);
}其他{
showToast(没有可用的网络连接!!!);
}
}
公共布尔isNetworkAvailable(上下文的背景下){
ConnectivityManager连接=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); 如果(连接== NULL){
返回false;
}其他{
的NetworkInfo []信息= connectivity.getAllNetworkInfo();
如果(信息!= NULL){
的for(int i = 0; I< info.length;我++){
如果(资讯[I] .getState()== NetworkInfo.State.CONNECTED){
返回true;
}
}
}
}
返回false;
}
I have to develop one android application.
Here i have to develop one twitter integration android application.
i have using below code:
public void onClickTwitt() {
if (isNetworkAvailable()) {
Twitt twitt = new Twitt(getActivity(), consumer_key, secret_key);
twitt.shareToTwitter(_Title);
} else {
showToast("No Network Connection Available !!!");
}
}
public boolean isNetworkAvailable() {
ConnectivityManager connectivity =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
Here am getting below error :
The method getSystemService(String) is undefined for the type
SubCate
Please help me...How can i resolve these error ????
解决方案
public void onClickTwitt() {
if (isNetworkAvailable(this)) {
Twitt twitt = new Twitt(getActivity(), consumer_key, secret_key);
twitt.shareToTwitter(_Title);
} else {
showToast("No Network Connection Available !!!");
}
}
public boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity =(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
这篇关于检查网络在Android中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文