-2

私はアンドロイド開発の初心者です。だから私は助けが必要です。

電話の状態が鳴っているときにアクティビティを表示し、状態がアイドルまたはオフフックのときにこのアクティビティを強制終了したいと思います。

そこで、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();        
};

}

ご協力いただきありがとうございます!

4

3 に答える 3

0

問題は MyPhoneStateListener コードにあると思われます。他のコードは私には問題ないようです。

次回は PhoneStateListener がどのように生きていないか。2度目の「RINGING」ログは見られますか?

于 2013-01-02T22:38:12.397 に答える
-2

Munish Katoch さん、返信ありがとうございます。解決策はありますが、適切な解決策が見つかるかどうかわかりません。Garbage Collector を使用すると、呼び出しごとに画面にアクティビティが表示されます。これが私のコードです:

public class MyPhoneStateListener extends PhoneStateListener {

Context ctx ;
private boolean iscalling ; 

MyPhoneStateListener(Context ctx)
{
    this.ctx = ctx ;
}

public void onCallStateChanged(int state,String incomingNumber){



    switch(state)
    {
       case TelephonyManager.CALL_STATE_IDLE:
            Log.d("DEBUG", "IDLE");
            if(iscalling == true)
            {
            Intent killIntentidle = new Intent();
            killIntentidle.setAction("RingingOver") ;
            ctx.sendBroadcast(killIntentidle);                
            System.gc() ;
            iscalling = false ;
            }
            break;
       case TelephonyManager.CALL_STATE_OFFHOOK:
            Log.d("DEBUG", "OFFHOOK");    
            if(iscalling == true)
            {                
            Intent killIntent = new Intent();
            killIntent.setAction("RingingOver") ;
            ctx.sendBroadcast(killIntent);      
            System.gc() ;
            iscalling = false ;
            }
            break;
       case TelephonyManager.CALL_STATE_RINGING:
           Log.d("DEBUG", "RINGING");
           iscalling = true ;
           Intent dialogIntent = new Intent(ctx , RingingActivity.class) ;
           dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           ctx.startActivity(dialogIntent);            
           System.gc() ;
           break;
    }
}   

}

于 2013-01-03T20:09:49.310 に答える
-2

ガベージコレクターに電話する必要がある理由がわかりません。アクティビティが作成されないため、根本原因の修正を試みてください。時折。まず、MyPhoneStateListener と RingingActivity にログを入力して問題を分析します。MyPhoneStateListener で、何らかのアクション (アクティビティの開始/強制終了) を実行する前に、RingingActivity アクティビティの状態を出力します。

于 2013-01-03T22:21:59.237 に答える