エミュレータの電話の状態をモニタリングするプログラムを作成しましたが
エミュレータの電話を押下してもモニタリングできませんでした。
原因を教えていただけないでしょうか?
android studio 4.0.1
■エミュレータ:
avd Pie Android 9.0 X86
■作成したソース
MainActivity.java
package com.example.sample;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.PhoneStateListener;
import com.example.sample.PhoneStateReceiver;
import com.mkyong.android.R;
public class MainActivity extends Activity{
TelephonyManager manager;
PhoneStateReceiver.PhoneReceiver myPhoneStateListener;
@Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myPhoneStateListener = new PhoneStateReceiver.PhoneReceiver(this); manager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); manager.listen(myPhoneStateListener,PhoneStateListener.LISTEN_DATA_CONNECTION_STATE); } @Override public void onResume(){ super.onResume(); manager.listen(myPhoneStateListener,PhoneStateListener.LISTEN_CALL_STATE); } @Override public void onPause(){ super.onPause(); manager.listen(myPhoneStateListener,PhoneStateListener.LISTEN_NONE); }
}
PhoneStateReceiver.java
package com.example.sample;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class PhoneStateReceiver extends BroadcastReceiver{
TelephonyManager manager;
PhoneReceiver myPhoneStateListener;
static boolean alreadyListening = false;
@Override public void onReceive(Context context, Intent intent){ myPhoneStateListener = new PhoneReceiver(context); manager = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)); //---do not add the listener more than once--- if (!alreadyListening){ manager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); alreadyListening = true; } } public static class PhoneReceiver extends PhoneStateListener{ Context context; public PhoneReceiver(Context context){ this.context = context; } @Override public void onCallStateChanged(int state, String incommingNumber){ super.onCallStateChanged(state, incommingNumber); switch(state){ case TelephonyManager.CALL_STATE_IDLE: Toast.makeText(context, "idle", Toast.LENGTH_LONG).show(); break; case TelephonyManager.CALL_STATE_RINGING: Toast.makeText(context, "Ringing: " + incommingNumber,Toast.LENGTH_LONG).show(); break; case TelephonyManager.CALL_STATE_OFFHOOK: Toast.makeText(context, "Offhook", Toast.LENGTH_LONG).show(); break; } } @Override public void onDataConnectionStateChanged(int State) { super.onDataConnectionStateChanged(State); switch(State){ case TelephonyManager.CALL_STATE_IDLE: Toast.makeText(context, "idle", Toast.LENGTH_LONG).show(); break; case TelephonyManager.CALL_STATE_RINGING: Toast.makeText(context, "Ringing",Toast.LENGTH_LONG).show(); break; case TelephonyManager.CALL_STATE_OFFHOOK: Toast.makeText(context, "Offhook", Toast.LENGTH_LONG).show(); break; } } }
}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。