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(); } //\\ }
相关推荐
luoj 2011-09-12
androidstudyroom 2011-08-19
TTHHVV 2011-12-05
peixiaopao 2012-05-06
ruanjianxiong 2012-05-23
fanjunjian 2015-03-03
Nostalgiachild 2014-11-02
oLimxing 2014-07-21
Jemy杰 2013-04-26
mlsnatalie 2012-10-21
家猫OR野猫 2012-05-11
crazymaple 2012-03-23
xiaozhifree 2012-02-27
西木 2012-01-03
xfcyhades 2011-10-07
rookieding 2011-09-10
刘炳昭 2011-08-15
dianlunhui 2011-06-13
wangkuifeng0 2011-05-26