Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to instantiate receiver has no zero argument constructor

Tags:

android

I have some Problem with using BroadCastReceiver.Here comes the AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.zhang.notificationtest">

    <uses-permission android:name="android.permission.RECEIVE_SMS"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".NotificationActivity"></activity>
        <receiver android:name=".NotificationActivity$SMSReceiver">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

and part of my codes

public class NotificationActivity extends AppCompatActivity {

public TextView textViewFrom, textViewContent;
public IntentFilter intentFilter;
public SMSReceiver smsReceiver;

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(smsReceiver);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notification);

    textViewContent = (TextView)findViewById(R.id.textViewContent);
    textViewFrom = (TextView)findViewById(R.id.textViewFrom);

    intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
    smsReceiver = new SMSReceiver();

    registerReceiver(smsReceiver,intentFilter);
}

public class SMSReceiver extends BroadcastReceiver{

    public SMSReceiver(){
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        Object[] pdus = (Object[]) bundle.get("pdus");
        SmsMessage[] smsMessages = new SmsMessage[pdus.length];
        for (int i = 0; i < smsMessages.length; i++)
            smsMessages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
        String from = smsMessages[0].getOriginatingAddress();
        StringBuilder content = new StringBuilder("");
        for (SmsMessage element: smsMessages)
            content.append(element.getMessageBody());
        textViewFrom.setText(from);
        textViewContent.setText(content.toString());
    }
}

} could anyone offer me some help and tell me why it happened?Thanks a lot!

like image 633
Anderson Zhang Avatar asked Sep 20 '16 23:09

Anderson Zhang


2 Answers

Your SMSReceiver is a non-static instance class. That means it can only be constructed in the context of the holding class, NotificationActivity. That's fine, but you can't register it in your manifest since the system would need to construct a NotificationActivity in order to instantiate your receiver to handle the broadcast.

BTW, either register the receiver in the manifest, or register it in your activity, but not both.

like image 70
Jeffrey Blattman Avatar answered Nov 05 '22 17:11

Jeffrey Blattman


SMSReceiver is a non-static inner class, make it static or move it out of NotificationActivity if you want to keep the receiver in the manifest.

When you add the BroadcastReciever to the manifest, you are declaring that it is always registered (so you don't need the register and unregister in your activity)

If you do want to keep it in the manifest, the life cycle of an Activity and BroadcastReceiver are different, so it would be dangerous to access textViewFrom as you are currently doing.

If you want the receiver to be triggered only when the activity is 'alive', remove it from the manifest

like image 35
FunkTheMonk Avatar answered Nov 05 '22 17:11

FunkTheMonk