不工作 每次 PhoneStateListener Android [英] Not work Every time PhoneStateListener Android

查看:56
本文介绍了不工作 每次 PhoneStateListener Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发完整的来电显示应用程序.在来电/未接来电时的动态屏幕通话中.现在这是一次工作.当我接到电话时.比在它不工作或此时未呼叫任何动态屏幕之后.

I have Developing Application of Full Caller Id. In that dynamic screen call at the time of Incoming call / After Missed Call. Now this is work one time. When i received call. Than after it is not work or Not call any dynamic screen at the time Incoming call/ Missed call.

我对我的问题很困惑.

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;

    }

推荐答案

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

this is because when any call comes your app goes into background, while for the first time when you receive call your MyPhoneStateListener listens for the call and it ends.. now do one thing.. Run your code in service it will run in background and it will call your dynamic screen in your main activity start service for this i have used toggle button(to enable or disable 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);
     }

}

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

i used this code to get details of last call you can modify it in your way

这篇关于不工作 每次 PhoneStateListener Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆