在Application中保存全局变量

1.新建一个类MyApp继承Application

import android.app.Application;

public class MyApp extends Application {
    
    private String username;

    public synchronized String getUsername() {
        return username;
    }

    public synchronized void setUsername(String username) {
        this.username = username;
    }

}

  

2.在AndroidManifest.xml文件中配置

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.ballonbay"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:name=".MyApp" android:icon="@drawable/app_icon" android:label="@string/app_name">
        ……
    </application>
</manifest> 

 3.在Activity中获取

MyApp myApp = (MyApp) getApplication();

 

你可能感兴趣的:(android,xml)