StringTokenizer用法

配置文件中

android:versionCode="1"
    android:versionName="1ee.0z.mm" >

package com.example.stringtokenizerdemo;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {
	String versionName;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//android:versionName="1ee.0z.mm"
		String localAPKVertion = getLocalAPKVertion();
		List<String> stringList = getStringList(localAPKVertion, ".");
		for (int i = 0; i < stringList.size(); i++) {
			Log.e("TAG", ""+stringList.get(i));
			
		}
		
		List<String> stringList1 = getStringList1(localAPKVertion, ".");
		for (int i = 0; i < stringList1.size(); i++) {
			Log.e("TAG", ""+stringList1.get(i));
			
		}

	}

	public List<String> getStringList(String string, String splitter) {
		List<String> list = new ArrayList<String>();
		StringTokenizer st = new StringTokenizer(string, splitter);
		while (st.hasMoreElements()) {
			list.add(st.nextToken());
		}
		return list;
	}

	public List<String> getStringList1(String string, String splitter) {
		List<String> list = new ArrayList<String>();
		StringTokenizer st = new StringTokenizer(string, splitter,true);
		while (st.hasMoreElements()) {
			list.add(st.nextToken());
		}
		return list;
	}
	public String getLocalAPKVertion() {
		try {
			PackageManager manager = getPackageManager();
			PackageInfo info = manager.getPackageInfo(getPackageName(), 0);
			versionName = info.versionName;
			// 下面的代码是获取版本Code
			return versionName;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return versionName;
	}
}

StringTokenizer用法_第1张图片

你可能感兴趣的:(StringTokenizer用法)