Android 获得imei 和 mac ,平板有时无法获取imei

package com.example.hellotest;

import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {

//	public static String IMEI;
//	private TelephonyManager mTelManager;
	public TextView tv;
	private SQLiteDatabase  mSqLiteDatabase = null;
	public static final String TAG = "HELLO";
	public static final String DB_NAME = "test.db";
	public static final String TABLE_NAME = "table_test";
	public static final String id = "_id";
	public static final String mac = "mac";
	public static final String num = "num";
	String sql = "create table " + TABLE_NAME
			+ "(" + id + " integer primary key, "
			+ mac + " varchar(50), "
			+ num + " interger);";
	String deleteSql = "DELETE FROM " + TABLE_NAME +";";

	public static String mString = null;
	public int mCount = 0;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView)findViewById(R.id.tv);
    	WifiManager wifi = (WifiManager) getSystemService(WIFI_SERVICE);   
		WifiInfo info = wifi.getConnectionInfo();
//		mTelManager = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
//		IMEI = mTelManager.getDeviceId();
		Log.i("MAC", info.getMacAddress());
		tv.setText(info.getMacAddress());
		mString = info.getMacAddress();
		mSqLiteDatabase = this.openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);
		try {
			mSqLiteDatabase.execSQL(sql);
		}
		catch (Exception e) {
			Log.i(TAG, "Wrong");
			// TODO: handle exception
			}
		Cursor cur = mSqLiteDatabase.rawQuery("SELECT * FROM table_test", null);
		if (cur != null) {
			if (cur.moveToFirst()) {
				int numColumn = cur.getColumnIndex("num");
				int number = cur.getInt(numColumn);
				int stringColumn = cur.getColumnIndex("mac");
				String str = cur.getString(stringColumn);
				Log.i("num", Integer.toString(number));
				Log.i("string", str);
			}
		}
		mSqLiteDatabase.execSQL(deleteSql);
    }
	public void AddData() {
		ContentValues cv = new ContentValues();
		cv.put(mac, mString);
		cv.put(num, 7);
		mSqLiteDatabase.insert(TABLE_NAME, null, cv);
		Log.i("Success", "AddSuccess...");
	}
	public void UpData() {
		ContentValues cv = new ContentValues();
		cv.put(mac, mString);
		cv.put(num, 5);
		mSqLiteDatabase.update(TABLE_NAME, cv, mac + "=" + mac, null);
		Log.i("Success", "UpdataSuccess...");
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		int item_id = item.getItemId();
		switch (item_id) {
		case R.id.menu_settings:
			AddData();
			MainActivity.this.finish();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
    
}

你可能感兴趣的:(Android 获得imei 和 mac ,平板有时无法获取imei)