您好我已經創建了一個全局的動作類,並創建了這個類內部的功能,我試圖訪問另一個活動內我遇到的問題是在日食我得到編碼錯誤訪問系統功能的函數(如getSystemService()和getApplicationContext())的確有人知道爲什麼或如何讓全局類接受系統功能?訪問系統功能的全球功能android
繼承人什麼我到目前爲止我的繼承人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);
}
}
你可能想看看http://developer.android.com/reference/android/app/Application.html – MrZander
@MrZander我並不建議擴展應用程序,也不要做文檔:*「通常沒有必要在大多數情況下,靜態單例可以以更模塊化的方式提供相同的功能。「* –