号码归属地查询软件


MalataNumSearchActivity.java


package com.aiai.www;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;
import java.util.Calendar;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.method.LinkMovementMethod;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.TextView;

public class MalataNumSearchActivity extends Activity implements
		OnClickListener, OnTouchListener {
	/** Called when the activity is first created. */
	/*
	 * private final String ACT_READ_PHONE_STATE =
	 * "android.intent.action.PHONE_STATE"; private final String
	 * ACT_PROCESS_OUTGOING_CALLS = "android.intent.action.NEW_OUTGOING_CALL";
	 */

	ImageButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
	ImageButton bcls, bsearch;
	TextView textNum;
	TextView textview1, textview2;
	
	private static final String CHECKBOX_STATUS="checkbox_status";
	CheckBox CheckAutoSearch;

	private SQLiteDatabase mSQLiteDatabase = null;

	String mSQL_num = null;
	String mSQL_areacode = null;
	String mSQL_postcode = null;
	String mSQL_province = null;
	String mSQL_city = null;
	String mSQL_attribution = null;
	
	public final static String filePath = "/sdcard";
	public final static String DB_NAME = "attribution.db";
	

	enum TextViewType {
		BLANK, POSTCODE, AREACODE, PHONENUM,
	}

	TextViewType mTextViewType = TextViewType.BLANK;
	private boolean longpress=false;
	private long mStartTime=0;
	private long mEndTime=0;
	private int width;
	private int height;

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		Display display = getWindowManager().getDefaultDisplay();

         width = display.getWidth();

 
         height = display.getHeight();
        
        System.out.println("width="+width+",height="+height);
         
        if(240==width && 480==height)   //HVGA
        	setContentView(R.layout.main240_480);
        else if(240==width && 432==height) //WQVGA432
        	setContentView(R.layout.main240_432);
        else if(240==width && 320==height)//QVGA
        	setContentView(R.layout.main240_320);
        else if(480==width && 800==height)//WVGA800
        	setContentView(R.layout.main480_800);
        else if(480==width && 854==height)//WVGA854
        	setContentView(R.layout.main480_854);
        else if(240==width && 400==height) //WQVGA400
        	setContentView(R.layout.main240_400);
        else
        	setContentView(R.layout.main);

		initRes(); 
			 
	    File dir = new File(filePath);
	       if(!dir.exists()){
	             dir.mkdirs();
	                      }
		File file = new File(filePath,DB_NAME);
		
		//File file = new File( "/data/data/com.aiai.www/databases", "attribution.db");
		if(copyDB(file)){
			mSQLiteDatabase = SQLiteDatabase.openOrCreateDatabase(file, null);
		}
	
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();

		mSQLiteDatabase.close();
	}

	private void initRes() {

		textNum = (TextView) findViewById(R.id.textViewNum);
		textview1 = (TextView) findViewById(R.id.textView1);
		textview2 = (TextView) findViewById(R.id.textView2);

		b0 = (ImageButton) findViewById(R.id.imageButton0);
		b0.setOnTouchListener(this);
		b1 = (ImageButton) findViewById(R.id.imageButton1);
		b1.setOnTouchListener(this);
		b2 = (ImageButton) findViewById(R.id.imageButton2);
		b2.setOnTouchListener(this);
		b3 = (ImageButton) findViewById(R.id.imageButton3);
		b3.setOnTouchListener(this);
		b4 = (ImageButton) findViewById(R.id.imageButton4);
		b4.setOnTouchListener(this);
		b5 = (ImageButton) findViewById(R.id.imageButton5);
		b5.setOnTouchListener(this);
		b6 = (ImageButton) findViewById(R.id.imageButton6);
		b6.setOnTouchListener(this);
		b7 = (ImageButton) findViewById(R.id.imageButton7);
		b7.setOnTouchListener(this);
		b8 = (ImageButton) findViewById(R.id.imageButton8);
		b8.setOnTouchListener(this);
		b9 = (ImageButton) findViewById(R.id.imageButton9);
		b9.setOnTouchListener(this);
		bcls = (ImageButton) findViewById(R.id.imageButtoncls);
		bcls.setOnTouchListener(this);
		bsearch = (ImageButton) findViewById(R.id.imageButtonsearch);
		bsearch.setOnTouchListener(this);

CheckAutoSearch = (CheckBox) findViewById(R.id.checkBoxAutoSearch);
		
		SharedPreferences pre =getSharedPreferences(CHECKBOX_STATUS, MODE_WORLD_READABLE);
		String content = pre.getString("checkbox_status", "");
		System.out.println("content:"+content);
		if(content.equals("checked"))
		{
			startService(new Intent(
					MalataNumSearchActivity.this,
					MalataNumSearchService.class));
			CheckAutoSearch.setChecked(true);
		}
		else
		{
			stopService(new Intent(MalataNumSearchActivity.this, MalataNumSearchService.class));
			CheckAutoSearch.setChecked(false);
		}
		
		CheckAutoSearch
				.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						if (isChecked)
						{
							System.out.println("checked aa");
							SharedPreferences.Editor editor = getSharedPreferences(CHECKBOX_STATUS, MODE_WORLD_WRITEABLE).edit();
							editor.putString("checkbox_status", "checked");
							editor.commit();

							startService(new Intent(
									MalataNumSearchActivity.this,
									MalataNumSearchService.class));
	 
						}
						else
						{
							System.out.println("not checked aa");
							
							System.out.println("checked aa");
							SharedPreferences.Editor editor = getSharedPreferences(CHECKBOX_STATUS, MODE_WORLD_WRITEABLE).edit();
							editor.putString("checkbox_status", "unchecked");
							editor.commit();
							
							stopService(new Intent(MalataNumSearchActivity.this, MalataNumSearchService.class));

							 
						}
					}
				});
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	private void inputChar(String str) {

		CharSequence tmpStr = textNum.getText();

		if (tmpStr.length() < 20)
		{
			tmpStr = tmpStr + str;
			textNum.setText(tmpStr);
		}

	}

	private void delCharAll() {
			
			textNum.setText("");
 
	}
	
	private void delChar() {
		CharSequence tmpStr = textNum.getText();
		if (tmpStr.length() >= 1)
		{
			CharSequence tmpStr2 = tmpStr.subSequence(0, tmpStr.length() - 1);
			textNum.setText(tmpStr2);
		}

	}

	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		
		
		
		if (event.getAction() == MotionEvent.ACTION_DOWN)
		{
			Date curDate =new  Date(System.currentTimeMillis());//获取当前时间
			
			mStartTime=curDate.getTime();
			
			System.out.println("mStartTime=" + mStartTime);
		}
		else if (event.getAction() == MotionEvent.ACTION_UP)
		{
			Date curDate =new  Date(System.currentTimeMillis());//获取当前时间
			
			mEndTime = curDate.getTime();
			
			System.out.println("mEndTime=" + mEndTime);
			
			System.out.println("**" + (mEndTime-mStartTime));
			
			

			if ((mEndTime - mStartTime) > 500)
			{
				longpress = true;
				mStartTime = mEndTime=0;
			}
			else
			{
				longpress = false;
			}
		}
	    
	     
	     
		switch (v.getId()) {
		case R.id.imageButton0:
			if (event.getAction() == MotionEvent.ACTION_DOWN)
			{
				b0.setBackgroundDrawable(null);
			}
			else if (event.getAction() == MotionEvent.ACTION_UP)
			{
				b0.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.n_0));

				inputChar("0");

			}
			break;
		case R.id.imageButton1:
			if (event.getAction() == MotionEvent.ACTION_DOWN)
			{
				b1.setBackgroundDrawable(null);
			}
			else if (event.getAction() == MotionEvent.ACTION_UP)
			{
				b1.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.n_1));

				inputChar("1");
			}
			break;
		case R.id.imageButton2:
			if (event.getAction() == MotionEvent.ACTION_DOWN)
			{
				b2.setBackgroundDrawable(null);
			}
			else if (event.getAction() == MotionEvent.ACTION_UP)
			{
				b2.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.n_2));

				inputChar("2");
			}
			break;
		case R.id.imageButton3:
			if (event.getAction() == MotionEvent.ACTION_DOWN)
			{
				b3.setBackgroundDrawable(null);
			}
			else if (event.getAction() == MotionEvent.ACTION_UP)
			{
				b3.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.n_3));

				inputChar("3");
			}
			break;
		case R.id.imageButton4:
			if (event.getAction() == MotionEvent.ACTION_DOWN)
			{
				b4.setBackgroundDrawable(null);
			}
			else if (event.getAction() == MotionEvent.ACTION_UP)
			{
				b4.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.n_4));

				inputChar("4");
			}
			break;
		case R.id.imageButton5:
			if (event.getAction() == MotionEvent.ACTION_DOWN)
			{
				b5.setBackgroundDrawable(null);
			}
			else if (event.getAction() == MotionEvent.ACTION_UP)
			{
				b5.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.n_5));

				inputChar("5");
			}
			break;
		case R.id.imageButton6:
			if (event.getAction() == MotionEvent.ACTION_DOWN)
			{
				b6.setBackgroundDrawable(null);
			}
			else if (event.getAction() == MotionEvent.ACTION_UP)
			{
				b6.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.n_6));

				inputChar("6");
			}
			break;
		case R.id.imageButton7:
			if (event.getAction() == MotionEvent.ACTION_DOWN)
			{
				b7.setBackgroundDrawable(null);
			}
			else if (event.getAction() == MotionEvent.ACTION_UP)
			{
				b7.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.n_7));

				inputChar("7");
			}
			break;
		case R.id.imageButton8:
			if (event.getAction() == MotionEvent.ACTION_DOWN)
			{
				b8.setBackgroundDrawable(null);
			}
			else if (event.getAction() == MotionEvent.ACTION_UP)
			{
				b8.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.n_8));

				inputChar("8");
			}
			break;
		case R.id.imageButton9:
			if (event.getAction() == MotionEvent.ACTION_DOWN)
			{
				b9.setBackgroundDrawable(null);
			}
			else if (event.getAction() == MotionEvent.ACTION_UP)
			{
				b9.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.n_9));

				inputChar("9");
			}
			break;
		case R.id.imageButtoncls:
			if (event.getAction() == MotionEvent.ACTION_DOWN)
			{
				bcls.setBackgroundDrawable(null);
			}
			else if (event.getAction() == MotionEvent.ACTION_UP)
			{
				bcls.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.arrow));
				if(longpress)
					delCharAll();
				else
				delChar();
			}
			break;

		case R.id.imageButtonsearch:
			if (event.getAction() == MotionEvent.ACTION_DOWN)
			{
				bsearch.setBackgroundDrawable(null);
			}
			else if (event.getAction() == MotionEvent.ACTION_UP)
			{
				bsearch.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.search));

				CharSequence tmpStr = textNum.getText();

				if (DBsearch((String) tmpStr))
					TextViewDisplay();
				else
					TextViewBlankDisplay();

			}
			break;
		default:
			break;
		}

		return false;
	}

 
	private boolean TextViewDisplay() {
		CharSequence tmpStr;

		if (TextViewType.PHONENUM == mTextViewType)
		{

			// textview1.setGravity(Gravity.CENTER);
			if (mSQL_province.equals(mSQL_city))
				textview1.setText(mSQL_province);
			else
				textview1.setText(mSQL_province + " " + mSQL_city);

			textview2.setText(mSQL_num + "(" + mSQL_attribution + ")\n区号:"
					+ mSQL_areacode + " 邮编:" + mSQL_postcode);
		}
		else if (TextViewType.AREACODE == mTextViewType)
		{
			if (mSQL_province.equals(mSQL_city))
				textview1.setText(mSQL_province);
			else
				textview1.setText(mSQL_province + " " + mSQL_city);

			textview2.setText(mSQL_areacode + "(区号查询)\n" + "邮编:"
					+ mSQL_postcode);
		}
		else if (TextViewType.POSTCODE == mTextViewType)
		{
			if (mSQL_province.equals(mSQL_city))
				textview1.setText(mSQL_province);
			else
				textview1.setText(mSQL_province + " " + mSQL_city);

			textview2.setText(mSQL_postcode + "(邮编查询)\n" + "区号:"
					+ mSQL_areacode);
		}

		return true;
	}

	private boolean TextViewBlankDisplay() {

		textview1.setText("请输入");

		if((240==width && 432==height)
			||(240==width && 320==height)
			||(240==width && 400==height))    //WQVGA432, QVGA ,WQVGA400
		   textview2.setText("您可以输入手机号码,区号,邮编 ");
		else
		   textview2.setText("您可以输入手机号码,区号,邮编\n ");
		
		return true;
	}

	private boolean DBsearch(CharSequence str) {

		// CharSequence tmpStr2 = tmpStr.subSequence(0, tmpStr.length() - 1);
		// textNum.setText(tmpStr2);

		CharSequence str2;

		if (str.length() >= 7)
		{
			str2 = str.subSequence(0, 7);
			if(DBPhoneNumsearch((String) str2))
				return true;

		}
		 if (str.length() == 6)
		{
			str2 = str;
			if(DBpostCodesearch((String) str2))
				return true;
		}
		 if (str.length() >= 4)
		{
			str2 = str.subSequence(0, 4);
			if (DBareaCodesearch((String) str2))
			{
				return true;
			}
		}
		 if (str.length() >= 3)
		{

			str2 = str.subSequence(0, 3);
			if (DBareaCodesearch((String) str2))
			{
				return true;
			}

		}

		mTextViewType = TextViewType.BLANK;
		return false;

		/*
		 * Cursor
		 * c=mSQLiteDatabase.query("t_numberattribution",null,"number=1598661"
		 * ,null,null,null,null); c.moveToFirst();
		 * 
		 * data1=c.getString(1); data2=c.getString(2); data3=c.getString(3);
		 * data4=c.getString(4); data5=c.getString(5);
		 * 
		 * System.out.println(" data1="+data1);
		 * System.out.println(" data2="+data2);
		 * System.out.println(" data3="+data3);
		 * System.out.println(" data4="+data4);
		 * System.out.println(" data5="+data5);
		 */

	}

	private boolean DBPhoneNumsearch(String str) {
	
		str = "'" + str + "'";
		Cursor c = mSQLiteDatabase.rawQuery(
				"SELECT * FROM t_numberattribution where number=" + str, null);
		if (c.moveToFirst())
		{
			mSQL_num = c.getString(c.getColumnIndex("number"));
			mSQL_areacode = c.getString(c.getColumnIndex("areaCode"));
			mSQL_postcode = c.getString(c.getColumnIndex("postcode"));
			mSQL_province = c.getString(c.getColumnIndex("province"));
			mSQL_city = c.getString(c.getColumnIndex("city"));
			mSQL_attribution = c.getString(c.getColumnIndex("attribution"));

			System.out.println("aa " + mSQL_num + " " + mSQL_areacode + " "
					+ mSQL_postcode + " " + mSQL_province + " " + mSQL_city
					+ " " + mSQL_attribution);

			mTextViewType = TextViewType.PHONENUM;
			return true;
		}
		return false;
	}

	private boolean DBareaCodesearch(String str) {
		str = "'" + str + "'";
		Cursor c = mSQLiteDatabase.rawQuery(
				"SELECT * FROM t_numberattribution where areaCode=" + str
						+ " limit 1", null);
		if (c.moveToFirst())
		{
			mSQL_num = c.getString(c.getColumnIndex("number"));
			mSQL_areacode = c.getString(c.getColumnIndex("areaCode"));
			mSQL_postcode = c.getString(c.getColumnIndex("postcode"));
			mSQL_province = c.getString(c.getColumnIndex("province"));
			mSQL_city = c.getString(c.getColumnIndex("city"));
			mSQL_attribution = c.getString(c.getColumnIndex("attribution"));
			System.out.println("bb " + mSQL_num + " " + mSQL_areacode + " "
					+ mSQL_postcode + " " + mSQL_province + " " + mSQL_city
					+ " " + mSQL_attribution);

			mTextViewType = TextViewType.AREACODE;
			return true;
		}
		return false;
	}

	private boolean DBpostCodesearch(String str) {
		str = "'" + str + "'";
		Cursor c = mSQLiteDatabase.rawQuery(
				"SELECT * FROM t_numberattribution where postcode=" + str
						+ " limit 1", null);
		if (c.moveToFirst())
		{
			mSQL_num = c.getString(c.getColumnIndex("number"));
			mSQL_areacode = c.getString(c.getColumnIndex("areaCode"));
			mSQL_postcode = c.getString(c.getColumnIndex("postcode"));
			mSQL_province = c.getString(c.getColumnIndex("province"));
			mSQL_city = c.getString(c.getColumnIndex("city"));
			mSQL_attribution = c.getString(c.getColumnIndex("attribution"));

			System.out.println("cc " + mSQL_num + " " + mSQL_areacode + " "
					+ mSQL_postcode + " " + mSQL_province + " " + mSQL_city
					+ " " + mSQL_attribution);

			mTextViewType = TextViewType.POSTCODE;
			return true;
		}
		return false;
	}

	public Boolean copyDB(File file){
		try{
		if(!file.exists()){ InputStream is = this.getResources().openRawResource(
                R.raw.attribution); //欲导入的数据库
        FileOutputStream fos = new FileOutputStream(file);
        
        byte[] buffer = new byte[8*1024];
        int count = 0;
        while ((count = is.read(buffer)) > 0) {
            fos.write(buffer, 0, count);
        } 
        fos.flush();
        fos.close();
        is.close();
        
    }
		
		return true;
		
	}catch(Exception e){
		e.printStackTrace();
		return false;
	}				
		}
	
}


MalataNumSearchService.java


package com.aiai.www;

import com.aiai.www.MalataNumSearchActivity.TextViewType;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class MalataNumSearchService extends Service {

	
	private SQLiteDatabase m2SQLiteDatabase = null;

	private final String ACT_READ_PHONE_STATE = "android.intent.action.PHONE_STATE";
	private final String ACT_PROCESS_OUTGOING_CALLS = "android.intent.action.NEW_OUTGOING_CALL";

	String incoming_number;

	private String mSQL_province;
	private String mSQL_city;
	
	private Toast t;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();

		System.out.println("onCreate service");

		IntentFilter intentFilter = new IntentFilter(ACT_READ_PHONE_STATE);
		registerReceiver(mCallBCR, intentFilter);

		IntentFilter intentFilter2 = new IntentFilter(
				ACT_PROCESS_OUTGOING_CALLS);
		registerReceiver(mCallBCR, intentFilter2);

	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();

		System.out.println("onDestroy service");
		unregisterReceiver(mCallBCR);

	}
	
	private String getValidNum(String str)
	{
		int len=str.length();
		
		if(str.subSequence(0, 3).equals("+86"))
			str=(String) str.subSequence(3, len);
 
		System.out.println("str="+str);
		return str;
		
		
	}

	
	private boolean DB2search(CharSequence str) {

		// CharSequence tmpStr2 = tmpStr.subSequence(0, tmpStr.length() - 1);
		// textNum.setText(tmpStr2);
		/*m2SQLiteDatabase = this.openOrCreateDatabase("attribution.db",
				MODE_PRIVATE, null);*/
		
		
		m2SQLiteDatabase = SQLiteDatabase.openOrCreateDatabase("/sdcard/attribution.db", null);
		
		CharSequence str2;

		if (str.length() >= 7)
		{
			str2 = str.subSequence(0, 7);
			if(DB2PhoneNumsearch((String) str2))
			{
				m2SQLiteDatabase.close();
				return true;
			}

		} 
		 if (str.length() >= 4)
		{
			str2 = str.subSequence(0, 4);
			if (DB2areaCodesearch((String) str2))
			{
				m2SQLiteDatabase.close();
				return true;
			}
		}
		 if (str.length() >= 3)
		{

			str2 = str.subSequence(0, 3);
			if (DB2areaCodesearch((String) str2))
			{
				m2SQLiteDatabase.close();
				return true;
			}

		}

		m2SQLiteDatabase.close();	  
		t=Toast.makeText(getApplicationContext(),"归属地查询失败", Toast.LENGTH_LONG);
		
		t.show();
		
		return false;

	 

	}
	
	private boolean DB2PhoneNumsearch(String str) {
		str = "'" + str + "'";
		Cursor c = m2SQLiteDatabase.rawQuery(
				"SELECT * FROM t_numberattribution where number=" + str, null);
		if (c.moveToFirst())
		{
 
			mSQL_province = c.getString(c.getColumnIndex("province"));
			mSQL_city = c.getString(c.getColumnIndex("city"));
			if(mSQL_province.equals(mSQL_city))
				  t=Toast.makeText(getApplicationContext(),mSQL_province, Toast.LENGTH_LONG);
			else
			      t=Toast.makeText(getApplicationContext(),mSQL_province+" "+mSQL_city, Toast.LENGTH_LONG);
			
			t.show();
 
			return true;
		}
		return false;
	}

	private boolean DB2areaCodesearch(String str) {
		str = "'" + str + "'";
		Cursor c = m2SQLiteDatabase.rawQuery(
				"SELECT * FROM t_numberattribution where areaCode=" + str
						+ " limit 1", null);
		if (c.moveToFirst())
		{			 
			mSQL_province = c.getString(c.getColumnIndex("province"));
			mSQL_city = c.getString(c.getColumnIndex("city"));
		
			if(mSQL_province.equals(mSQL_city))
				  t=Toast.makeText(getApplicationContext(),mSQL_province, Toast.LENGTH_LONG);
			else
			      t=Toast.makeText(getApplicationContext(),mSQL_province+" "+mSQL_city, Toast.LENGTH_LONG);
			
			t.show();
			
			return true;
		}
		return false;
	}

 
	
	
	private BroadcastReceiver mCallBCR = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub

			 
			// 如果是拨打电话
			if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL))
			{

				String phoneNumber = intent
						.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
				System.out.println("call OUT:" + phoneNumber);
				
				String tmpNum=getValidNum(phoneNumber);
				DB2search(tmpNum);

			}
			else if (intent.getAction().equals(ACT_READ_PHONE_STATE))
			{
				// 如果是来电
				TelephonyManager tm = (TelephonyManager) context
						.getSystemService(Service.TELEPHONY_SERVICE);

				switch (tm.getCallState()) {
				case TelephonyManager.CALL_STATE_RINGING:

					incoming_number = intent.getStringExtra("incoming_number");
					System.out.println("RINGING :" + incoming_number);
					
					String tmpNum=getValidNum(incoming_number);
					DB2search(tmpNum);

					break;
				case TelephonyManager.CALL_STATE_OFFHOOK:
					System.out.println("incoming ACCEPT :" + incoming_number);
					break;

				case TelephonyManager.CALL_STATE_IDLE:
					System.out.println("incoming IDLE");

					break;
				}
			}
		}

	};
}


PhoneReceiver.java  (该代码仅供参考,未在工程中调用)

package com.aiai.www;
 
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
 
public class PhoneReceiver extends BroadcastReceiver {
 
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("action"+intent.getAction());
		if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
			//如果是去电(拨出)
			System.out.println("拨出");
		}else{
			//查了下android文档,貌似没有专门用于接收来电的action,所以,非去电即来电
			System.out.println("来电");
			TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);   
			tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
			//设置一个监听器
		}
	}
	PhoneStateListener listener=new PhoneStateListener(){
 
		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
			// TODO Auto-generated method stub
			//state 当前状态 incomingNumber,貌似没有去电的API
			super.onCallStateChanged(state, incomingNumber);
			switch(state){
			case TelephonyManager.CALL_STATE_IDLE:
				System.out.println("挂断");
				break;
			case TelephonyManager.CALL_STATE_OFFHOOK:
				System.out.println("接听");
				break;
			case TelephonyManager.CALL_STATE_RINGING:
				System.out.println("响铃:来电号码"+incomingNumber);
				//输出来电号码
				break;
			}
		}
 
	};
}

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.aiai.www"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <uses-permission android:name="android.permission.READ_PHONE_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" >
    </uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
    </uses-permission>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
    </uses-permission>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MalataNumSearchActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".MalataNumSearchService" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
        </service>

        <!--
             <receiver android:name=".PhoneReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
        </receiver>
        -->
    </application>

</manifest>


main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" android:layout_marginLeft="50dip" android:layout_marginRight="50dip" android:layout_marginTop="62dip" android:layout_gravity="center">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="请输入"
            android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/black" android:textStyle="bold" android:layout_gravity="center_horizontal"/>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="您可以输入手机号码,区号,邮编                                               .                              "
            android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="@color/black" android:layout_marginTop="5dip" android:layout_gravity="center"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dip"
        android:layout_marginTop="80dip"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textViewNum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="@color/black" android:layout_marginTop="8dip" android:layout_marginLeft="45dip"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:layout_marginLeft="25dip" android:layout_marginTop="0dip">

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/n_1" android:background="@drawable/n_1"/>

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/n_2" android:background="@drawable/n_2" android:layout_marginLeft="-10dip"/>

        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/n_3" android:background="@drawable/n_3" android:layout_marginLeft="-10dip"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:layout_marginLeft="5dip" android:layout_marginTop="-5dip">

        <ImageButton
            android:id="@+id/imageButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="-5dip"
            android:background="@drawable/n_4"
            android:src="@drawable/n_4" android:layout_marginLeft="20dip"/>

        <ImageButton
            android:id="@+id/imageButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/n_5" android:background="@drawable/n_5" android:layout_marginLeft="-10dip" android:layout_marginTop="-5dip"/>

        <ImageButton
            android:id="@+id/imageButton6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/n_6" android:background="@drawable/n_6" android:layout_marginTop="-5dip" android:layout_marginLeft="-10dip"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:layout_marginLeft="5dip" android:layout_marginTop="-5dip">

        <ImageButton
            android:id="@+id/imageButton7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/n_7" android:background="@drawable/n_7" android:layout_marginTop="-5dip" android:layout_marginLeft="20dip"/>

        <ImageButton
            android:id="@+id/imageButton8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/n_8" android:background="@drawable/n_8" android:layout_marginTop="-5dip" android:layout_marginLeft="-10dip"/>

        <ImageButton
            android:id="@+id/imageButton9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/n_9" android:background="@drawable/n_9" android:layout_marginTop="-5dip" android:layout_marginLeft="-10dip"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:layout_marginLeft="5dip" android:layout_marginTop="-5dip">

        <ImageButton
            android:id="@+id/imageButtoncls"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/arrow" android:background="@drawable/arrow" android:layout_marginTop="-5dip" android:layout_marginLeft="20dip"/>

        <ImageButton
            android:id="@+id/imageButton0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/n_0" android:background="@drawable/n_0" android:layout_marginTop="-5dip" android:layout_marginLeft="-10dip"/>

        <ImageButton
            android:id="@+id/imageButtonsearch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/search" android:background="@drawable/search" android:layout_marginTop="-5dip" android:layout_marginLeft="-10dip"/>

    </LinearLayout>

    <CheckBox
        android:id="@+id/checkBoxAutoSearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="来电自动查询" android:textColor="@color/yellow" android:layout_marginLeft="7dip"/>

</LinearLayout>



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值