SMS_RECEIVED not working on Ice Cream Sandwich?

前端 未结 6 980
猫巷女王i
猫巷女王i 2021-01-03 22:51

I\'m trying to use android.provider.Telephony.SMS_RECEIVED to catch incoming SMS\'s.

I built a simple app, which works on 2.x, but when I try it on my 4.0 emulator o

相关标签:
6条回答
  • 2021-01-03 23:29

    in case you try "catch" in backgroung you can see this post.

    "android.provider.Telephony.SMS_RECEIVED" work great from Service. otherwise only during activity lifecycle you can "catch" it

    0 讨论(0)
  • 2021-01-03 23:31

    Make sure that you are actually creating and registering the receiver in an Activity or a Service, otherwise it will not get called (I believe).

    A very simple example of this might be:

    public class MyActivity extends Activity {
    
        private BroadcastReceiver receiver;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            IntentFilter filter = new IntentFilter();
            filter.addAction("android.provider.Telephony.SMS_RECEIVED");
            //Extends BroadcastReceiver
            receiver = new MyFirstApp();
            registerReceiver(receiver,filter);
        }
    
    
       //Also, to save headaches later
       @Override
       protected void onDestroy() {
            unregisterReceiver(receiver);
       }
    }
    

    I can't promise this will work, but I believe it will fix some things. If you have any questions about stuff, just ask in comments. I believe you are correct in saying that it is not even getting called because your receiver is not registered to anything. If you want it to run in the background consider using a service. I really hope this helps and best of luck with your endeavors!

    0 讨论(0)
  • 2021-01-03 23:31

    You just need to exported value true for the reciever.

    <receiver android:name=".MyFirstApp" exported="true">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
            </intent-filter>
    </receiver>
    
    0 讨论(0)
  • 2021-01-03 23:41

    This may help you..try this.. In brodcast receiver class

     public static final String SMS_BUNDLE = "pdus";
    
    
    public void onReceive(Context context, Intent intent)
    {
    
         Bundle intentExtras = intent.getExtras();
            if (intentExtras != null) {
                Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
                String smsMessageStr = "";
                for (int i = 0; i < sms.length; ++i) {
                    SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
    
                    smsBody = smsMessage.getMessageBody().toString();
                    address = smsMessage.getOriginatingAddress();
    
                    smsMessageStr += "SMS From: " + address + "\n";
                    smsMessageStr += smsBody + "\n";
                }
                Toast.makeText(context, smsMessageStr, Toast.LENGTH_SHORT).show();
        }
    
    0 讨论(0)
  • 2021-01-03 23:47

    I think your error is that you use the class name in your package name.

    In your manifest you wrote package="com.giggsey.MyFirstApp" and also <receiver android:name=".MyFirstApp"> in your receiver. This would mean that the full name of your receiver is com.giggsey.MyFirstApp.MyFirstApp, but I belive it is just com.giggsey.MyFirstApp.

    Exchange com.giggsey.MyFirstApp in your manifest with com.giggsey that sould work if I guess right.

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
             package="com.giggsey" android:versionCode="1" android:versionName="1.0">
    [...]
    

    And also this:

    package com.giggsey;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    
    public class MyFirstApp extends BroadcastReceiver {
        private static final String TAG = "MyFirstApp";
    
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(TAG, "Intent recieved: " + intent.getAction());
        }
    }
    
    0 讨论(0)
  • 2021-01-03 23:47

    in the On receive please try to add the following line

    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    
    if (intent.getAction() == SMS_RECEIVED) {
        //any action you want here..
        Toast.makeText(MyClass.this, "SMS RECEIVED",Toast.LENGTH_LONG).show();
    }
    
    0 讨论(0)
提交回复
热议问题