Android 从服务访问应用程序类
为了将值存储为全局变量,我了解到可以使用应用程序类。我打算从主活动中获取用户名和密码,将它们存储在应用程序类变量中并启动一个新活动,然后在新活动中启动的服务中获取这些值,但是,使用我在应用程序类中定义的getter方法,我会得到空值 我的应用程序类:Android 从服务访问应用程序类,android,global-variables,Android,Global Variables,为了将值存储为全局变量,我了解到可以使用应用程序类。我打算从主活动中获取用户名和密码,将它们存储在应用程序类变量中并启动一个新活动,然后在新活动中启动的服务中获取这些值,但是,使用我在应用程序类中定义的getter方法,我会得到空值 我的应用程序类: public class MyApp extends Application { private String uid; private String upwd; @Override public void onCreate()
public class MyApp extends Application
{
private String uid;
private String upwd;
@Override
public void onCreate()
{
super.onCreate();
}
public void setUID(String value)
{
uid = value;
}
public void setPWD(String value)
{
upwd = value;
}
public String getUID()
{
return uid;
}
public String getPWD()
{
return upwd;
}
}
public void setvalues()
{
unameval = Unametxtfield.getText().toString();
pswrdval = Pswrdtxtfield.getText().toString();
((MyApp)this.getApplicationContext()).setUID(unameval);
((MyApp)this.getApplicationContext()).setPWD(pswrdval);
}
public void fetchvalues()
{
String uname = ((MyApp).getApplicationContext()).getUID();
String upswrd = ((MyApp).getApplicationContext()).getPWD();
}
<application
android:name="MyApp"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- A Service here -->
<service
android:name="Service1"
android:process=":myapp_service1" >
</service>
<!-- A service in which I do the fetching of uname and pswd -->
<service
android:name="Service2"
android:process=":myapp_Service2" >
</service>
<activity
android:name=".Second_Activity"
android:exported="false"
android:label="@string/activityname" >
<intent-filter>
<action android:name="android.intent.action.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/Firstactivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
在我的主要活动中:
public class MyApp extends Application
{
private String uid;
private String upwd;
@Override
public void onCreate()
{
super.onCreate();
}
public void setUID(String value)
{
uid = value;
}
public void setPWD(String value)
{
upwd = value;
}
public String getUID()
{
return uid;
}
public String getPWD()
{
return upwd;
}
}
public void setvalues()
{
unameval = Unametxtfield.getText().toString();
pswrdval = Pswrdtxtfield.getText().toString();
((MyApp)this.getApplicationContext()).setUID(unameval);
((MyApp)this.getApplicationContext()).setPWD(pswrdval);
}
public void fetchvalues()
{
String uname = ((MyApp).getApplicationContext()).getUID();
String upswrd = ((MyApp).getApplicationContext()).getPWD();
}
<application
android:name="MyApp"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- A Service here -->
<service
android:name="Service1"
android:process=":myapp_service1" >
</service>
<!-- A service in which I do the fetching of uname and pswd -->
<service
android:name="Service2"
android:process=":myapp_Service2" >
</service>
<activity
android:name=".Second_Activity"
android:exported="false"
android:label="@string/activityname" >
<intent-filter>
<action android:name="android.intent.action.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/Firstactivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
服务内部我的第二个活动:
public class MyApp extends Application
{
private String uid;
private String upwd;
@Override
public void onCreate()
{
super.onCreate();
}
public void setUID(String value)
{
uid = value;
}
public void setPWD(String value)
{
upwd = value;
}
public String getUID()
{
return uid;
}
public String getPWD()
{
return upwd;
}
}
public void setvalues()
{
unameval = Unametxtfield.getText().toString();
pswrdval = Pswrdtxtfield.getText().toString();
((MyApp)this.getApplicationContext()).setUID(unameval);
((MyApp)this.getApplicationContext()).setPWD(pswrdval);
}
public void fetchvalues()
{
String uname = ((MyApp).getApplicationContext()).getUID();
String upswrd = ((MyApp).getApplicationContext()).getPWD();
}
<application
android:name="MyApp"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- A Service here -->
<service
android:name="Service1"
android:process=":myapp_service1" >
</service>
<!-- A service in which I do the fetching of uname and pswd -->
<service
android:name="Service2"
android:process=":myapp_Service2" >
</service>
<activity
android:name=".Second_Activity"
android:exported="false"
android:label="@string/activityname" >
<intent-filter>
<action android:name="android.intent.action.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/Firstactivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Android清单:
public class MyApp extends Application
{
private String uid;
private String upwd;
@Override
public void onCreate()
{
super.onCreate();
}
public void setUID(String value)
{
uid = value;
}
public void setPWD(String value)
{
upwd = value;
}
public String getUID()
{
return uid;
}
public String getPWD()
{
return upwd;
}
}
public void setvalues()
{
unameval = Unametxtfield.getText().toString();
pswrdval = Pswrdtxtfield.getText().toString();
((MyApp)this.getApplicationContext()).setUID(unameval);
((MyApp)this.getApplicationContext()).setPWD(pswrdval);
}
public void fetchvalues()
{
String uname = ((MyApp).getApplicationContext()).getUID();
String upswrd = ((MyApp).getApplicationContext()).getPWD();
}
<application
android:name="MyApp"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- A Service here -->
<service
android:name="Service1"
android:process=":myapp_service1" >
</service>
<!-- A service in which I do the fetching of uname and pswd -->
<service
android:name="Service2"
android:process=":myapp_Service2" >
</service>
<activity
android:name=".Second_Activity"
android:exported="false"
android:label="@string/activityname" >
<intent-filter>
<action android:name="android.intent.action.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/Firstactivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
注意:我在某个地方读到,当您在多个进程上使用应用程序类(我想我正在这样做)时,应用程序类不会工作,这是真的吗?使用还是使用。这是真的。您不能在多个进程中使用此方法。我已经切换到共享首选项并使其正常工作。没有问题,但我想了解早期方法不起作用的原因,无论如何,感谢Singleton也不起作用。与应用程序对象的原因相同:不同的进程意味着不同的VM实例。它们不能直接通信并共享相同的内存空间。服务是否作为一个独立进程运行?