在android中声明全局变量[重复]
Posted
技术标签:
【中文标题】在android中声明全局变量[重复]【英文标题】:Declaring global variables in android [duplicate] 【发布时间】:2014-01-21 09:01:06 【问题描述】:我浏览了很多页,但无法正确理解如何声明一个包含全局变量的类及其在清单文件中的声明(最重要的是,需要额外集中精力)
班级
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;
清单
<?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>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"></activity>
<activity android:name=".passforgot"></activity>
<activity android:name=".newuser"></activity>
<activity android:name=".settings"></activity>
<receiver android:name=".smsreciever">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</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;
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 += "\n";
//---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();
这里我想给它设置数据...
Global g = (Global)getApplication(); // this line gives error for get application
【问题讨论】:
创建一个Application类的子类,并在其中定义全局变量 【参考方案1】:除了使用 Application 创建全局变量外,您还可以创建一个常规类来保存您的变量。现在,我正在使用这个:
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 设置一个新值:
GlobalVar.getInstance().setGlobalVar1(value);
为了获得价值:
GlobalVar.getInstance().getGlobalVar1;
【讨论】:
【参考方案2】:公共类 GlobalState 扩展应用程序
private int gameScore = 0;
public int getGameScore()
return gameScore;
public void setGameScore(int gameScore)
this.gameScore = gameScore;
public void incrementScore()
gameScore++;
清单文件
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:name="GlobalState" >
<!-- component definitions -->
</application>
在活动中的使用
GlobalState state = ((GlobalState) getApplicationContext());
【讨论】:
我的课程没有扩展活动,所以我将如何使用它? 您只需要一个上下文,将其转换为该上下文所属的活动类并在其上调用getApplicationContext()
以上是关于在android中声明全局变量[重复]的主要内容,如果未能解决你的问题,请参考以下文章