2012-03-05 31 views
0

私は、ブロードキャスト受信機を使用してSMSを受信または配信したときに通知するアプリを作成したいと考えていますか?ありがとうございます。SMSプッシュ通知

+0

はhttp://stackoverflow.com/questions/4117701/android-sms-broadcastに違いがありますレシーバー? – Tim

答えて

1

この パブリッククラスsmsActivityはBroadcastReceiver API 7イメージを実行して、エミュレータでテスト{

 protected static final String TAG = "PlayingAudio"; 



     @Override 
     public void onReceive(Context context, Intent intent) 
     { 
      this.mContext = context; 
      mFileName = Environment.getExternalStorageDirectory().getAbsolutePath(); 
      mFileName += "/audiorecordtest.3gp"; 
      //---get the SMS message passed in--- 
      Bundle bundle = intent.getExtras();   

      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]); 
        incomingnum=msgs[i].getOriginatingAddress(); 
        MsgBody=msgs[i].getMessageBody().toString(); 
        //str += "SMS from " +incomingnum;      
        //str += " :"; 
        str += MsgBody; 
        // str += "\n";   
        System.out.println("Str is "+str); 
       } 
Notification notification = new Notification(android.R.drawable.ic_popup_reminder, 
          "My Notification", System.currentTimeMillis()); 
        notification.defaults |= Notification.DEFAULT_SOUND; 
        notification.defaults |= Notification.DEFAULT_VIBRATE;*/ 

       } 
      } 

     } 

    } 
+0

ありがとうございます。しかし、コードは実行されていません。 – swanand

1

を拡張してみてください。

レシーバクラス:

package add.yourpackage.here; 

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.telephony.SmsMessage; 
import android.util.Log; 

public class SmsReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Bundle bundle = intent.getExtras(); 

     if (bundle != null) { 
      StringBuilder smsSummary = new StringBuilder("SMS received from: "); 

      Object[] pdus = (Object[]) bundle.get("pdus"); 
      SmsMessage[] msgs = new SmsMessage[pdus.length]; 
      for (int i = 0; i < msgs.length; i++) { 
       msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); 

       final String sender = msgs[i].getOriginatingAddress(); 
       final String message = msgs[i].getMessageBody().toString(); 
       smsSummary.append(sender); 
       smsSummary.append("; ").append(message).append("\n"); 

       Log.d("SMS_RECEIVER", "Str is " + smsSummary); 
      } 

      NotificationManager notifManager = (NotificationManager) context 
        .getSystemService(Context.NOTIFICATION_SERVICE); 

      Notification notif = new Notification(
        android.R.drawable.ic_popup_reminder, smsSummary, 
        System.currentTimeMillis()); 
      notif.defaults |= Notification.DEFAULT_SOUND; 
      notif.defaults |= Notification.DEFAULT_VIBRATE; 
      notif.defaults |= Notification.DEFAULT_LIGHTS; 

      // The notification will be canceled when clicked by the user... 

      notif.flags |= Notification.FLAG_AUTO_CANCEL; 

      // ...but we still need to provide and intent; an empty one will 
      // suffice. Alter for your own app's requirement. 

      Intent notificationIntent = new Intent(); 
      PendingIntent pi = PendingIntent.getActivity(context, 0, 
        notificationIntent, 0); 
      notif.setLatestEventInfo(context, "SMS Notification", 
        "Another txt for you", pi); 

      notifManager.notify(0, notif); 
     } 
    } 
} 

あなたが代わりにNotification.Builderを使用することができ11+ APIを実行しているが、私の携帯電話は、ジンジャーブレッドの上に残っている場合。

受信者のマニフェストエントリ。あなたのアプリはまだSMSメッセージを受信するために定義された権限が必要になりますが、私はすでにそれをやったと仮定します。

<receiver 
     android:name=".service.SmsReceiver" 
     android:enabled="true" > 
     <intent-filter> 
      <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
      <action android:name="android.intent.action.DATA_SMS_RECEIVED" /> 
     </intent-filter> 
    </receiver> 
+0

私はAPI 8に取り組んでいます。 – swanand

+1

これは大丈夫ですが、このコードはAPI 8でもうまく動作します:-)。私がインストールした最低のAPIであり、オリジナルの質問に1つも書いていないので、私は7でしかテストしませんでした。 – Chilledrat