私はアンドロイド開発の初心者です。だから私は助けが必要です。
電話の状態が鳴っているときにアクティビティを表示し、状態がアイドルまたはオフフックのときにこのアクティビティを強制終了したいと思います。
そこで、Broadcastreceiver を拡張するクラスを作成しました。私は myphonestatelistener クラスを呼び出して PhoneStateListener を拡張します。また、ringingactivity という名前のアクティビティを作成します。
これが私のコードです(MyPhoneStateListener):
public void onCallStateChanged(int state,String incomingNumber){
switch(state)
{
case TelephonyManager.CALL_STATE_IDLE:
Log.d("DEBUG", "IDLE");
Intent killIntentidle = new Intent();
killIntentidle.setAction("RingingOver") ;
ctx.sendBroadcast(killIntentidle);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("DEBUG", "OFFHOOK");
Intent killIntent = new Intent();
killIntent.setAction("RingingOver") ;
ctx.sendBroadcast(killIntent);
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("DEBUG", "RINGING");
Intent dialogIntent = new Intent(ctx , RingingActivity.class) ;
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(dialogIntent);
break;
}
}
私のテストでは、telnet からエミュレーターを呼び出すと、呼び出し中のアクティビティが正常に表示されます。通話を受け入れるか拒否すると、アクティビティは終了します。しかし、エミュレーターを再度呼び出すと、アクティビティを再作成できませんでした。画面では見えません。私は何を間違っていますか?
ここに私の活動コードがあります:
public class RingingActivity extends Activity {
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.ringing_activity);
IntentFilter filter = new IntentFilter();
filter.addAction("RingingOver");
registerReceiver(receiver, filter);
//registerReceiver(receiver,filter) ;
}
public void finish() {
super.finish();
};
}
ご協力いただきありがとうございます!