我要听来电和拨出的电话,这个过程应该运行作为service.I制成,其做工精细识别呼入和呼出一个活动,但我需要将其更改为服务,以便它可以运行到background.I不能找出如何改变it.My活动如下:想听听来电和拨出呼叫
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
mTelephonyMgr.listen(new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);
}
class TeleListener extends PhoneStateListener
{
public void onCallStateChanged(int state, String incomingNumber)
{
super.onCallStateChanged(state, incomingNumber);
switch (state)
{
case TelephonyManager.CALL_STATE_IDLE:
//CALL_STATE_IDLE;
Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE", 10000).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//CALL_STATE_OFFHOOK;
Toast.makeText(getApplicationContext(), "CALL_STATE_OFFHOOK", 10000).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
//CALL_STATE_RINGING
Toast.makeText(getApplicationContext(), "CALL_STATE_RINGING", 10000).show();
break;
default:
break;
}
}
}
}
我的第二个问题可我没有一个活动安装服务为我的电话。
另一个活动**从ICS向上,你需要一个活动启动您的服务** 如果我要使用启动此服务ACTION_BOOT_COMPLETED呢?我不想使用任何活动。 – Anirban 2013-04-24 02:03:38
对于ICS和高达你需要从活动启动完成后调用你的接收器启动服务一次。我想这是出于安全原因。 – 2013-04-24 02:09:02
如果我打算让这个活动来服务我应该在哪里把这个 'TelephonyManager mTelephonyMgr =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)另一件事; mTelephonyMgr.listen(新的TeleListener(),PhoneStateListener.LISTEN_CALL_STATE);' – Anirban 2013-04-24 02:11:09