android > Application 全局变量操作

先 新建一个 Application 类 并声明 好 获取值 和修改值 的方法, 在 整个工程中 最好只 申请 这1个 Application 类

package t3.com;

import android.app.Application;

public class MyApplication extends Application {   
    private int value = 0;   
    @Override  
    public void onCreate() {   
        super.onCreate();   
    }   
    public int getName() {   
        return value;   
    }   
    public void setName(int tvalue) {   
        this.value = tvalue;   
    }   
}

然后在 Manifest <application 标签中  中添加    android:name=".MyApplication"

新建一个服务  在里面 修改 值

package t3.com;

import android.app.Activity;
import android.app.Application;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class UdpReceive extends Service{
	private boolean threadDisable;
	private int count;	
	public int appGlobal_udpReceive = 0;
	MyApplication app;  
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		new Thread(new Runnable() {
			
			public void run() {
				// TODO Auto-generated method stub
				
				while (!threadDisable) {
					try {
						Thread.sleep(1000);
					} catch (Exception e) {
						// TODO: handle exception
					}
					count++;
					if(count == 5){
						app = (MyApplication) getApplication(); //获得我们的应用程序MyApplication   
						app.setName(1); 
					}
					System.out.println( "Count is " + count);
				}
				
				
			}
		}).start();
		
	}	
	

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.threadDisable = true;
        Log.v("CountService", "on destroy");
    }

    public int getCount() {
        return count;
    }

	
}

在 Activity 中 获取值

package t3.com;





import java.io.File;
import java.io.IOException;



import android.R.bool;
import android.app.Activity;
import android.app.Application;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;  
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;


public class T3Activity extends Activity {
    /** Called when the activity is first created. */

	private Context content;
	MyApplication app; //需要在 while 前 申请
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        content = T3Activity.this;
        
        Intent intent = new Intent();  
        intent.setClass(content, UdpReceive.class);  
        startService(intent); 
        
		new Thread(new Runnable() {
			
			public void run() {
				// TODO Auto-generated method stub
				
				int res = 0;
				
				while (res == 0) {
					app = (MyApplication) getApplication(); //获得我们的应用程序MyApplication  
					res = app.getName();					
				}
				if(res == 1){
					System.out.println("z");
					content.stopService(new Intent(content,UdpReceive.class));
				}
				
				
			}
		}).start();
      
    }   

 
    //\\
}

相关推荐