如何更换我自己的“手机铃声”屏幕而不是Android的默认屏幕?

时间:2016-05-30 05:34:02

标签: android android-service phone-state-listener

我做了一些谷歌搜索,但无法找到我的问题的明确答案。

我有一项活动,需要在手机开始响铃时接听(接听电话) - 而不是机器人默认屏幕。

enter image description here

我知道我必须在Manifest.xml中设置一个接收器:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<receiver android:name=".ServiceReceiver">
    <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
</receiver>

并创建一个接收器类:

public class MyPhoneStateListener extends PhoneStateListener {

    public static Boolean phoneRinging = false;

    public void onCallStateChanged(int state, String incomingNumber) {

        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                Log.e("DEBUG", "IDLE");
                phoneRinging = false;
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.e("DEBUG", "OFFHOOK");
                phoneRinging = false;
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                //Intent intent=new Intent(getClass().)
                Log.e("DEBUG", "RINGING");
                phoneRinging = true;

                break;
        }
    }

}

class ServiceReceiver extends BroadcastReceiver {
    TelephonyManager telephony;
    Intent in;
    public void onReceive(Context context, Intent intent) {
        MyPhoneStateListener phoneListener = new MyPhoneStateListener();
        telephony = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
   
    }
}

现在我可以正确地在我的logcat中获取“RINGING”但是如何开始自定义活动或者首选方法是什么?

1 个答案:

答案 0 :(得分:0)

我找到了答案,我将与你分享。

首先与onCallStateChanged部分无关。 所以我必须在BroadcastReceiver类中的onReceive中调用目标活动:

@Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            String state = extras.getString(TelephonyManager.EXTRA_STATE);
            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                Intent intent1=new Intent(context, Ringing.class);
                intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                context.startActivity(intent1);
              
            }
        }
    }