阻止传入文本(Android)

前端之家收集整理的这篇文章主要介绍了阻止传入文本(Android)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个应用程序,希望能够阻止传入的文本消息(取决于用户设置),但我无法检测传入的消息.

你介意看我的代码,让我知道我做错了什么吗?
我正在查看与此类似的其他问题,但我找不到任何详细的答案或足够的信息供我参考.

  1. import android.content.BroadcastReceiver;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5.  
  6. public class SmsReceiver extends BroadcastReceiver{
  7.  
  8. public void onReceive(Context context,Intent intent) {
  9. if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
  10. Bundle bundle = intent.getExtras();
  11. if (bundle != null){
  12. abortBroadcast();
  13. }
  14. }
  15. }
  16.  
  17. }

这是我的清单

  1. <receiver android:name=".listener.SmsReceiver">
  2. <intent-filter android:priority="100">
  3. <action android:name="android.provider.Telephony.SMS_RECEIVED" />
  4. </intent-filter>
  5. </receiver>

我一直在关注MobiForge(http://mobiforge.com/developing/story/sms-messaging-android)的教程以及这里的问题:

How to block an incoming message in android?

Android – Listen For Incoming SMS Messages

任何人都能指出我在正确的方向吗?
我会很感激.

解决方法

这是我用来阻止传入文本的内容.
这就是我回答我的问题的方法.

SmsReceiver.java

  1. import android.content.BroadcastReceiver;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.telephony.SmsMessage;
  6. import android.widget.Toast;
  7.  
  8. public class BroadCastReceiver extends BroadcastReceiver
  9. {
  10. /** Called when the activity is first created. */
  11. private static final String ACTION = "android.provider.Telephony.SEND_SMS";
  12. public static int MSG_TPE=0;
  13. public void onReceive(Context context,Intent intent)
  14. {
  15. String MSG_TYPE=intent.getAction();
  16. if(MSG_TYPE.equals("android.provider.Telephony.SMS_RECEIVED"))
  17. {
  18. // Toast toast = Toast.makeText(context,"SMS Received: "+MSG_TYPE,Toast.LENGTH_LONG);
  19. // toast.show();
  20.  
  21. Bundle bundle = intent.getExtras();
  22. Object messages[] = (Object[]) bundle.get("pdus");
  23. SmsMessage smsMessage[] = new SmsMessage[messages.length];
  24. for (int n = 0; n < messages.length; n++)
  25. {
  26. smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
  27. }
  28.  
  29. // show first message
  30. Toast toast = Toast.makeText(context,"BLOCKED Received SMS: " + smsMessage[0].getMessageBody(),Toast.LENGTH_LONG);
  31. toast.show();
  32. abortBroadcast();
  33. for(int i=0;i<8;i++)
  34. {
  35. System.out.println("Blocking SMS **********************");
  36. }
  37.  
  38. }
  39. else if(MSG_TYPE.equals("android.provider.Telephony.SEND_SMS"))
  40. {
  41. Toast toast = Toast.makeText(context,"SMS SENT: "+MSG_TYPE,Toast.LENGTH_LONG);
  42. toast.show();
  43. abortBroadcast();
  44. for(int i=0;i<8;i++)
  45. {
  46. System.out.println("Blocking SMS **********************");
  47. }
  48.  
  49. }
  50. else
  51. {
  52.  
  53. Toast toast = Toast.makeText(context,"SIN ELSE: "+MSG_TYPE,Toast.LENGTH_LONG);
  54. toast.show();
  55. abortBroadcast();
  56. for(int i=0;i<8;i++)
  57. {
  58. System.out.println("Blocking SMS **********************");
  59. }
  60.  
  61. }
  62.  
  63. }
  64.  
  65. }

AndroidManifest.xml中

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="APP.PACKAGE.NAMEHERE"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6.  
  7. <uses-sdk android:minSdkVersion="10" />
  8.  
  9. <supports-screens
  10. android:largeScreens="true"
  11. android:normalScreens="true"
  12. android:smallScreens="true"
  13. android:resizeable="true"
  14. android:anyDensity="true" />
  15.  
  16. <uses-feature android:name="android.hardware.telephony" />
  17. <uses-permission android:name="android.permission.READ_SMS" />
  18. <uses-permission android:name="android.permission.WRITE_SMS" />
  19. <uses-permission android:name="android.permission.SEND_SMS" />
  20. <uses-permission android:name="android.permission.RECEIVE_SMS" />
  21. <uses-permission android:name="android.permission.RECEIVE_MMS" />
  22.  
  23. <application
  24. android:icon="@drawable/ic_launcher"
  25. android:label="@string/app_name" >
  26. <activity
  27. android:name=".APPACTIVITYHERE"
  28. android:label="@string/app_name"
  29. android:configChanges="orientation|keyboardHidden" >
  30.  
  31.  
  32. <service android:name=".MyService" android:enabled="true"/>
  33. <receiver android:name="SmsReceiver">
  34. <intent-filter android:priority="2147483647">
  35. <action android:name="android.provider.Telephony.SMS_SENT"/>
  36. </intent-filter>
  37. </receiver>
  38.  
  39. <service android:name=".MyServiceSentReceived" android:enabled="true"/>
  40. <receiver android:name="SmsReceiver">
  41. <intent-filter android:priority="2147483645">
  42. <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
  43. </intent-filter>
  44. </receiver>
  45.  
  46. </application>

从清单中拿走的主要内容是服务块,接收器块和权限.

猜你在找的Android相关文章