这是我的 phonestate 类,我在其中检查手机状态变化
public class CustomPhoneStateListener extends PhoneStateListener
{
private static final String TAG = "CustomPhoneStateListener";
Context context; //Context to make Toast if required
public CustomPhoneStateListener(Context context)
{
super();
this.context = context;
}
public void onCallStateChange(int state, String incomingNumber)
{
Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");
Log.v(TAG, incomingNumber);
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG, "RINGING");
Toast.makeText(context, "Incoming number :"+incomingNumber, Toast.LENGTH_LONG).show();
break;
} }
这是我与广播连接的广播接收器类
public class CustomBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "CustomBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Bundle bundle = intent.getExtras();
String phoneNr= bundle.getString("incoming_number");
Log.v(TAG, "phoneNr: "+phoneNr);
}
我有一些想法在java文件中定义意图,bcz 3.0以上的android版本需要定义意图。
Intent intent = new Intent();
intent.setAction("com.example.SendBroadcast");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(intent);
我不知道如何以及在哪里定义这部分代码,无论是在广播类还是 phonestate 类中。