Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 手機安全衛士——高級工具-電話歸屬地查詢,安全衛士歸屬地查詢

手機安全衛士——高級工具-電話歸屬地查詢,安全衛士歸屬地查詢

編輯:關於android開發

手機安全衛士——高級工具-電話歸屬地查詢,安全衛士歸屬地查詢


AToolsActivity.java

//高級工具
public class AtoolsActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_atools);
            
    }    
    //電話號碼查詢..我們用的是數據庫查詢
    public void numberAddressQuery(View view){      
        startActivity(new Intent(this,AddressActivity.class));    
    }
}
AddressActivity.java
//電話歸屬地查詢頁面
public class AddressActivity extends Activity {
    
    private EditText etNumber;
    private TextView tvResult;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);
        etNumber = (EditText) findViewById(R.id.et_number);
        tvResult = (TextView) findViewById(R.id.tv_result);    
        //監聽edittext的變化,實時監聽TexiView的變化
        etNumber.addTextChangedListener(new TextWatcher() {            
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub//文字發生變化的回調
                String address = AddressDao.getAddress(s.toString());
                tvResult.setText(address);
            }
            
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            
            @Override
            public void afterTextChanged(Editable s) {          
            }
        });
    }

    //開始查詢,  查詢按鈕的點擊事件
    public void query(View view){    
        String number = etNumber.getText().toString().trim();    
        if(!TextUtils.isEmpty(number)){
            String address = AddressDao.getAddress(number);
            tvResult.setText(address);
        }else{//沒有輸入時,窗口抖動,這是個動畫
            Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
            etNumber.startAnimation(shake);
            vibrate();
        }    
    }
    //手機震動,需要權限
    private void vibrate(){
        Vibrator v = (Vibrator) getSystemService(VIBRATOR_SERVICE);//系統的服務
        v.vibrate(2000);
        //v.vibrate(new long[]{1000,2000,1000,3000}, -1);
        //先等待1s,在震動2S,在等待1S,在震動3S.-1表示只執行一次,不循環。有節奏的震動
        //v.cancel();//取消震動
    }
}

 shake.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXDelta="0"
    android:interpolator="@anim/cycle_7"
    android:toXDelta="10" />

    <!-- 插補器interpolator  循環7次  即1s抖動7次 -->
cycle_7.xml
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:cycles="7" />

 AddressDao.java

/**
 * 歸屬地查詢
 */
public class AddressDao {

    private static final String PATH = "data/data/com.mxn.mobilesafe/files/address.db";// 注意,該路徑必須是data/data目錄的文件,否則數據庫訪問不到

    // 返回地名
    public static String getAddress(String number) {
        String address = "未知號碼";

        // 獲取數據庫對象
        //只支持從默認的位置進行讀取,不支持assert讀取。所以需要拷貝數據庫到data/data目錄之下。在SplashActivity頁面進行拷貝數據庫。
        SQLiteDatabase database = SQLiteDatabase.openDatabase(PATH, null, SQLiteDatabase.OPEN_READONLY);

        // 手機號碼特點: 1 + (3,4,5,6,7,8) + (9位數字)
        // 正則表達式
        // ^1[3-8]\d{9}$

        if (number.matches("^1[3-8]\\d{9}$")) {// 匹配手機號碼
            Cursor cursor = database.rawQuery(
                    "select location from data2 where id=(select outkey from data1 where id=?)",
                    new String[] { number.substring(0, 7) });

            if (cursor.moveToNext()) {
                address = cursor.getString(0);
            }

            cursor.close();
        } else if (number.matches("^\\d+$")) {// 匹配數字
            switch (number.length()) {
            case 3:
                address = "報警電話";
                break;
            case 4:
                address = "模擬器";
                break;
            case 5:
                address = "客服電話";
                break;
            case 7:
            case 8:
                address = "本地電話";
                break;
            default:
                // 01088881234
                // 048388888888
                if (number.startsWith("0") && number.length() > 10) {// 有可能是長途電話
                    // 有些區號是4位,有些區號是3位(包括0)

                    // 先查詢4位區號
                    Cursor cursor = database.rawQuery("select location from data2 where area =?",
                            new String[] { number.substring(1, 4) });

                    if (cursor.moveToNext()) {
                        address = cursor.getString(0);
                    } else {
                        cursor.close();

                        // 查詢3位區號
                        cursor = database.rawQuery("select location from data2 where area =?",
                                new String[] { number.substring(1, 3) });

                        if (cursor.moveToNext()) {
                            address = cursor.getString(0);
                        }

                        cursor.close();
                    }
                }
                break;
            }
        }

        database.close();// 關閉數據庫
        return address;
    }
}

 



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved