Android Start (1)

Android Start (1)

這是我十個月沒寫Android之後,開始重寫一個新的app所需要的東西。

儲存Cache資訊,(記錄登入後所需的資訊,或從Server要的東西)

    public String PREF_NAME = "Setting";
    public Context mContext;

    public void setData(String key,String value) {
        SharedPreferences mSettings = mContext.getSharedPreferences(PREF_NAME,  Context.MODE_PRIVATE);
        mSettings.edit().putString(key, value).commit();
    }

    public String getData(String key){
        SharedPreferences mSettings = mContext.getSharedPreferences(PREF_NAME,  Context.MODE_PRIVATE);
        String value = mSettings.getString(key, "");
        return value;
    }

取得MacAddress

        WifiManager wm = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifi = wm.getConnectionInfo();
        String macAddress = wifi.getMacAddress();

取得SerialID

        String serial = "";
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            serial = Build.SERIAL;
        }

取得AndroidID

String androidId = Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.ANDROID_ID);

取得IMEI

        TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        String deviceId = tm.getDeviceId();

取得Imsi

        TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        String imsi = tm.getSubscriberId();

取得手機號碼

        TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        String phoneNubmer = tm.getLine1Number();

取得Gmail

        AccountManager accountManager = AccountManager.get(mContext);
        // 取得指定 type 的 Account
        Account[] accounts = accountManager.getAccountsByType("com.google");
        for(Account account : accounts){
            String EMmail = account.name;
        }

取得手機機型

String model = Build.MODEL;

取得Android作業系統版本

"Android " + Build.VERSION.RELEASE;

取得APP版本

    public int getAppVersion() {
        int versionCode = -1;
        try {
            PackageInfo packageInfo = mContext.getPackageManager()
                    .getPackageInfo(mContext.getPackageName(), 0);
            versionCode = packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            // should never happen
        }
        return versionCode;
    }

取得App PackageName

    public String getAppPackageName(){
        return mContext.getPackageName();
    }

取得APP 版本

    public String getAppVerName() {
        String versionName = "";
        try {
            PackageManager manager = mContext.getPackageManager();
            PackageInfo info = manager.getPackageInfo(mContext.getPackageName(), 0);
            versionName = info.versionName;
        }catch (Exception ex) {
            ex.printStackTrace();
        }
        return versionName;
    }