[英]Android : How to Declare static broadcast receiver
我想知道我们如何通过AndroidManifest.xml注册静态广播接收器
我的班级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
}
}
}
我的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>
关于我的项目,它有两个模块,第二个模块完全是由mainactivity运行的Asynctask线程,我想这个第二个模块的广播接收器。
我正在使用Android Studio 0.4.4,因此需要帮助
您应该给android:name
标签指定完全限定的类名。 请在此处参考SDK文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.