I want to create an android application, which is received SMS only , not receive native Message apps . But I receive SMS , and native Message apps will receive . My coce:..
SMSreceiver.java
public class SMSreceiver extends BroadcastReceiver{
private final String TAG = "MYSMSRECEIVE";
final SmsManager sms = SmsManager.getDefault();
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Bundle extras = intent.getExtras();
StringBuilder sb = new StringBuilder();
String strMessage = "";
String strMsgBody = "";
String strMsgSrc = "";
if ( extras != null )
{
Object[] smsextras = (Object[]) extras.get( "pdus" );
for ( int i = 0; i < smsextras.length; i++ )
{
SmsMessage smsmsg = SmsMessage.createFromPdu((byte[])smsextras[i]);
strMsgBody = smsmsg.getMessageBody().toString();
strMsgSrc = smsmsg.getOriginatingAddress();
sb.append(smsmsg.getDisplayOriginatingAddress());
sb.append(smsmsg.getDisplayMessageBody());
strMessage += "SMS from " + strMsgSrc + " : " + strMsgBody;
Log.d("abcd", "estimateGlass estimateGlass::" +strMessage);
Toast toast = Toast.makeText(context, "senderNum: "+ strMessage + ", message: ", Toast.LENGTH_LONG);
toast.show();
}
int icon = R.drawable.inbox;
CharSequence tickerText = strMsgSrc + ": " + strMsgBody;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "New SMS Message";
CharSequence contentText = sb.toString();
Intent notificationIntent = new Intent();
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.vibrate = new long[] { 100, 250, 100, 500};
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(1, notification);
}
}
}
Manifest file :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sms.testreceiveservies"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.sms.testreceiveservies.SMSreceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<service
android:name=".ServiceCommunicator"
android:enabled="true" />
</application>
Please help me
Pre KitKat 4.4, you can simply call abortBroadcast() but make sure you have a high priority on your receiver. Post KitKat you cannot do that, unless your are the default messaging chosen by user.
<intent-filter android:priority="1000"><!-- Or max int if you will -->