Android Open Source - Pairs S M S Receiver






From Project

Back to project page Pairs.

License

The source code is released under:

MIT License

If you think the Android project Pairs listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.pairs;
//from  w  w w  .j  a v a 2s .c  om
import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class SMSReceiver extends Activity {
  
  AlarmManager aManager;
  String ifeq;
  String number;
  String read;
  Calendar currentTime = Calendar.getInstance();
  Button choose;
  SharedPreferences sharedPreferences;
  SharedPreferences inboxNum;
  SharedPreferences.Editor editor;
  SharedPreferences readkeyword;
  SharedPreferences.Editor writekeyword;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.start_listen);
    choose = (Button) findViewById(R.id.chooseContact);
    
    //???????????????????????
    choose.setOnClickListener(new OnClickListener()
    {
      @Override
      public void onClick(View v)
      {
        //???????????????????????
        Intent intent = new Intent(SMSReceiver.this, ChooseContactActivity.class);
        startActivity(intent);
      }
    });
    
    //???ContentObserver????Uri.parse("content://sms")?????????????Uri?????????????
    getContentResolver().registerContentObserver(Uri.parse("content://sms"), true, new SmsObserver(new Handler()));
  }
  
  //???SmsObserver????ContentObserver????ContentProvider??????
  private final class SmsObserver extends ContentObserver
  {
    public SmsObserver(Handler handler)
    {
      super(handler);
    }
    
    //???????????????onChange????
    public void onChange(boolean selfChange)
    {  
      sharedPreferences = getSharedPreferences("pairsContact", Activity.MODE_PRIVATE);
      //???????????????????????
      inboxNum = getSharedPreferences("pairsInbox", Activity.MODE_PRIVATE);
      editor = inboxNum.edit();
      Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
      //??????????????????
      int j = 0, i;
      while (cursor.moveToNext())
        j++;
      i = inboxNum.getInt("inbox", 0);
      cursor.close();
      
      //????????????????????????????????????????????
      if(i < j)
      {
        //???????????????????????????????
        cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
        cursor.moveToFirst();
        number = cursor.getString(cursor.getColumnIndex("address"));
        ifeq = cursor.getString(cursor.getColumnIndex("body"));
        
        //????????????
        Cursor contact = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        //???????????????????????
             Boolean mark = false;
             while(contact.moveToNext())
             {
               //????????????????????????????????
            if(sharedPreferences.getBoolean(contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)), false) == true)
            {
              //?????????????????????
              String contactId = contact.getString(contact.getColumnIndex(ContactsContract.Contacts._ID));
                Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
                while(phones.moveToNext())
                {
                  String phonenum = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace("-", "");
                  //????????????????????????????????
                  //????????????+86????????????????
                  if(number.equals(phonenum) || number.equals("+86"+phonenum) || phonenum.equals("+86"+number))
                  {
                    int subHour, subMin;
                    
                    //????????
                    if (ifeq.indexOf("???????") > -1 || ifeq.indexOf("??") > -1 || ifeq.indexOf("???") > -1 || ifeq.indexOf("????") > -1)
                         {
                      readkeyword = getSharedPreferences("pairsKeyWord", Activity.MODE_PRIVATE);
                      writekeyword = readkeyword.edit();
                      writekeyword.putString("body", ifeq);
                      writekeyword.commit();
                      
                      //?????Activity
                           Intent intent = new Intent(SMSReceiver.this, PhotoActivity.class);
                           aManager = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
                           PendingIntent pi = PendingIntent.getActivity(SMSReceiver.this, 0, intent, 0);
                           //?????????????????????????????????
                           Calendar c = Calendar.getInstance();
                           c.setTimeInMillis(System.currentTimeMillis());
                           c.set(Calendar.YEAR, 2000);
                           c.set(Calendar.DAY_OF_MONTH, 23);
                           c.set(Calendar.HOUR_OF_DAY, 1);
                           c.set(Calendar.MINUTE, 1);
                           c.set(Calendar.SECOND, 1);
                           //???????
                           aManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pi);
                         }
                    //??????????????????????
                    else if(ifeq.length() >= 2)
                         {
                      if(ifeq.length() >= 6)
                      {
                        String n = ifeq.substring(2,6);
                        //???????????????????????
                        if(n.matches("[0-9]+"))
                        {
                          subHour = Integer.parseInt(ifeq.substring(2,4));
                          subMin = Integer.parseInt(ifeq.substring(4,6));
                          //????????
                          if(ifeq.substring(0,2).equals("????") && subHour < 24 && subMin < 60)
                          {
                            //??????Activity
                            aManager = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
                                 Intent intent = new Intent(SMSReceiver.this, AlarmActivity.class);
                                 PendingIntent pi = PendingIntent.getActivity(SMSReceiver.this,0,intent,0);
                                 
                                 Calendar now = Calendar.getInstance();
                                 now.getTimeInMillis();

                                 if(now.get(Calendar.HOUR_OF_DAY) <= subHour)
                                 {
                                   if(now.get(Calendar.MINUTE) <= subMin)
                                   {
                                     //???????
                                     Calendar c = Calendar.getInstance();
                                     c.setTimeInMillis(System.currentTimeMillis());
                                     c.set(Calendar.HOUR_OF_DAY, subHour);
                                     c.set(Calendar.MINUTE, subMin);
                                     c.set(Calendar.SECOND, 1);
                                     //???????
                                     aManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pi);
                                     Toast.makeText(SMSReceiver.this, contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))+"??????????"+ifeq.substring(2,4)+":"+ifeq.substring(4,6)+"??????", Toast.LENGTH_SHORT).show();
                                   }
                                   else
                                   {
                                     //???????
                                     Calendar c = Calendar.getInstance();
                                     c.setTimeInMillis(System.currentTimeMillis());
                                     c.set(Calendar.DAY_OF_YEAR, now.get(Calendar.DAY_OF_YEAR) + 1);
                                     c.set(Calendar.HOUR_OF_DAY, subHour);
                                     c.set(Calendar.MINUTE, subMin);
                                     c.set(Calendar.SECOND, 1);
                                     //???????
                                     aManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pi);
                                     Toast.makeText(SMSReceiver.this, contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))+"??????????????"+ifeq.substring(2,4)+":"+ifeq.substring(4,6)+"??????", Toast.LENGTH_SHORT).show();
                                   }
                                 }
                                 else
                                 {
                                   //???????
                                   Calendar c = Calendar.getInstance();
                                   c.setTimeInMillis(System.currentTimeMillis());
                                   c.set(Calendar.DAY_OF_YEAR, now.get(Calendar.DAY_OF_YEAR) + 1);
                                   c.set(Calendar.HOUR_OF_DAY, subHour);
                                   c.set(Calendar.MINUTE, subHour);
                                   c.set(Calendar.SECOND, 1);
                                   //???????
                                   aManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pi);
                                   Toast.makeText(SMSReceiver.this, contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))+"??????????????"+ifeq.substring(2,4)+":"+ifeq.substring(4,6)+"??????", Toast.LENGTH_SHORT).show();
                                 }
                          }
                          //????????
                          else if(ifeq.substring(0,2).equals("????"))
                          {
                            //??????Activity
                            aManager = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
                                 Intent intent = new Intent(SMSReceiver.this, AlarmActivity.class);
                                 PendingIntent pi = PendingIntent.getActivity(SMSReceiver.this,0,intent,0);
                                 //???????
                                 Calendar c = Calendar.getInstance();
                                 c.setTimeInMillis(System.currentTimeMillis());
                                 c.set(Calendar.YEAR, 2000);
                                 c.set(Calendar.DAY_OF_MONTH, 23);
                                 c.set(Calendar.HOUR_OF_DAY, 1);
                                 c.set(Calendar.MINUTE, 1);
                                 c.set(Calendar.SECOND, 1);
                                 //???????
                                 aManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pi);
                          }
                        }
                        //????????
                        else if(ifeq.substring(0,2).equals("????"))
                        {
                          //??????Activity
                          aManager = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
                               Intent intent = new Intent(SMSReceiver.this, AlarmActivity.class);
                               PendingIntent pi = PendingIntent.getActivity(SMSReceiver.this,0,intent,0);
                               //???????
                               Calendar c = Calendar.getInstance();
                               c.setTimeInMillis(System.currentTimeMillis());
                               c.set(Calendar.YEAR, 2000);
                               c.set(Calendar.DAY_OF_MONTH, 23);
                               c.set(Calendar.HOUR_OF_DAY, 1);
                               c.set(Calendar.MINUTE, 1);
                               c.set(Calendar.SECOND, 1);
                               //???????
                               aManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pi);
                        }
                      }
                      //????????
                      else if(ifeq.substring(0,2).equals("????"))
                      {
                        aManager = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
                        //??????Activity
                             Intent intent = new Intent(SMSReceiver.this, AlarmActivity.class);
                             PendingIntent pi = PendingIntent.getActivity(SMSReceiver.this,0,intent,0);
                             //???????
                             Calendar c = Calendar.getInstance();
                             c.setTimeInMillis(System.currentTimeMillis());
                             c.set(Calendar.YEAR, 2000);
                             c.set(Calendar.DAY_OF_MONTH, 23);
                             c.set(Calendar.HOUR_OF_DAY, 1);
                             c.set(Calendar.MINUTE, 1);
                             c.set(Calendar.SECOND, 1);
                             //???????
                             aManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pi);
                      }
                       }
                    //????????????????
                         mark = true;
                         break;
                  }
                }
                phones.close();
                //????????????????
                if(mark == true)
                  break;
            }
          }
          contact.close();
      }
      //?????????????????????????????????????
      editor.putInt("inbox", j);
      editor.commit();  
    }
  }
}




Java Source Code List

com.example.pairs.AlarmActivity.java
com.example.pairs.ChooseContactActivity.java
com.example.pairs.Function.java
com.example.pairs.HelpActivity.java
com.example.pairs.Help.java
com.example.pairs.MainActivity.java
com.example.pairs.OurPage.java
com.example.pairs.Ours.java
com.example.pairs.PhotoActivity.java
com.example.pairs.SMSReceiver.java
com.example.pairs.SetContactActivity.java