Android sms receiver doesnt work

天大地大妈咪最大 提交于 2019-12-12 00:22:41

问题


I've searched a lot on web and on SO I couldn't find an answer to my case or a straightforward tutorial, I was following this my problem is that the constructor doesn't get call nor onReceive().

public class Smsreceiver extends BroadcastReceiver
{
    public Smsreceiver()
    {
        Log.v("constructing", "constructing");
    }

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.v("received", "received");
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String str = "";           
        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]);               
                str += "SMS from " + msgs[i].getOriginatingAddress();                    
                str += " :";
                str += msgs[i].getMessageBody().toString();
                str += "\n";
            }
                //---display the new SMS message---
                Toast.makeText(context, str, Toast.LENGTH_LONG).show();
           }
    }
} 

and manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dwaik.testreceivesms"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" >
    </uses-permission>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver
            android:name=".SmsReceiver"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

I'm building against api-level 15 (4.0.3) and running on xperia neo v (4.0.3), what am I missing? or shall I do anything special beside run to get my service to run?

EDIT

I can't find my service between running apps menu, though it does exist on installed apps menu


回答1:


Register your receiver in your main activity:

String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

SmsReceiver smsReceiver = new SmsReceiver();

registerReceiver(smsReceiver, new IntentFilter(SMS_RECEIVED));

Remember to unregister it at the end.




回答2:


It works if you put the correct name of your class in the manifest.xml. In fact you called: "Smsreceiver" your java class and you try to register a service in the manifest that is called SmsReceiver. Change the name of the class or into the manifest.




回答3:


<receiver android:name="com.example.package_name.HERE YOUR SMS RECEIVER">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>


来源:https://stackoverflow.com/questions/20066500/android-sms-receiver-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!