2011-01-09 2 views
19

私のアプリが入ってくるSMSを分析し、それをブロックしたり、何か(多分別のSMSフォルダに移動したり)できるように、Androidでコードを作成するにはどうすればいいですか?私はAndroid 2.1以上をターゲットにしています。Androidで受信SMSを分析するにはどうすればよいですか?

私は、ユーザが指定した迷惑メールの着信SMSを分析したい場合、メッセージを別のフォルダに読み書きするように削除/マークしたいと考えています。

答えて

24

私はBroadcastReceiverとして、このコードを使用します。

public void onReceive(Context context, Intent intent) 
{ 
    //this stops notifications to others 
    this.abortBroadcast(); 

    //---get the SMS message passed in--- 
    Bundle bundle = intent.getExtras(); 
    SmsMessage[] msgs = null; 
    String str = "";    
    if (bundle != null) 
    { 
     //---retrieve the SMS message received--- 
     Object[] pdus = (Object[]) bundle.get("pdus"); 
     msgs = new SmsMessage[pdus.length];    
     for (int i=0; i<msgs.length; i++){ 
      msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);     
      str += "SMS from " + msgs[i].getOriginatingAddress(); 
      from = msgs[i].getOriginatingAddress(); 
      str += " :"; 
      str += msgs[i].getMessageBody().toString(); 
      msg = msgs[i].getMessageBody().toString(); 
      str += "\n"; 
     } 
     if(checksomething){ 
      //make your actions 
      //and no alert notification and sms not in inbox 
     } 
     else{ 
      //continue the normal process of sms and will get alert and reaches inbox 
      this.clearAbortBroadcast(); 
     } 
    } 

はマニフェストにそれを追加し、ブロードキャストまたはSMSのhiggestの優先順位(100)を追加することを忘れないで受信トレイに最初に行くとアラート通知を取得します。

<receiver android:name=".SmsReceiver"> 
     <intent-filter android:priority="100"> 
      <action android:name="android.provider.Telephony.SMS_RECEIVED"></action> 
     </intent-filter> 
    </receiver> 

希望します。

0

まあuは、トラップの受信SMSをすることができますが、私はuが通知をブロックすることができなくなると思う.....
uはここにSMSを削除したい場合は助けることができるスレッドです....
How to delete an SMS from the inbox in Android programmatically?

+0

私のアプリケーションが着信SMSをすでに処理している場合は、デフォルトの「新しいメッセージ」通知を表示しないようにAndroidが必要です。だからそれは最高のユーザーエクスペリエンスのための重要な要件です:( –

+0

よくStackOverflow上の多くのスレッドは、それは隠されているそれらの独自の拡張クラスを使用せずには不可能だと言うだろう.......しかし、 – viv

+1

今すぐabortBroadcast();メソッドを使用して、受信メッセージを受信ボックスに移動するように制限することができます。 – AB1209

0

このコードは2.3.3のデバイスで動作します。 HTC MyTouch 4gスライド。 abortBroadcastはステータスバーのnotificationsound + notificationを抑制します。+ SMSは受信ボックスに移動できません。 一部のユーザーは、実際のデバイスでは動作せず、常にエミュレータでしか動作しないと述べています。優先度が100の場合、この特定のデバイスでは、コードは期待どおりに機能します。