我想创建一个Android应用程序,它只接收短信,不接收本机消息应用程序。但我收到短信,本机消息应用程序将收到。 我的意思是:..
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);
}
}
}
清单文件:
<?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>
请帮帮我
答案 0 :(得分:2)
Pre KitKat 4.4,您只需调用abortBroadcast(),但请确保您的接收器具有高优先级。发布KitKat你不能这样做,除非你是用户选择的默认消息。
<intent-filter android:priority="1000"><!-- Or max int if you will -->