当我的Android应用程序运行时,我接到一个电话,要么我参加通话,要么取消它我的应用程序在后台关闭。
我希望我的应用在后台运行。
当我接到onStop方法的电话时,我能够看到我添加的Toast消息
@Override
protected void onStop() {
super.onStop();
Toast.makeText(getApplicationContext(), "onStop()", Toast.LENGTH_LONG).show();
}
电话结束后我该怎么办?我需要打开我的应用程序吗?
这是我的onResume
的代码片段@Override
protected void onResume() {
super.onResume();
Toast.makeText(getApplicationContext(), "on Resume being called", Toast.LENGTH_LONG).show();
}
这在活动开始时被调用
@Override
protected void onPause() {
super.onPause();
}
我应该在onPause和onResume中添加什么才能返回我的应用
答案 0 :(得分:0)
使用电话管理器跟踪电话呼叫状态:
public class Main_Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager tele = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener call = new PhoneStateListener(){
public void onCallStateChanged (int state , String incomingNumber){
if(state==TelephonyManager.CALL_STATE_RINGING)
{
Log.e ("Phone State" , "_________________Phone is Ringing");
Toast.makeText(getBaseContext(), "Ringing", Toast.LENGTH_SHORT).show();
}
else if (state==TelephonyManager.CALL_STATE_OFFHOOK)
{
Log.e ("Call" , "_________________Phone Call");
Toast.makeText(getBaseContext(), "On Call", Toast.LENGTH_SHORT).show();
}
else if (state==TelephonyManager.CALL_STATE_IDLE)
{
Log.e ("Call" , "_________________No Call");
Toast.makeText(getBaseContext(), "Ideal", Toast.LENGTH_SHORT).show();
}
}
};
tele.listen(call,PhoneStateListener.LISTEN_CALL_STATE);
}
对我而言,它完美无缺。
答案 1 :(得分:0)
您可以使用BroadcastReceiver来收听手机状态。并在电话结束时恢复您的应用。 所以你应该添加一个PhoneStateBroadcastReceiver
添加到清单文件
<receiver android:name=".PhoneStateBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
PhoneStateBroadcastReceiver类
public class PhoneStateBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);
}
}