代码下载地址:

http://download.csdn.net/detail/a332324956/6927141


主要代码片段为:

package com.lxm.app;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.waps.AppConnect;

public class CallerlocationActivity extends Activity implements OnClickListener {
	/** Called when the activity is first created. */
	private Button Inquire;
	private Button Save;
	private EditText InputNum;
	private TextView ShowNum;
	private TextView ShowCallerLocation;
	private String datapath;
	private static String dbname = "callerlocation.db";
	private Handler subHandler;
	public final static int INQUIRE_ENUM = 0;
	public final static int SAVE_ENUM = 1;
	public final static int INQUIRE_STATUS_DOING = 0;
	public final static int INQUIRE_STATUS_END = 1;
	public final static int INQUIRE_STATUS_CALLERNUM = 2;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Inquire = (Button) findViewById(R.id.button1);
		Save = (Button) findViewById(R.id.button2);
		Inquire.setOnClickListener(this);
		Save.setOnClickListener(this);
		ShowNum = (TextView) findViewById(R.id.textView3);
		ShowCallerLocation = (TextView) findViewById(R.id.textView4);
		InputNum = (EditText) findViewById(R.id.editText1);
		datapath = "/data/data/" + this.getPackageName().toString()
				+ "/databases";
		AppConnect.getInstance(this);
		AppConnect.getInstance(this).initPopAd(this);
		AppConnect.getInstance(this).showPopAd(this);
		LinearLayout adlayout =(LinearLayout)findViewById(R.id.AdLinearLayout);
		AppConnect.getInstance(this).showBannerAd(this, adlayout);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		AppConnect.getInstance(this).close();
	}

	private Handler handler = new Handler() {
		String Location;

		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case INQUIRE_STATUS_END:
				Inquire.setEnabled(true);
				break;
			case INQUIRE_STATUS_DOING:
				Message Sendmsg = Message.obtain();
				Sendmsg.what = INQUIRE_ENUM;
				Sendmsg.obj = new String(InputNum.getText().toString());
				subHandler.sendMessage(Sendmsg);
				Inquire.setEnabled(false);
				break;
			case INQUIRE_STATUS_CALLERNUM:
				Location = (String) msg.obj;
				ShowNum.setText(InputNum.getText().toString());
				ShowCallerLocation.setText(Location);
				break;
			default:
				break;
			}
		}
	};

	@Override
	public void onClick(View v) {
		if (v.equals(Inquire)) {
			ShowNum.setText("");
			ShowCallerLocation.setText("");
			if (InputNum.length() == 0) {
				Toast.makeText(this, "请输入号码!", Toast.LENGTH_LONG).show();
				return;
			}
			if (InputNum.length() < 3) {
				Toast.makeText(this, "请修改输入!", Toast.LENGTH_LONG).show();
				return;
			}
			new Thread() {
				public void run() {
					Looper.prepare();
					handler.sendEmptyMessage(INQUIRE_STATUS_DOING);
					subHandler = new Handler() {
						public void handleMessage(android.os.Message msg) {

							switch (msg.what) {
							case INQUIRE_ENUM:
								String InquireNum = (String) msg.obj;
								try {

									// 首先将数据复制出去到data区域
									String callerName = "";
									CopyRawtodata.CopyRawtodata(datapath,
											dbname, getApplicationContext(),
											R.raw.callerlocation, true);
									//
									// Thread.sleep(1000);
									callerName = InquireCallerPos(InquireNum,
											datapath, dbname);

									Message Sendmsg = Message.obtain();
									Sendmsg.what = 2;
									Sendmsg.obj = new String(callerName);
									handler.sendMessage(Sendmsg);
								} catch (Exception e) {
									e.printStackTrace();
									subHandler.getLooper().quit();
								} finally {
									handler.sendEmptyMessage(INQUIRE_STATUS_END);
									subHandler.getLooper().quit();
								}
								break;
							case SAVE_ENUM:

								break;
							default:
								break;
							}// end switch
						}
					};

					Looper.loop();

				}
			}.start();

		} else if (v.equals(Save)) {

		}

	}

	// 查询数据,依据号码,返回归属地位置,数据库查询接口当前未作封装,直接处理
	private String InquireCallerPos(String num, String dbpath, String dbname) {
		int cityId = 0;
		String callerName = "未知属地";
		SQLiteDatabase db = openOrCreateDatabase(dbpath + "/" + dbname,
				Context.MODE_PRIVATE, null);
		Cursor cursor = null;
		boolean IsNeed2Search = false;
		if (num.length() < 7) {
			Log.i("lxm", num);
			cursor = db.rawQuery("select * from phones WHERE number = " + num,
					null);
		} else {
			IsNeed2Search = true;
			String tmp = num.substring(0, 7);
			Log.i("lxm", "" + tmp);
			cursor = db.rawQuery("select * from mobiles WHERE number = " + tmp,
					null);
		}

		if (IsNeed2Search) {
			if (0 == cursor.getCount()) {
				String tmp = num.substring(0, 3);
				cursor = db.rawQuery("select * from phones WHERE number = "
						+ tmp, null);
			}
			if (0 == cursor.getCount()) {
				String tmp = num.substring(0, 4);
				cursor = db.rawQuery("select * from phones WHERE number = "
						+ tmp, null);
			}
			if (0 == cursor.getCount()) {
				String tmp = num.substring(0, 5);
				cursor = db.rawQuery("select * from phones WHERE number = "
						+ tmp, null);
			}
		}
		if (0 == cursor.getCount()) {
			Log.i("lxm", "" + "xxxxx");
		}
		while (cursor.moveToNext()) {
			cityId = cursor.getInt(cursor.getColumnIndex("cityid"));
		}
		cursor = db
				.rawQuery("select * from cities WHERE _id = " + cityId, null);
		while (cursor.moveToNext()) {
			callerName = cursor.getString(cursor.getColumnIndex("name"));
		}
		db.close();
		Log.i("lxm", "" + cityId + " " + callerName);
		return callerName;

	}
}

代码比较简单,大家可以自己阅读查看了。





Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐