0
package kr.phpdev.call;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.PhoneNumberUtils;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;


import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class PhoneStateReceiver extends BroadcastReceiver {
    static String mLastState;
    static final String TAG = "Call Manager";
    final OkHttpClient client = new OkHttpClient();


@Override
public void onReceive(Context context, Intent intent) {

    CallReceivedChk(context, intent);


}

private void CallReceivedChk(Context context, Intent intent) {


    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    telephony.listen(new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            String mState = String.valueOf(state);
            if (mState.equals(mLastState)) { // 두번 호출되는 문제 해결 목적
                return;
            } else {
                mLastState = mState;
            }

            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    Log.d(TAG, "CALL_IDLE");
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    Log.d(TAG, "CALL_OFFHOOK");
                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    Log.d(TAG, "CALL_RINGING >>> " + PhoneNumberUtils.formatNumber(incomingNumber));
                    RequestBody formBody = new FormBody.Builder()
                            .add("pn", PhoneNumberUtils.formatNumber(incomingNumber))
                            .build();
                    final Request request = new Request.Builder()
                            .url("http://phpdev.kr/cm/logsend.php")
                            .post(formBody)
                            .build();

                    AsyncTask<String, String, String> asyncTask = new AsyncTask<String, String, String>() {


                        @Override
                        protected void onPreExecute() {

                            super.onPreExecute();

                        }

                        @Override
                        protected String doInBackground(String... params) {
                            try {
                                Response response = client.newCall(request).execute();
                                if (!response.isSuccessful()) {
                                    return null;
                                }
                                return response.body().string();


                            } catch (Exception e) {
                                e.printStackTrace();
                                return null;
                            }
                        }


                        @Override
                        protected void onPostExecute(String s) {
                            super.onPostExecute(s);
                            if (s != "FAILED") {


                                Log.d(TAG, s);
                                Toast.makeText(getApplicationContext(), "토스트메시지입니다.", Toast.LENGTH_SHORT).show();

                            }
                        }

                    };

                    asyncTask.execute();
                    break;
            }
        }
    }, PhoneStateListener.LISTEN_CALL_STATE);
   }

   }

how can i get toast message?

Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56

3 Answers3

0

Use Toast.makeText(context, "토스트메시지입니다.",Toast.LENGTH_SHORT).show();`

@Override
   protected void onPostExecute(String s) {
      super.onPostExecute(s);
      if (s != "FAILED") {


      Log.d(TAG, s);
      Toast.makeText(context, "토스트메시지입니다.", Toast.LENGTH_SHORT).show();

         }
    }
Mukesh M
  • 2,242
  • 6
  • 28
  • 41
0

You need to do that on onPostExecute since it is running on the UI thread. And also in Java string comparison is like s.equals("Failed"):

 @Override
                    protected void onPostExecute(String s) {
                        super.onPostExecute(s);
                        if (!s.equals("FAILED")) {


                            Log.d(TAG, s);
                            Toast.makeText(context, "토스트메시지입니다.", Toast.LENGTH_SHORT).show();

                        }
                    }
Pooya
  • 6,083
  • 3
  • 23
  • 43
0

Error:(103, 68) error: local variable context is accessed from within inner class; needs to be declared final. You just need using "context" instead of "getApplicationContext()".

ppp wang
  • 32
  • 2