我正在尝试听手机状态。我在我的清单文件中注册了接收器。但接收器没有被调用。 收到广播后,我想听电话状态并开始服务。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.networkmonitor" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".ConnectvityMonitor" >
<intent-filter>
<!--<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />-->
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<activity
android:name=".NetWorkMonitorActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我的接收器
package com.example.networkmonitor;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class ConnectvityMonitor extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String state="";
Log.d("BroadCast Fired","BroadCast Fired");
Toast.makeText(context,"BroadCast",Toast.LENGTH_SHORT).show();
}
}