Java 如何为3.0以上的android版本定义广播接收器?
这是我的phonestate类,我在其中检查电话状态更改Java 如何为3.0以上的android版本定义广播接收器?,java,android,android-intent,Java,Android,Android Intent,这是我的phonestate类,我在其中检查电话状态更改 public class CustomPhoneStateListener extends PhoneStateListener { private static final String TAG = "CustomPhoneStateListener"; Context context; //Context to make Toast if required public CustomPhoneStateListen
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 android版本
超过3.0的需求
定义意图
Intent intent = new Intent();
intent.setAction("com.example.SendBroadcast");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(intent);
我不知道如何定义这部分代码,也不知道在哪里定义这部分代码
广播类或电话状态类。显然!!将此代码调用到CustomPhoneStateListener类中,其他wise广播将永远不会调用。非常抱歉,我无法正确理解,,,我需要将其放入mainactivity类中,然后调用phonestatelistener,或者必须将其放入phonestatelistener中。。。。。。感谢您的回复..我在oncreate()内的mainactivity中尝试了这一点,并在phonestatelistener mainactivity m=new mainactivity()中调用了它;但它仍然不起作用,请在这方面帮助我