0

I need help with that, i found one more same question but didnt work for me.

Thats my code:

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.provider.CallLog;
import android.provider.ContactsContract;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.ArrayAdapter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CallDetector extends Service {

    static String[] test = new String[15];
    boolean isstart = false;
    AudioManager audio_mng;

        private static final String DEBUG_TAG = "InviteActivity";

        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        CallDetector calldetector= new CallDetector(this);

        Log.v(DEBUG_TAG, "Service start");

        int i = 0;
        File file = new File("pathtofile/myfile.txt");

        try {
            Scanner scaner = new Scanner(new FileReader(file));
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;

            do{
                line = scaner.nextLine();
                test[i] = line;

                i++;

                br.close();
            }while(scaner.hasNext());
        }
        catch (IOException e) {
            //You'll need to add proper error handling here
        }

        calldetector.start();
        return START_STICKY;
    }

    public void onDestroy(){
        CallDetector calldetector = new CallDetector(this);

        calldetector.stop();
        super.onDestroy();
    }

    public void onCreate(){
        super.onCreate();
        audio_mng = (AudioManager) getBaseContext().getSystemService(ctx.AUDIO_SERVICE);
    }

        private class PhoneCallListener extends PhoneStateListener {

            private boolean isPhoneCalling = false;

            @Override
            public void onCallStateChanged(int state, String incomingNumber) {

                if (TelephonyManager.CALL_STATE_RINGING == state) {

                    for(int i =0; i <15; i++) {
                        if (incomingNumber.equals(test[i])) {
                            Log.v(DEBUG_TAG, "OK");
                            onchangetonormal();
                        }
                    }

                }
            }
        }

        private Context ctx;
        private TelephonyManager tm;
        private PhoneCallListener callStateListener;

        public void onchangetonormal(){
            audio_mng.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        }

        public CallDetector(Context ctx) {
            this.ctx = ctx;

            callStateListener = new PhoneCallListener();
        }

        /**
         * Start calls detection.
         */
        public void start() {
            Log.v(DEBUG_TAG, "Start listening");
            tm = (TelephonyManager)     ctx.getSystemService(Context.TELEPHONY_SERVICE);
            tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        }

        /**
         * Stop calls detection.
         */
        public void stop() {
                tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
        }

    }

when i get a call with service running i get the error but the service keep running.

In my first code was this:

if (TelephonyManager.CALL_STATE_RINGING == state) {

                    for(int i =0; i <15; i++) {
                        if (incomingNumber.equals(test[i])) {
                            Log.v(DEBUG_TAG, "OK");
AudioManager audio_mng;
audio_mng = (AudioManager) getBaseContext().getSystemService(ctx.AUDIO_SERVICE);
audio_mng.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                        }
                    }

                }

All was in the if for when the phone get a call ! I found a solution as i said and i try to put them in onCreat and then call it in if but nothing.

Edit:

FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.test.testapp.CallDetector.onchangetonormal(CallDetector.java:156)
            at com.test.testapp.CallDetector$PhoneCallListener.onCallStateChanged(CallDetector.java:107)
            at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:393)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4867)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
            at dalvik.system.NativeStart.main(Native Method)

Thanks!

And sorry for my bad english !

dpapatsarouchas
  • 151
  • 1
  • 2
  • 15

2 Answers2

0

As in log NullPointerException exception occur in onchangetonormal method means audio_mng is null.

Put null check before using audio_mng object in onchangetonormal method:

if(audio_mng==null){
  audio_mng=(AudioManager)CallDetector.this.getSystemService(
                                                      Context.AUDIO_SERVICE);
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • nothing... this line is in onCreate so i put it here. Also i try this if(audio_mng == null) { Log.v(DEBUG_TAG, "audiomanager" + audio_mng); } in onCreat so if it is null it will show message. But it is not show anything so it isnt null. – dpapatsarouchas Feb 02 '15 at 23:37
  • @NinjTsax: what happening when you are using suggest code in `onchangetonormal` method? – ρяσѕρєя K Feb 02 '15 at 23:38
  • I didn't understand exactly what you asking me to do. But I think about permissions. I already have permissionfor phophone state but its possible service need separate permissions? – dpapatsarouchas Feb 03 '15 at 00:34
0

I have an idea today.... To declare AudioManager audio_mng as static.... That works !

If you have other explanation or another answer or why that happened please answer.

Thank you for your interest

dpapatsarouchas
  • 151
  • 1
  • 2
  • 15