Error using TimerTask to disable a BroadcastReceiver

0

What I'm trying to do is turn off a BroadcastReceiver when X time passes. This I already achieve, and it works. I tried it using a switch and it worked, when it was in on . It was activated and when it was in off no. Initially I have it disabled, my goal is that when it happens for a while X is activated. I also checked that I entered the run() method using the Log.d() and entered. The problem is when I call the method, also try to copy the method activarFuncion() for the class Timer, but it tells me that cannotresolve getPackageManager() , in order that it does not work. The class Call_Reciver is the one that inherits from BroadcastReceiver . Any ideas???

Here is the COMPLETE class (only the import ones are missing):

public class Activity_llamadas extends Activity {
Button boton;
EditText editText;
TextView textView;
TextView textView2;
TextView textView3;
Switch aSwitch;
int h=0,m=0;
Vibrator vivatror;
long[] pattern = {0, 500, 100, 1000};

@Override
 public void onCreate(Bundle savedInstanceState) {

    ComponentName reciver= new ComponentName(this,Call_Reciver.class);
    PackageManager pm=getPackageManager();
    pm.setComponentEnabledSetting(reciver,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_llamadas);
    aSwitch= (Switch)findViewById(R.id.switchAcvivar);


    SimpleDateFormat date=  new SimpleDateFormat("mm"); 
    String mhora= date.format(System.currentTimeMillis()); 
    textView3=(TextView)findViewById(R.id.textHoraAct);
    textView3.setText(mhora);
    vivatror = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    textView=(TextView)findViewById(R.id.textViewHora);
    textView2=(TextView)findViewById(R.id.textViewhora2);
     boton=(Button)findViewById(R.id.buttonHora);
     boton.setOnClickListener(new View.OnClickListener() {
 @Override
         public void onClick(View v) {
             TactivarBroucast();
        }
     });
    editText=(EditText)findViewById(R.id.getHora);
    editText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mostrarHora(textView);
        }
    });
    editText=(EditText)findViewById(R.id.getHora2);
    editText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mostrarHora(textView2);

        }
    });

 }
   public void setTxHora(int h,int m, TextView t){
   t.setText(String.valueOf(h)+": "+String.valueOf(m));
}

private void mostrarHora(final TextView t) {
    final TimePickerDialog timePickerDialog = new TimePickerDialog(this,
            new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker view, int hourOfDay,int minute) {
                 int h= view.getCurrentHour();
                 int  m = view.getCurrentMinute();
                   setTxHora(h,m,t);
                }
            }, h, m, false);
    timePickerDialog.setTitle("Selecciona la hora");
    timePickerDialog.show();
}
   public void stardNotificacion(){
    NotificationCompat.Builder mBuilder= (NotificationCompat.Builder) new NotificationCompat.Builder(this);
    mBuilder.setSmallIcon(R.mipmap.ic_launcher);
    mBuilder.setContentTitle("SiyOp");
    mBuilder.setContentText("La aplicion ha comensado a funcionar a las "+getSystemHora());
    NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0,mBuilder.build());

}
public void iniciarBrocast(){
int i=20;
Intent intent= new Intent(this,Call_Reciver.class);
 intent.putExtra(TelephonyManager.EXTRA_STATE ,TelephonyManager.EXTRA_STATE ); 
PendingIntent pendingIntent= PendingIntent.getBroadcast(getApplicationContext(),15635435,intent,0);  

AlarmManager alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);

alarmManager.setExact(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+(i*1000),pendingIntent);
}

public  void activarFuncion(){
    ComponentName reciver= new ComponentName(this,Call_Reciver.class);
    PackageManager pm=getPackageManager();
    pm.setComponentEnabledSetting(reciver,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
}
 public void getSwitchState(View v){
    if(aSwitch.isChecked()){
        activarFuncion();
    }
    else {
        ComponentName reciver= new ComponentName(this,Call_Reciver.class);
        PackageManager pm=getPackageManager();            pm.setComponentEnabledSetting(reciver,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);
    }
}
public void TactivarBroucast(){
    Date date= new Date(System.currentTimeMillis());
    Calendar c=Calendar.getInstance();
    c.set(Calendar.MILLISECOND,5000);
    date=c.getTime();
    Timer time= new Timer();
    time.schedule(new Temporisador(),date);
}    
}

Method in the class

MainActivity:

    public void activarFuncion(){
        ComponentName reciver= new ComponentName(this,Call_Reciver.class);
        PackageManager pm=getPackageManager();
        pm.setComponentEnabledSetting(reciver,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
    }    

Timer Class:

   public class Temporizador extends TimerTask {
    Activity_llamadas activity_llamadas;
    @Override
     public void run(){
      activity_llamadas.activarFuncion();//<----------ESTA ES LA LINEA 15
      }
}

And this is Logcat:

02-26 18:57:55.005 26935-26975/com.example.andry.myapplication E/AndroidRuntime: FATAL EXCEPTION: Timer-0
                                                                             Process: com.example.andry.myapplication, PID: 26935
                                                                             java.lang.NullPointerException
                                                                                 at
     

com.example.andry.myapplication.Temporisador.run (Temporisador.java:15)                                                                                        at java.util.Timer $ TimerImpl.run (Timer.java:284)

NEW LOGCAT:

  

02-26 21: 05: 22.020 27421-27605 / com.example.andry.myapplication   E / AndroidRuntime: FATAL EXCEPTION: Timer-0                                                                                    Process: com.example.andry.myapplication, PID: 27421                                                                                    java.lang.RuntimeException: Can not create handler inside thread that   has not called Looper.prepare ()                                                                                        at android.os.Handler. (Handler.java:208)                                                                                        at android.os.Handler. (Handler.java:122)                                                                                        at android.app.Activity. (Activity.java:765)                                                                                        at   com.example.andry.myapplication.Activity_called. (Activity_llamadas.java:28)                                                                                        at   com.example.andry.myapplication.Temporisador.run (Temporisador.java:15)                                                                                        at java.util.Timer $ TimerImpl.run (Timer.java:284)

    
asked by Andry_UCI 27.02.2018 в 02:14
source

0 answers