Error using AlarmManager and BroadcastReceiver

2

The goal is that after x time, in this case 10s, I start to put the call from the number x in vibration mode. When I do not call the initProcess () method, everything works OK. But when I call it, this error appears in the Logcat:

02-22 21:05:49.982 5787-5787/com.example.andry.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.andry.myapplication, PID: 5787
                                                                           java.lang.RuntimeException: Unable to start receiver com.example.andry.myapplication.Call_Reciver: java.lang.NullPointerException
                                                                               at android.app.ActivityThread.handleReceiver(ActivityThread.java:2580)
                                                                               at android.app.ActivityThread.access$1700(ActivityThread.java:151)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                               at android.os.Looper.loop(Looper.java:193)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5299)
                                                                               at java.lang.reflect.Method.invokeNative(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:515)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:822)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
                                                                               at dalvik.system.NativeStart.main(Native Method)
                                                                            Caused by: java.lang.NullPointerException
                                                                               at com.example.andry.myapplication.Call_Reciver.onReceive(Call_Reciver.java:19)
                                                                               at android.app.ActivityThread.handleReceiver(ActivityThread.java:2573)
                                                                               at android.app.ActivityThread.access$1700(ActivityThread.java:151) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                               at android.os.Looper.loop(Looper.java:193) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5299) 
                                                                               at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                               at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:822) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638) 
                                                                               at dalvik.system.NativeStart.main(Native Method) 

This is the part where I call the initProcess () method:

  boton.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            iniciarProceso();
         }
     });

StartProcess Method ():

public void iniciarProceso(){
int i=10;
Intent intent= new Intent(this,Call_Reciver.class);
PendingIntent pendingIntent= PendingIntent.getBroadcast(getApplicationContext(),1,intent,0);
AlarmManager alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+(i*1000),pendingIntent);
Toast.makeText(this,"La funcion inicia dentro de 10s",Toast.LENGTH_LONG).show();

}

And finally this is the method of the Call_Reciver class:

@Override
public void onReceive(Context context, Intent intent) {
      AudioManager am=(AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)){
        String incomingnumber=intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        if (incomingnumber.equals("unNumeroCualquiera")){
            am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
        }

    }
 }

This is my Manifest: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.andry.myapplication"> <uses-permission android:name="android.permission.VIBRATE"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".Activity_llamadas"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".Call_Reciver"> <intent-filter> <action android:name="android.intent.action.PHONE_STATE"> </action> </intent-filter> </receiver> </application> </manifest>

Line 19 of the Call_Reciver class is this: if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING))

    
asked by Andry_UCI 23.02.2018 в 03:14
source

2 answers

-1

The problem shown in the LogCat,

  

java.lang.RuntimeException: Unable to start receiver   com.example.andry.myapplication.Call_Reciver:   java.lang.NullPointerException Caused by:   java.lang.NullPointerException at   com.example.andry.myapplication.Call_Reciver.onReceive (Call_Reciver.java:19)

It's because the Intent that you get in the onReceive() method does not have the value you're trying to get (key: TelephonyManager.EXTRA_STATE ):

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

         ...      
         intent.getStringExtra(TelephonyManager.EXTRA_STATE)
         ...

         }

and this is because you are not sending this value in intent that you create in iniciarProceso() , you must add the value that will be received in class Call_Reciver :

public void iniciarProceso(){
int i=10;
Intent intent= new Intent(this,Call_Reciver.class);

/*Agregar valor en intent que será recibido en Call_Reciver.class*/
intent.putExtra(TelephonyManager.EXTRA_STATE, "AQUI AGREGA EL VALOR");

PendingIntent pendingIntent= PendingIntent.getBroadcast(getApplicationContext(),1,intent,0);
AlarmManager alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+(i*1000),pendingIntent);
Toast.makeText(this,"La funcion inicia dentro de 10s",Toast.LENGTH_LONG).show();
}
    
answered by 23.02.2018 / 17:04
source
1

If you want it to vibrate you have to put it in the manifest:

<uses-permission android:name="android.permission.VIBRATE"/>
    
answered by 23.02.2018 в 04:33