0

Refactored my simple dial application and moved my custom 'ServiceReceiver' class so it is nested in my Main Class like so (partial code showing just nested ServiceReceiver class)

public class Main extends Activity implements OnClickListener{


    public class ServiceReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(final Context context, Intent intent) {
            TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            telephony.listen(new PhoneStateListener(){
                @Override
                public void onCallStateChanged(int state, String incomingNumber) {
                    super.onCallStateChanged(state, incomingNumber);
                    Log.d("foo","incomingNumber : "+incomingNumber);

                    EditText incomingNum = (EditText) findViewById(R.id.inputText);

                    incomingNum.setText(incomingNumber);

                }
            },PhoneStateListener.LISTEN_CALL_STATE);
        }
    }

My question is, after doing this, what changes need to be made to the manifest.xml?

Here is my existing (old) manifest file when ServiceReceiver was in a seprate file, class, not nested.

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

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.foo.Main"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".ServiceReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
    </application>

</manifest>
Bachalo
  • 6,965
  • 27
  • 95
  • 189

1 Answers1

0

Did you try something like this:

<receiver android:name="com.example.foo.Main$ServiceReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
</receiver>

and make the Receiver class as static

Manjunath
  • 2,063
  • 2
  • 29
  • 60
  • When I try and make nested ServiceReceiver static get the error Cannot make a static reference to the non-static method findViewById from the type Activity – Bachalo Dec 18 '13 at 18:41
  • But if I omit static, it compiles and I get the following runtime error :Unable to instantiate receiver, Can't instantiate; no empty constructor – Bachalo Dec 18 '13 at 18:47
  • Reference : http://stackoverflow.com/questions/4391974/is-it-possible-to-define-a-broadcast-receiver-as-an-inner-class-in-manifest-file – Manjunath Dec 18 '13 at 18:49
  • Can you try adding an empty constructor. – Manjunath Dec 18 '13 at 18:57