使用的BroadcastReceiver与PhoneStateListener功能功能、BroadcastReceiver、PhoneStateListener

2023-09-06 04:50:21 作者:凉了时光空了旧城

我试图做一个MissCall应用程序,它收到一个未接来电,当一个消息自动发送的我已经完成了我的应用程序,它能正常工作 下面是完整的场景! 问题: 该应用程序工作正常的但是当我重新启动设备中的应用程序没有工作!。它只有当我开始我的应用程序ATLEAST一次后,它工作得很好,直到它被关闭工作。 这是我的code:

I am trying to make a MissCall App which sends a message automatically when a miss call is received.I had completed my app and it worked fine ! Here is the complete scenario : Problem : The app was working fine but when i restarted the device the app didn't work ! . It only worked when i started my App atleast once after that it worked fine till it is switched off . Here is my code :

package com.example.misscallapp;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class Pref_Main extends PreferenceActivity {
    int checkIt = 0;
    TelephonyManager tm;
    CallStateListener callStateListener = new CallStateListener();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs);
        tm = (TelephonyManager) getBaseContext().getSystemService(
                Context.TELEPHONY_SERVICE);
        tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    }

    private class CallStateListener extends PhoneStateListener {

        @Override
        public void onCallStateChanged(int state, String incomingNumber) { 
           // Is called whenever there is a change in call state
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                // called when someone is ringing to this phone

                Toast.makeText(getBaseContext(), "Incoming: " + incomingNumber,
                        Toast.LENGTH_LONG).show();
                checkIt = 1;
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                checkIt = 0;
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                if (checkIt == 1) {
                    Intent i = new Intent(getBaseContext(),MyService.class);
                    i.putExtra("phno", incomingNumber);
                    startService(i); // service that sends the SMS
                }
                break;
            }
        }

    }



}

解决方法: 我发现的解决方案是使用的BroadcastReceiver 。所以我注册一个BroadcastReceiver,但它并没有给我的功能 PhoneStateListener 对于如我尝试使用下面的code,但由于没有工作的 BroadcastReceivers 当它收到相反的东西只能叫 PhoneStateListener 它调用的方法onCallStateChanged每当有呼叫状态的改变:

Solution : I found out that the solution to this is to use BroadcastReceiver . So i registered a BroadcastReceiver but it didn't give me the functionality of PhoneStateListener For e.g I tried using the following code but it didn't work since BroadcastReceivers are only called when it receives something in contrast to PhoneStateListener which calls the method onCallStateChanged whenever there is a change in call state :

package com.example.misscallapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class CallReceiverBroadcast extends BroadcastReceiver {
    int checkIt = 0;

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            String state = extras.getString(TelephonyManager.EXTRA_STATE);
            String incomingNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                Toast.makeText(context , "Incoming: " + incomingNumber,
                        Toast.LENGTH_LONG).show();
                checkIt = 1;
            }

            if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
                // Call received
                checkIt = 0;
            }

            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                if (checkIt == 1) {
                    Toast.makeText(context , "This is not shown ",
                            Toast.LENGTH_LONG).show();
                    Intent i = new Intent(context,MyService.class);
                    i.putExtra("phno", incomingNumber);
                    context.startService(i);
                }
            }

        }

    }
}

我也尝试过一种解决办法,但结果均为阴性,如:

I also tried a work around but it showed negative results like :

package com.example.misscallapp;

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 CallReceiverBroadcast extends BroadcastReceiver {
    int checkIt = 0;
    Context contextt;
    TelephonyManager tm;
    CallStateListener callStateListener = new CallStateListener();
    @Override
    public void onReceive(Context context, Intent intent) {
        contextt = context;
        tm = (TelephonyManager) context.getSystemService(
                Context.TELEPHONY_SERVICE);
        tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

    }

    private class CallStateListener extends PhoneStateListener {

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                // called when someone is ringing to this phone

                Toast.makeText(contextt, "Incoming: " + incomingNumber,
                        Toast.LENGTH_LONG).show();
                checkIt = 1;
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                checkIt = 0;
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                if (checkIt == 1) {
                 //startting the service
                    break;
                }
            }

        }

    }
}

以上code满足所有要求,但它发出的在arithematic发展一样,如果它是第一个未接来电发送1,如果是第十然后将其发送10封邮件! 我认真地需要在这方面的帮助, 先感谢您 。 修改1: 问题是,每次当的onReceive()方法被调用一个新的 TelphoneManager 实例时间创建并注册为一个监听器Phoone国家。 解决方法: 我做了 CallReceiverBroadcast 类的每个变量的静态!它的解决了这个问题!的程度,但仍是服务每一次两次打电话就意味着一些如何有注册为一个监听器类的2实例,但我不知道为什么。虽然我可以解决它通过一些条件,但它造成不必要的开销和任何人有一个更好的解决方案将是非常鸭preciated 。

The above code fulfils all the requirements but it sends the in arithematic progression like if it is the first miss call it sends 1 and if it is tenth then it sends 10 messages ! I seriously need help on this, Thank you in advance . Edit 1 : The problem is that every time when the onReceive() method is called a new TelphoneManager instance is created and registers as a listener to Phoone State . Solution : I made every variable of the CallReceiverBroadcast class static ! and it solved the problem !! to an extent but still the service is called twice every time it means that some how there is 2 instance of the class registered as a listener but i don't know why. Although i can work around it through some condition but it is causing unnecessary overhead and Anyone having a better solution will be highly Appreciated .

推荐答案

这是因为一旦你得到了未接来电,那么你是在 TelephonyManager.CALL_STATE_IDLE 舞台和消息将发送到相应的消息,但你的服务运行模式,这就是为什么它会发送10个或多个短信。

This is because of once you got the miss call then you are AT TelephonyManager.CALL_STATE_IDLE stage and message will send to appropriate message but your service is running mode that's Why it will send 10 or multiple sms.