我正在运行以下代码,它正在编译,但我没有得到任何结果或 toast 显示请帮助...
CustomBroadcastReceiver.java 此类将接收动作电话状态更改并将实例化customephonestatelistener
public class CustomBroadcastReceiver extends BroadcastReceiver {
TelephonyManager telephony =
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener(context);
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Bundle bundle = intent.getExtras();
String phoneNr= bundle.getString("incoming_number");
Log.v(TAG, "phoneNr: "+phoneNr);
Toast toast = Toast.makeText(context, "phoneNr: "+phoneNr, Toast.LENGTH_LONG);
toast.show();
}
}
CustomPhonestateListener.java
这个类是主要的操作类
public class CustomPhoneStateListener extends PhoneStateListener {
private static final String TAG = "CustomPhoneStateListener";
private Context mContext;
public CustomPhoneStateListener(Context context) {
// TODO Auto-generated constructor stub
mContext = context;
}
public void onCallStateChanged(int state, String incomingNumber){
Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");
Log.v(TAG, incomingNumber);
Toast toast = Toast.makeText(mContext, "WE ARE INSIDE!!!!!!!!!!!", Toast.LENGTH_LONG);
toast.show();
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG, "RINGING");
break;
}
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>