我是 android 新手,我已经创建了这样的意图 -
<receiver android:name=".IncommigCallListener" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<receiver android:name=".OutgoingCallReciever" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
现在我创建了这样的服务-
<service
android:name=".CallLogger"
android:exported="false"/>
班级CallLogger
public class CallLogger extends IntentService {
public CallLogger(String name) {
super(name);
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent arg0) {
// TODO Auto-generated method stub
System.out.println("service started");
}
}
我不想在我的应用程序中进行任何活动,我只想启动该服务,以便它可以在后台工作并接收PHONE_STATE
和NEW_OUTGOING_CALL
意图。
当我启动此应用程序时,它不会记录任何内容PHONE_STATE
或NEW_OUTGOING_CALL
意图。
如何在不使用任何活动的情况下在后台启动服务?
编辑 :
public class OutgoingCallReciever extends BroadcastReceiver {
@Override
public void onReceive(Context ctx, Intent intent) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
}
}
和
public class IncommigCallListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
String incommingCallNumber = incomingNumber;
System.out.println("incomming call : " + incomingNumber);
break;
}
}
}