0

MakeMissedCallActivity.java:

package com.android.main;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.os.Bundle;
import android.util.Log;

public class MakeMissedCallActivity extends Activity {
    private Button button;
    private static boolean mCallMadeFromApp = false;
    private String LOG_TAG = "MakeMissedCall App";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Listen for call state changes before making the call through button
        CallStateListener callStateListener = new CallStateListener();
        TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

        // add button and add dial functionality 
        button = (Button) findViewById(R.id.buttonCall);
        button.setOnClickListener(new OnClickListener() {
            //@Override
            public void onClick(View arg0) {
                mCallMadeFromApp = true;
                Log.i(LOG_TAG, "Button clicked");
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:+918028563681"));
                startActivity(callIntent);
            }
        });
    }

    public boolean getmCallMadeFromApp() {
        Log.i(LOG_TAG, "mCallMadeFromApp=" +mCallMadeFromApp);
        return mCallMadeFromApp;
    }

    public void setmCallMadeFromApp(boolean mNewValue) {
        mCallMadeFromApp = mNewValue;
    }
}

CallStateListener.java:

package com.android.main;

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

//monitor phone call activities
public class CallStateListener extends PhoneStateListener {
    private String LOG_TAG = "MakeMissedCall App";
    MakeMissedCallActivity makeMissedCallActivity;

    CallStateListener() {
        MakeMissedCallActivity makeMissedCallActivity = new MakeMissedCallActivity();
    }

    @Override
    public void onCallStateChanged(int call_state, String incomingNumber) {
        switch(call_state) {
            case TelephonyManager.CALL_STATE_RINGING:
                Log.i(LOG_TAG, "CALL_STATE_RINGING");
                break;

            case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.i(LOG_TAG, "CALL_STATE_OFFHOOK. mCallMadeFromApp=" + makeMissedCallActivity.getmCallMadeFromApp());
                break;

            case TelephonyManager.CALL_STATE_IDLE:
                if (makeMissedCallActivity.getmCallMadeFromApp() == true) {
                    makeMissedCallActivity.setmCallMadeFromApp(false);
                }
                Log.i(LOG_TAG, "CALL_STATE_IDLE");
                break;
        }
    }
}

当我运行应用程序时,如果(makeMissedCallActivity.getmCallMadeFromApp() == true)CallStateListener.java.

知道可能是什么问题吗?

4

1 回答 1

4

您的问题似乎出在这段代码中;

MakeMissedCallActivity makeMissedCallActivity;

CallStateListener() {
    MakeMissedCallActivity makeMissedCallActivity = new MakeMissedCallActivity();
}

您正在创建一个私有变量,但在您的构造函数中,您正在创建一个具有相同名称的新局部变量并将新活动分配给该变量。当您退出构造函数时,私有变量仍将是null.

你的意思可能只是简单的;

MakeMissedCallActivity makeMissedCallActivity;

CallStateListener() {
    makeMissedCallActivity = new MakeMissedCallActivity();
}
于 2012-04-22T05:43:34.787 回答