不工作每次都是PhoneStateListener Android

时间:2014-08-01 05:47:32

标签: android performance android-layout broadcastreceiver phone-state-listener

我开发了Full Caller Id的应用程序。在来电/未接电话时的动态屏幕通话中。现在这是一次工作。当我接到电话。比之后不工作或不拨打任何动态屏幕来电/未接来电。


我对我的问题非常困惑。

switch (state) {
    case TelephonyManager.CALL_STATE_IDLE:
        Log.v("idle state", "CALL_STATE_IDLE");
        // CALL_STATE_IDLE;

        if(ring == true && callReceived == true && CheckMissCall.isReject == true)
        {
            Intent inter = new Intent(c, callLog.class);
            inter.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);          
            c.startActivity(inter); 
        }
        if (ring == true && callReceived == false && CheckMissCall.isRunning== false) {

            Log.v("missed call", "Missed call from : " + incomingNumber);
            if(CheckMissCall.isShown)
            {
                c.stopService(new Intent(c, Unlock_hud.class));

            }

            flag = true;
            if (prefs.getBoolean("main_state", true))
            {
                Intent inter = new Intent(c, MissCall.class);
                inter.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                inter.putExtra("CellNumber", incomingNumber);
                c.startActivity(inter);
            }

        }



        break;
    case TelephonyManager.CALL_STATE_OFFHOOK:
        // CALL_STATE_OFFHOOK;
        callReceived = true;
        Log.v("call received", "CALL_STATE_OFFHOOK  " + incomingNumber);


        break;
    case TelephonyManager.CALL_STATE_RINGING:
        ring = true;

        // CALL_STATE_RINGING
        Log.v("call ringing", "CALL_STATE_RINGING  " + incomingNumber);
        Log.d("flags", "flags: " + flag);
        if (flag) {

            //cut = true;
            //flag = false;
            CheckMissCall call = new CheckMissCall(c);
            call.setName(incomingNumber);
            call.setNumber4Sms(incomingNumber);
            call.setContactPhoto();

            Log.d("main", "state ringing");
            //prefs = PreferenceManager.getDefaultSharedPreferences(c);

            if (!prefs.getBoolean("main_state", false)) {

                return;
            }
        /*  if (!isScreenOn && CheckMissCall.isRunning) {

                return;
            }*/
            if (CheckMissCall.isRunning) {

                return;
            }
            else {
                Log.d("main", "EEEEEEEEEEEEEEEE:  Unlock hud");
                Intent in = new Intent(c, Unlock_hud.class);
                in.setFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
                c.startService(in);

                // c.stopService(new Intent(c, Unlock_hud.class));
            }
        }
        break;

    }

1 个答案:

答案 0 :(得分:0)

这是因为当有任何来电时,您的应用会进入后台,而当您第一次接到来电时,您的MyPhoneStateListener会听取来电并结束。 现在做一件事..在service运行你的代码它将在后台运行,它将调用你的动态屏幕 在你的主要活动启动服务中我已经使用了切换按钮(启用或禁用服务)

tgl_switch.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(tgl_switch.isChecked()){
                 startService(new Intent(getApplicationContext(),LogsService.class));
                Toast.makeText(getApplicationContext(), "Call Logger Active",
                       Toast.LENGTH_SHORT).show();

            }else{
                stopService(new Intent(getApplicationContext(),LogsService.class));
                Toast.makeText(getApplicationContext(), "Call Logger Deactive",
                           Toast.LENGTH_SHORT).show();
            }
        }});

例如我做了这个

public class LogsService extends Service {

  String name;
  String phNumber;
 String callType;
 String callDate;
 Date callDayTime;
 String callDuration;
 String dir ;
  String fileName;

boolean wasRinging=false, wasoffHook=false, wasidle=false;


private final BroadcastReceiver receiver = new BroadcastReceiver() {
     @Override
        public void onReceive(final Context context, Intent intent) {

            if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                // This code will execute when the phone has an incoming call
                 Log.d("ring ", "Detected");
                 wasRinging = true;
                // get the phone number 
           //     String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
           //     Toast.makeText(context, "Call from:" +incomingNumber, Toast.LENGTH_LONG).show();

            } else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                    TelephonyManager.EXTRA_STATE_OFFHOOK)){
                wasoffHook=true;
         //       Toast.makeText(context, "hang up", Toast.LENGTH_LONG).show();

            }
            else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                            TelephonyManager.EXTRA_STATE_IDLE)) {
                // This code will execute when the call is disconnected
                wasidle=true;

           //   Toast.makeText(context, "IDLE STATE", Toast.LENGTH_LONG).show();


                     if((wasRinging && wasoffHook && wasidle)){
                      // this is received call event


                     startActivity(new Intent(getApplicationContext(), Incoming.class));


                       } else if(wasRinging && wasidle){

                          // this is missed call event

                               startActivity(new Intent(getApplicationContext(), MissCall.class));


                       }

                         wasidle = false;
                         wasoffHook = false;
                         wasRinging=false;
                   }                     
            }
        }



@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
 @Override
   public void onCreate() {


     IntentFilter filter = new IntentFilter();
     filter.addAction(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED);
     registerReceiver(receiver, filter);
    }

    @Override
     public void onDestroy() {

         unregisterReceiver(receiver);
     }

}

我使用此代码获取上次通话的详细信息,您可以按照自己的方式进行修改