今天用了几个小时做的小应用,一个短信拦截器,标记黑名单什么的。做这货的主要原因是为了巩固下前面的知识,这货用了数据库,listview,activity跳转,brocastcast receiver等知识。代码写得不好看看就行了。
~~~
package com.example.duanxinlanjie;
import java.util.Vector;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText set_num;
private EditText clear_num;
private Button set_bt;
private Button clear_bt;
private Button exit_app;
private Button find_num;
private ListView lv;
private SQLiteDatabase db;
private MyAdapter myAdapter;
private Vector<String> sender;
private Vector<String> message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
set_num = (EditText) findViewById(R.id.set_num);
clear_num = (EditText) findViewById(R.id.clear_num);
set_bt = (Button) findViewById(R.id.set_bt);
clear_bt = (Button) findViewById(R.id.clear_bt);
lv = (ListView) findViewById(R.id.lv);
exit_app = (Button) findViewById(R.id.exit_app);
find_num = (Button) findViewById(R.id.find_num);
sender = new Vector<String>();
message = new Vector<String>();
MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this);
db = helper.getWritableDatabase();
getData();
myAdapter = new MyAdapter();
lv.setAdapter(myAdapter);
set_bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String num = set_num.getText().toString();
// 数据表phone_num插入一个待拦截的电话号码
db.execSQL("insert into num_data (phone_num) values (?)",
new Object[] { num });
Toast.makeText(MainActivity.this, "成功输入号码", 0).show();
}
});
clear_bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String num = clear_num.getText().toString();
// 数据表phone_num插入一个待拦截的电话号码
db.execSQL("delete from num_data where phone_num = ?",
new Object[] { num });
Toast.makeText(MainActivity.this, "成功删除号码", 0).show();
}
});
exit_app.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
find_num.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this, FindNum.class);
startActivity(intent);
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
db.close();
}
private void getData() {
Cursor cursor = db.rawQuery("select * from msg_data", null);
while (cursor.moveToNext() == true) {
String s = cursor.getString(1);
String m = cursor.getString(2);
sender.add(s);
message.add(m);
}
cursor.close();
}
private class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
int count = sender.size();
return count;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
String senderInView = (String) sender.elementAt(position);
String messageInView = (String) message.elementAt(position);
View v = View.inflate(MainActivity.this, R.layout.lv_item, null);
TextView tv_num = (TextView) v.findViewById(R.id.tv_num);
TextView tv_content = (TextView) v.findViewById(R.id.tv_content);
tv_num.setText(senderInView);
tv_content.setText(messageInView);
return v;
}
}
}
~~~
~~~
package com.example.duanxinlanjie;
import java.util.Vector;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FindNum extends Activity {
private Vector<String> num;
private ListView num_list;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.find_num);
num = new Vector<String>();
num_list = (ListView) findViewById(R.id.num_list);
MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this);
db = helper.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from num_data", null);
while (cursor.moveToNext()) {
String n = cursor.getString(1);
num.add(n);
}
cursor.close();
db.close();
num_list.setAdapter(new ArrayAdapter<String>(this, R.layout.num_item,
R.id.num_text, num));
}
}
~~~
~~~
package com.example.duanxinlanjie;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
public MySQLiteOpenHelper(Context context) {
super(context, "phone_data.db", null, 1);
// TODO Auto-generated constructor stub
}
/**
* 建立两份表,第一份表是存要拦截的号码,第二份表存的是已经拦截的号码和短信内容
*/
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table num_data(id integer primary key autoincrement, phone_num varchar(20))");
db.execSQL("create table msg_data(id integer primary key autoincrement, phone_num varchar(20), msg varchar(100))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
~~~
~~~
package com.example.duanxinlanjie;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver {
private MySQLiteOpenHelper helper;
private SQLiteDatabase db;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
helper = new MySQLiteOpenHelper(context);
db = helper.getWritableDatabase();
Object[] pdus = (Object[]) intent.getExtras().get("pdus");
for (Object pdu : pdus) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
String body = smsMessage.getMessageBody();
String sender = smsMessage.getDisplayOriginatingAddress();
Cursor cursor = db.rawQuery(
"select * from num_data where phone_num = ?",
new String[] { sender });
if (cursor.getCount() == 0) {
cursor.close();
helper = null;
db.close();
return;
}
cursor.close();
db.execSQL("insert into msg_data (phone_num , msg) values (?,?)",
new Object[] { sender, body });
db.close();
helper = null;
}
Toast.makeText(context, "成功拦截", 0).show();
abortBroadcast();
}
}
~~~
~~~
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="标记号码: "
android:textSize="20dp" />
<EditText
android:id="@+id/set_num"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:layout_weight="7.69" />
<Button
android:id="@+id/set_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="确认" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="清除号码: "
android:textSize="20dp" />
<EditText
android:id="@+id/clear_num"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:layout_weight="7.69" />
<Button
android:id="@+id/clear_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="确认" />
</LinearLayout>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="10"
android:layout_marginTop="10dp" >
</ListView>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_weight="0"
android:orientation="horizontal" >
<Button
android:id="@+id/exit_app"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="退出程序"
android:layout_alignParentRight="true"
android:layout_marginRight="50dp" />
<Button
android:id="@+id/find_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拦截号码"
android:layout_alignParentLeft="true"
android:layout_marginLeft="50dp" />
</RelativeLayout>
</LinearLayout>
~~~
~~~
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/num_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></ListView>
</LinearLayout>
~~~
~~~
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_num"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="电话号码" />
<TextView
android:id="@+id/tv_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="18dp"
android:text="短信内容" />
</LinearLayout>
~~~
~~~
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/num_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="号码"
/>
</LinearLayout>
~~~
~~~
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.duanxinlanjie"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/wuya"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:logo="@drawable/wuya" >
<activity
android:name="com.example.duanxinlanjie.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MySQLiteOpenHelper" >
</activity>
<receiver android:name="com.example.duanxinlanjie.SmsReceiver" >
<intent-filter android:priority="1000" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity android:name="com.example.duanxinlanjie.FindNum" >
</activity>
</application>
</manifest>
~~~
- 前言
- 安卓ListView一个简单代码的注释和探讨
- 安卓wifi热点编程代码的若干注释
- 安卓距离传感器编程
- 简单的ScrollView
- 简单的ListView
- 简单的ArrayAdapter
- AsyncTask的初步学习
- AsyncTask再度学习
- Handler初步学习
- 开启系统Activity
- 隐式调用Activity
- Activity的生命周期
- startActivityForResult的初步学习
- 多点触控拉伸图片
- LruCache图片缓存技术
- SQLiteOpenHelper数据库操作
- 短信拦截器
- 简单的Notification
- ListView优化以及checkbox状态问题
- 安卓多线程下载
- 安卓JSON解析初步探讨
- 选项卡样式的fragment
- DrawerLayout实现简单的侧滑功能
- 安卓软引用解决图片OOM问题
- 安卓隐式Intent启动Activity和BroadcastReceiver若干注意点
- Dialog学习笔记
- ActionBar使用