嗨,我创建了一个全局操作类并在这个类中创建了函数,我试图在另一个活动中访问我遇到的问题是,在 Eclipse 中,我在访问系统功能(如 getSystemService)的函数周围出现编码错误() 和 getApplicationContext() 有人知道为什么或如何让全局类接受系统功能吗?
继承人我到目前为止继承人我的 GloblActions.java
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
public class GlobalActions{
Context mContext;
// constructor
public GlobalActions(Context context){
this.mContext = context;
}
public final static boolean isOnline (Context someContext){ {
Log.v("globals", "isonline");
ConnectivityManager cm = (ConnectivityManager) someContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
}
public final static void checkInternet(Context someContext){
isOnline(someContext);
if(isOnline(someContext) == false){
Log.v("globals", "isOnline = false");
Intent register = new Intent(someContext.getApplicationContext(), LoginForm.class);
someContext.startActivity(register);
}
}
}
这是我在活动中使用该功能的地方。我的目标是通过调用全局函数来检查每个活动的互联网连接,如果没有找到连接,则转到一个没有互联网连接的活动。
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.os.Handler;
import com.myApp.myApp.GlobalActions;
public class IntroLoader extends Activity {
public Handler handler;
public TextView loadText = null;
public Animation AniFadein = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lo_introloader);
findViewById(R.id.progressBar1).setVisibility(View.GONE);
findViewById(R.id.loadTextView).setVisibility(View.GONE);
GlobalActions.isOnline(null);
GlobalActions.checkInternet(null);
handler = new Handler();
final Runnable fadeIn = new Runnable()
{
public void run()
{
animations();
findViewById(R.id.progressBar1).setVisibility(View.VISIBLE);
findViewById(R.id.loadTextView).setVisibility(View.VISIBLE);
}
};
handler.postDelayed(fadeIn, 3000);
final Runnable aSyncTask= new Runnable()
{
public void run()
{
PostTask posttask;
posttask = new PostTask();
posttask.execute();
}
};
handler.postDelayed(aSyncTask, 4000);
}
public void animations(){
loadText = (TextView)findViewById(R.id.loadTextView);
AniFadein = AnimationUtils.loadAnimation(this, R.anim.fadein);
loadText.startAnimation(AniFadein);
}
public class PostTask extends AsyncTask<Void, String, Boolean> {
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(Void... params) {
boolean result = false;
publishProgress("progress");
return result;
}
protected void onProgressUpdate(String... progress) {
StringBuilder str = new StringBuilder();
for (int i = 1; i < progress.length; i++) {
str.append(progress[i] + " ");
}
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
checkLoginData();
}
}
public void checkLoginData(){
Intent register = new Intent(getApplicationContext(), LoginForm.class);
startActivity(register);
}
}