Accéder au contenu principal

How to listen incoming sms on android ?

How to listen incoming sms on android ?

Pour écouter les messages entrant sur Android , nous allons utilisés l'objet appelé broadcastreceiver.
  • Après avoir crée le projet dans Android Studio, nous allons procéder comme suit :
  • nous allons modifier notre fichier manifest.xml et ajouter les permissions pour le recevoir les sms à savoir <uses-permission android:name="android.permission.RECEIVE_SMS" />.
  • nous allons créer un service qui vas se charger d'écouter les messages entrants. pour ceux qui ne connaissent pas bien les services, je vous recommande service.
  • dans notre service, nous allons declarer un objet du type broadcast receiver. il est à noter que dans cette méthode nous allons enregistrer automatique notre broadcast receiver. Ainsi nous allons ajouter :
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (action.equals("android.provider.Telephony.SMS_RECEIVED")) {
                    Bundle bundle = intent.getExtras();
                    if (bundle != null) {
                        Object[] pdus = (Object[]) bundle.get("pdus");
                        SmsMessage[] messages = new SmsMessage[pdus.length];
                        for (int i = 0; i < pdus.length; i++) {
                            messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                        }
                        String strMessageBody = "";
                        String strMessageFrom = "";
                        for (SmsMessage message : messages) {
                            strMessageFrom = message.getDisplayOriginatingAddress();
                            strMessageBody = message.getDisplayMessageBody();
                        }
                         Toast.makeText(context,strMessageFrom,Toast.LENGTH_LONG).show();
                         Toast.makeText(context,strMessageBody,Toast.LENGTH_LONG).show();
                 }
              }
         }
    };
    
  • c'est le Action provider qui permet de "capter" le signal émi par l'arriver d'un sms.
  • Ainsi lorsque message arrive, on nous affiche la source et le contenu du message
  • Il suffit maintenant de lancer notre service dans le oncreate de l'activité principale

dans le prochain article, nous proposerons une méthode assez intelligente pour économiser la batterie car cette méthode a pour inconvénient de tourner continuellement.

Commentaires

Posts les plus consultés de ce blog

Container Database Oracle 12.X what you need to understand

A container is a collection of schemas, objects, and related structures in a multitenant container database (CDB) that appears logically to an application as a separate database. Within a CDB, each container has a unique ID and name. The root container, also called the root, is a collection of schemas, schema objects, and nonschema objects to which all PDBs belong. Every CDB has one and only one root container, named CDB$ROOT, which stores the system metadata required to manage PDBs. All PDBs belong to the root. The root does not store user data. Thus, you must not add user data to the root or modify system-supplied schemas in the root. However, you can create common users and roles for database administration A common user with the necessary privileges can switch between PDBs. A PDB is a user-created set of schemas, objects, and related structures that appears logically to an application as a separate database. Every PDB is owned by SYS, which is a common user in the CDB r...

A Simple Knowledge-Engineering Methodology part one

There is no one “correct” way or methodology for developing ontologies. Here we discuss general issues to consider and offer one possible process for developing an ontology. We describe an iterative approach to ontology development: we start with a rough first pass at the ontology. We then revise and refine the evolving ontology and fill in the details. Along the way, we discuss the modeling decisions that a designer needs to make, as well as the pros, cons, and implications of different solutions. First, we would like to emphasize some fundamental rules in ontology design to which we will refer many times. These rules may seem rather dogmatic. They can help, however, to make design decisions in many cases. There is no one correct way to model a domain— there are always viable alternatives. The best solution almost always depends on the application that you have in mind and the extensions that you anticipate. Ontology development is necessarily an iterative process. Concep...