Android 后台安卓手机短信同步
我发现了一个非常奇怪的问题,如下所示: 问题是,如果我继续与某人发送消息,我的应用程序将按顺序正确获取所有消息,但在此期间,如果出现任何中断,如打开其他应用程序或在设备中弄乱其他东西,然后,我的应用程序停止获取消息,在这种情况下,我们需要打开应用程序,它将再次开始获取消息,但跳过我们发送的传出消息,同时处理其他内容 对于此获取短信,我使用我的自定义SMSContentObserver获取短信。我正在存储最近的SMSID,以便在每次之后获取新消息 请帮帮我,我想不出原因 以下是一些代码片段:Android 后台安卓手机短信同步,android,android-asynctask,sms,android-sqlite,contentobserver,Android,Android Asynctask,Sms,Android Sqlite,Contentobserver,我发现了一个非常奇怪的问题,如下所示: 问题是,如果我继续与某人发送消息,我的应用程序将按顺序正确获取所有消息,但在此期间,如果出现任何中断,如打开其他应用程序或在设备中弄乱其他东西,然后,我的应用程序停止获取消息,在这种情况下,我们需要打开应用程序,它将再次开始获取消息,但跳过我们发送的传出消息,同时处理其他内容 对于此获取短信,我使用我的自定义SMSContentObserver获取短信。我正在存储最近的SMSID,以便在每次之后获取新消息 请帮帮我,我想不出原因 以下是一些代码片段: pu
public class SMSContentObserver extends ContentObserver
{
public SMSContentObserver()
{
super(null);
}
@Override
public void onChange(boolean selfChange)
{
super.onChange(selfChange);
Log.e("", "~~~~~~" + selfChange);
try{
appContactList = new ArrayList<HashMap<String, String>>();
appContactList = databaseHandler.getAllappContact();
if(appContactList.size() != 0){
try{
for (int i = 0; i < appContactList.size(); i++) {
strcontactname = appContactList.get(i).get("app_contact_name");
strcontactnumber = appContactList.get(i).get("app_contact_number");
strcontactid = appContactList.get(i).get("app_contact_id");
strsmsid = appContactList.get(i).get("app_contact_smsid");
strsmsdate = appContactList.get(i).get("app_contact_smsdate");
jsonArray = new JSONArray();
fetchInboxSmsIncoming(1, UserID, strcontactid, strcontactname ,strcontactnumber ,strsmsid ,strsmsdate ,"in");
fetchInboxSmsIncoming(2, UserID, strcontactid,strcontactname , strcontactnumber,strsmsid ,strsmsdate ,"out");
if (Constants.isNetworkAvailable(mContext))
{
new SMSListenerNetwork().execute();
}
else
{
Toast.makeText(PhoneContactListActivity.this, Constants.msgNoInternet, Toast.LENGTH_SHORT).show();
}
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
@Override
public boolean deliverSelfNotifications()
{
return true;
}
}
SMSListenerNetwork异步任务:
public void fetchInboxSmsIncoming(int type, String userID2, String strcontactid2,
String strcontactname2, String strcontactnumber2, String strsmsid2,
String strsmsdate2, String direction) {
// TODO Auto-generated method stub
try{
JSONObject obj = null;
databaseHandler = new DatabaseHandler(mContext);
Uri uriSms = Uri.parse("content://sms");
Cursor cursor = this.getContentResolver()
.query(uriSms,
new String[] { "_id", "address", "date", "body",
"type", "read" }, "type=" + type , null,
"date" + " COLLATE LOCALIZED ASC");
if (cursor != null) {
cursor.moveToLast();
int i = 0;
if (cursor.getCount() > 0) {
do {
Log.e("message.messageNumber",cursor.getString(cursor
.getColumnIndex("address")));
String incomingmessagefromSMS = processWord(cursor.getString(cursor.getColumnIndex("address")));
String incomingmessagefromPhone = processWord(strcontactnumber2);
if((incomingmessagefromSMS.contains(incomingmessagefromPhone)) && Integer.parseInt(cursor.getString(cursor.getColumnIndex("_id"))) > Integer.parseInt(strsmsid2)){
obj = new JSONObject();
//Toast.makeText(mContext, ""+incomingmessagefromSMS+" = " +incomingmessagefromPhone, Toast.LENGTH_LONG).show();
try {
String _id = cursor.getString(cursor.getColumnIndex("_id"));
String date = cursor.getString(cursor.getColumnIndex("date"));
Long timestamp = Long.parseLong(date);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp);
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
obj.put("Date", formatter.format(calendar.getTime()));
String text = cursor.getString(cursor.getColumnIndexOrThrow("body"));
if(text.contains("\""))
text = text.replace("\"", "");
obj.put("Text", text);
obj.put("UserID", userID2);
obj.put("ContactID",strcontactid2);
obj.put("Direction",direction);
if(i == 0){
databaseHandler.insertappContact(strcontactid2, strcontactname2, strcontactnumber2, _id, formatter.format(calendar.getTime()));
i = 1;
}
//Toast.makeText(mContext, "SMS"+obj, Toast.LENGTH_LONG).show();
jsonArray.put(obj);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//Toast.makeText(mContext, "SMS Exception"+e, Toast.LENGTH_LONG).show();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//Toast.makeText(mContext, "SMS Exception"+e, Toast.LENGTH_LONG).show();
}
}
if(!cursor.getString(cursor
.getColumnIndex("address")).contains(strcontactnumber2)){
break;
}
} while (cursor.moveToPrevious());
}
}
if(type == 1){
Log.e("jsonArray 1 ",""+jsonArray);
}else{
Log.e("jsonArray 2",""+jsonArray);
}
}catch(Exception e){
e.printStackTrace();
//Toast.makeText(mContext, "SMS Exception"+e, Toast.LENGTH_LONG).show();
}
}
公共类SMSListenerNetwork扩展异步任务{
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(Void... params) {
try{
String url = Constants.API_URL + "MessageConversation";
List<NameValuePair> Parameters = new ArrayList<NameValuePair>();
JSONObject obj = new JSONObject();
String json = null;
try {
json = obj.put("users", jsonArray).toString();
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Parameters.add(new BasicNameValuePair("ConversationText",json ));
Parameters.add(new BasicNameValuePair("DeviceID", androidToken));
Parameters.add(new BasicNameValuePair("Token", securitytoken));
ServerResponse response = jsonParser.postData(Parameters, url, Constants.API_RESPONSE_TYPE_JSON_OBJECT);
Log.e("Parameters",""+Parameters);
Log.e("url",""+url);
System.out.println("Responce:"+response);
Log.e("Responce",""+response);
if(response.getStatus() == Constants.RESPONSE_STATUS_CODE_SUCCESS){
JSONObject jsonObj = response.getjObj();
System.out.println("jsonObjEmp:"+jsonObj);
try {
response_message = jsonObj.getString("msg");
String status = jsonObj.getString("status");
if(status.equals("Success") || response_message.equals("Data is missing"))
{
return true;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}catch(Exception e){
e.printStackTrace();
return false;
}
return false;
}
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if(result)
{
try {
if(jsonArray.isNull(0)){
Log.e("SMSRECEIVER", "SMS response_message "+"Data is missing");
}else{
Log.e("SMSRECEIVER", response_message);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
Log.e("SMSRECEIVER", response_message);
}
}
}
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的布尔doInBackground(Void…params){
试一试{
字符串url=Constants.API_url+“MessageConversation”;
列表参数=新的ArrayList();
JSONObject obj=新的JSONObject();
字符串json=null;
试一试{
json=obj.put(“用户”,jsonArray).toString();
}捕获(JSONException e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
添加(新的BasicNameValuePair(“ConversationText”,json));
添加(新的BasicNameValuePair(“DeviceID”,androidToken));
添加(新的BasicNameValuePair(“令牌”,securitytoken));
ServerResponse-response=jsonParser.postData(参数、url、常量、API\u响应类型\u JSON\u对象);
Log.e(“参数”和“+”参数);
Log.e(“url”,“url+url”);
System.out.println(“response:+response”);
Log.e(“响应”,“响应+响应”);
if(response.getStatus()==Constants.response\u STATUS\u CODE\u SUCCESS){
JSONObject jsonObj=response.getjObj();
System.out.println(“jsonObjEmp:+jsonObj”);
试一试{
response_message=jsonObj.getString(“msg”);
String status=jsonObj.getString(“status”);
if(status.equals(“成功”)| response|u message.equals(“数据缺失”))
{
返回true;
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
}捕获(例外e){
e、 printStackTrace();
返回false;
}
返回false;
}
受保护的void onPostExecute(布尔结果){
super.onPostExecute(结果);
如果(结果)
{
试一试{
if(jsonArray.isNull(0)){
Log.e(“SMSRECEIVER”,“SMS响应_消息”+“数据丢失”);
}否则{
Log.e(“SMSRECEIVER”,响应消息);
}
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
其他的
{
Log.e(“SMSRECEIVER”,响应消息);
}
}
}
请在此提供一些建议
提前感谢。也许您可以使用服务来运行它。或者尝试在Androidmanifest.xml中添加SMS接收器。并在收到新短信时重新加载所有短信