我浏览了很多页面,但是无法正确理解如何声明一个类来包含全局变量及其在清单文件中的声明(最重要的是,需要额外关注)
班级
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
答案 0 :(得分: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;
答案 1 :(得分:0)
公共类GlobalState扩展了Application {
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());