Android TelephonyManager和PhoneStateListener

时间:2012-05-19 09:08:45

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

我希望在收到来电并且处于振铃模式时在顶部弹出一个新视图。但我无法执行此操作。 我发布了我的代码,但也考虑了评论中的代码。

package com.android.CallIntent;

import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class Listener extends PhoneStateListener {
    Context Context;
    public Listener(Context context)
    {
        this.Context = context;
    }

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        // TODO Auto-generated method stub
        super.onCallStateChanged(state, incomingNumber);

        switch(state)
        {
            case TelephonyManager.CALL_STATE_OFFHOOK:

                break;
            case TelephonyManager.CALL_STATE_RINGING:

                Log.d("message","Ringing");
                Intent intent = new Intent(Context, CallOptions.class);
                // I want to start my new Activity From here please Help me.

                //I am unable to access StartActivity
                break;

        }
    }

}

// My BroadcastReceiver class is here
public class Receiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        Listener phoneStateListener= new Listener();
        manager.listen(phoneStateListener, Listener.LISTEN_CALL_STATE);


    }

请有人帮帮我,请。

先谢谢。

1 个答案:

答案 0 :(得分:0)

看这里......它必须帮助你......

   case TelephonyManager.CALL_STATE_RINGING:
            Toast.makeText( context, "incoming call", Toast.LENGTH_LONG).show();
            IntentService = new Intent(context, PlayService.class).setAction("incoming_call");
            IntentService.putExtra("phone_number",intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER) );
            if (SmsReceiver.bool)
            context.startService(IntentService);
            break;