我目前正在尝试创建一个简单的应用程序,记录您通话的分钟数,并在接近免费通话时间时发出警告。
此时,我已经创建了一个名为CallService.java的服务,该服务在用户拨打电话时被调用。此服务仅记录通话的开始时间和结束时间。使用一个名为OutgoingCallReciever.Java的类启动该服务,该类等待用户拨打电话然后启动CallService。
现在,我正在尝试在用户手机未通话时停止CallService。即(电话状态为空闲、摘机或有人呼叫用户),但我不知道如何做到这一点(我是Java/Android的新手)。我是否可以使用PhoneStateListener 的 onCallStateChanged 方法?(我不确定如何使用它...)
希望您能提供帮助!
以下是相关的类:
MainActivity.java
此时,我已经创建了一个名为CallService.java的服务,该服务在用户拨打电话时被调用。此服务仅记录通话的开始时间和结束时间。使用一个名为OutgoingCallReciever.Java的类启动该服务,该类等待用户拨打电话然后启动CallService。
现在,我正在尝试在用户手机未通话时停止CallService。即(电话状态为空闲、摘机或有人呼叫用户),但我不知道如何做到这一点(我是Java/Android的新手)。我是否可以使用PhoneStateListener 的 onCallStateChanged 方法?(我不确定如何使用它...)
希望您能提供帮助!
以下是相关的类:
MainActivity.java
package com.fouadalnoor.callcounter;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.telephony.TelephonyManager;
import android.telephony.PhoneStateListener;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
PhoneStateListener ps =(PhoneStateListener) getSystemService(TELEPHONY_SERVICE);
Toast.makeText(this, "Phone State = " + tm.getCallState() , Toast.LENGTH_LONG).show();
CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
stopService (new Intent(buttonView.getContext(),CallService.class));
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
OutgoingCallReciever.java
package com.fouadalnoor.callcounter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class OutgoingCallReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, CallService.class));
}
}
CallService.java
package com.fouadalnoor.callcounter;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import java.lang.String;
public class CallService extends Service {
public long startTime,endTime, totalTime;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
Toast.makeText(this, "Call Service stopped", Toast.LENGTH_LONG).show();
endTime = System.currentTimeMillis()/1000;
Toast.makeText(this, "endTime = " + endTime, Toast.LENGTH_LONG).show();
totalTime = endTime-startTime;
Toast.makeText(this, "totalTime = " + totalTime , Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "Call Service started by user.", Toast.LENGTH_LONG).show();
startTime = System.currentTimeMillis()/1000;
Toast.makeText(this, "startTime = "+ startTime, Toast.LENGTH_LONG).show();
}
}