吐司无法画画

时间:2013-05-13 05:40:58

标签: android android-broadcast

我正在创建一个广播接收器应用程序。因为我得到了以下错误

The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (MyPhoneCall, String, int)

Java代码:

package com.example.myphonecall;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class MyPhoneCall extends BroadcastReceiver {

    @
    Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            String state = extras.getString(TelephonyManager.EXTRA_STATE);
            Log.w("MY_DEBUG_TAG", state);
            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                String phoneNumber = extras
                    .getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                //Log.w("MY_DEBUG_TAG", phoneNumber);
                Toast.makeText(this, "Getting Call from " + phoneNumber, Toast.LENGTH_LONG).show();
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

使用

Toast.makeText(context, "Getting Call from "+phoneNumber, Toast.LENGTH_LONG).show();

答案 1 :(得分:0)

而不是传递this,传递context,它将解决您的问题。