【问题标题】:Android-nested Class and Manifest fileAndroid 嵌套类和清单文件
【发布时间】:2013-12-18 18:00:50
【问题描述】:

重构了我的简单拨号应用程序并移动了我的自定义“ServiceReceiver”类,使其像这样嵌套在我的主类中(部分代码仅显示嵌套的 ServiceReceiver 类)

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);
        }
    }

我的问题是,在这样做之后,需要对 manifest.xml 进行哪些更改?

这是我现有的(旧)清单文件,当时 ServiceReceiver 位于单独的文件、类中,而不是嵌套的。

<?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>

【问题讨论】:

    标签: android android-manifest


    【解决方案1】:

    你有没有尝试过这样的事情:

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

    并将 Receiver 类设为静态

    【讨论】:

    • 当我尝试将嵌套的 ServiceReceiver 设为静态时出现错误无法从 Activity 类型对非静态方法 findViewById 进行静态引用
    • 但是如果我省略静态,它会编译并且我得到以下运行时错误:无法实例化接收器,无法实例化;没有空的构造函数
    • 你可以尝试添加一个空的构造函数。
    猜你喜欢
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-10
    • 1970-01-01
    • 2019-03-06
    • 1970-01-01
    相关资源
    最近更新 更多