我运行下面的代码,它的编译但我没有得到任何结果或吐司显示的请帮助...
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>
“但现在的问题是,我要重新启动设备,以便开始在那里我是错的,现在这个service..any建议?”
你必须重新启动你的手机的原因是,在你的AndroidManifest.xml文件,你已经允许只有一个动作...
<!-- Your Problem is here -->
<action android:name="android.intent.action.BOOT_COMPLETED" />
你已经确立了自己的BroadcastReceiver当手机被引导到只启动。
为了解决这个问题,你需要使用DR VOLT提供了答案。
就在行动PHONE_STATE和NEW_OUTGOING_CALL添加到您的接收器
例如:
<receiver android:name=".MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
于是,这将每当出现这些行动开始你的广播接收器。
日Thnx的答案...我尝试另一种方法和下面的代码似乎work..Joined接收器和phonestatelistener。 但现在的问题是我需要重新启动设备,以启动该服务 ..any建议我在哪里是错误的,现在?
public class IncomingCallReciever extends BroadcastReceiver {
private Context mContext;
private Intent mIntent;
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
mIntent = intent;
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int events = PhoneStateListener.LISTEN_CALL_STATE;
tm.listen(phoneStateListener, events);
}
private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
String callState = "UNKNOWN";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
callState = "IDLE";
break;
case TelephonyManager.CALL_STATE_RINGING:
// -- check international call or not.
if (incomingNumber.startsWith("00")) {
Toast.makeText(mContext,"International Call- " + incomingNumber,Toast.LENGTH_LONG).show();
callState = "International - Ringing (" + incomingNumber+ ")";
} else {
Toast.makeText(mContext, "Local Call - " + incomingNumber, Toast.LENGTH_LONG).show();
callState = "Local - Ringing (" + incomingNumber + ")";
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
String dialingNumber = mIntent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
if (dialingNumber.startsWith("00")) {
Toast.makeText(mContext,"International - " + dialingNumber,Toast.LENGTH_LONG).show();
callState = "International - Dialing (" + dialingNumber+ ")";
} else {
Toast.makeText(mContext, "Local Call - " + dialingNumber,Toast.LENGTH_LONG).show();
callState = "Local - Dialing (" + dialingNumber + ")";
}
break;
}
Log.i(">>>Broadcast", "onCallStateChanged " + callState);
super.onCallStateChanged(state, incomingNumber);
}
};
}
问题是在你的清单文件,你必须有这样的:
<receiver android:name=".MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
代替 :
<receiver android:name=".MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>