简体   繁体   English

Android:如何声明静态广播接收器

[英]Android : How to Declare static broadcast receiver

i want to know how can we register static broadcast receiver through AndroidManifest.xml 我想知道我们如何通过AndroidManifest.xml注册静态广播接收器

My class BroadcastReceiver class 我的班级BroadcastReceiver班级

public class YourBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (bundle == null)
            return;
        String phoneNumber = null;

        // Incoming call
        String state = bundle.getString(TelephonyManager.EXTRA_STATE);
        if ((state != null)&&(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))) {
            phoneNumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
            // Here: do something with the number
        }
        // Outgoing call
        else if (state == null) {
            phoneNumber = bundle.getString(Intent.EXTRA_PHONE_NUMBER);
            // Here: do something with the number
        }
    }
}

My AnrdoidMainfest.xml files in which i get error ( class or interface expected ) on that line <receiver android:name=".YourBroadcastReceiver"> 我的AnrdoidMainfest.xml文件,在该行中我收到错误(类或接口预期) <receiver android:name=".YourBroadcastReceiver">

<?xml version="1.0" encoding="utf-8"?>
<!--suppress ALL -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gfun.Andp"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />
    <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.gfun.Andp.main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".YourBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE"></action>
            </intent-filter>
        </receiver>
    </application>
</manifest>

about my project , it have two modules and this second module is entirely Asynctask thread run by mainactivity and i want this broadcast receiver for this second module . 关于我的项目,它有两个模块,第二个模块完全是由mainactivity运行的Asynctask线程,我想这个第二个模块的广播接收器。
am using android studio 0.4.4 so need help 我正在使用Android Studio 0.4.4,因此需要帮助

You should give a fully qualified class name to android:name tag. 您应该给android:name标签指定完全限定的类名。 Refer to sdk docs here 请在此处参考SDK文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM