Declaring global variables in android
本问题已经有最佳答案,请猛点这里访问。
我浏览了很多页,但无法正确理解如何声明一个类来包含全局变量,以及它在清单文件中的声明(最重要的是,需要对它进行额外的关注)
班级
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | global file /** * */ package com.furniture; /** * @author sanketh * */ public class Gloabal extends ap{ public String refer=""; public int set=0; public String getData(){ return this.refer; } public void setData(String d,int i){ this.refer=d; set=i; } } |
显示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.furniture" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="12" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.SEND_SMS"/> <uses-permission android:name="android.permission.RECEIVE_SMS"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" android:name=".Gloabal"> <activity android:name="com.furniture.login" android:label="@string/app_name"> <intent-filter> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </activity> </activity> </activity> </activity> <receiver android:name=".smsreciever"> <intent-filter> </intent-filter> </receiver> </application> </manifest> somefile package com.furniture; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsMessage; import android.util.Log; import android.widget.Toast; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | public class smsreciever extends BroadcastReceiver { public String value; @Override public void onReceive(Context context, Intent intent) { //---get the SMS message passed in--- Bundle bundle = intent.getExtras(); SmsMessage[] msgs = null; String str =""; if (bundle != null) { //---retrieve the SMS message received--- Object[] pdus = (Object[]) bundle.get("pdus"); msgs = new SmsMessage[pdus.length]; for (int i=0; i<msgs.length; i++){ msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]); str +="SMS from" + msgs[i].getOriginatingAddress(); str +=" :"; str += msgs[i].getMessageBody().toString(); str +=" "; } //---display the new SMS message--- // Intent i1=new Intent(); //((Gloabal)this.get).setData(str); // Gloabal g1=new Gloabal(); Global g = (Global)getApplication(); int data=g.getData(); Log.v("sanketh","smsreciver value of str:"+str); int a=1; g1.setData(str,a); Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); } } } |
我想在这里设置数据…
1 | Global g = (Global)getApplication(); // this line gives error for get application |
除了使用应用程序创建全局变量外,还可以创建一个常规类来保存变量。现在,我正在使用这个:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class GlobalVar { public String getGlobalVar1() { return GlobalVar1; } public void setGlobalVar1(String GlobalVar1) { this.GlobalVar1 = GlobalVar1; } private String GlobalVar1 =""; static { instance = new GlobalVar(); } private GlobalVar() { } public static GlobalVar getInstance() { return GlobalVar.instance; } } |
为Globalvar1设置新值:
1 | GlobalVar.getInstance().setGlobalVar1(value); |
为了获得价值:
1 | GlobalVar.getInstance().getGlobalVar1; |
公共类GlobalState扩展应用程序{
1 2 3 4 5 6 7 8 9 10 11 12 13 | private int gameScore = 0; public int getGameScore() { return gameScore; } public void setGameScore(int gameScore) { this.gameScore = gameScore; } public void incrementScore(){ gameScore++; } |
清单文件
1 2 3 4 5 6 7 8 9 | <uses-sdk android:minSdkVersion="14" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:name="GlobalState"> <!-- component definitions --> </application> |
活动中的用法
1 | GlobalState state = ((GlobalState) getApplicationContext()); |