Lo que intento hacer que al pasar X tiempo se desactive un BroadcastReceiver
. Esto ya lo logre, y funciona. Lo probé usando un Switch y funciono, cuando estaba en on
.Se activaba y cuando estaba en off
no. Inicialmente lo tengo desactivado, mi objetivo es que cuando pase un tiempo X se active. También comprobé que entrara al método run()
usando el Log.d()
y entro. El problema esta cuando llamo al método, también intente copiar el método activarFuncion()
para la clase Temporizador, pero me dice que cannotresolve getPackageManager()
, en fin QUE NO FUNCIONA.La clase Call_Reciver
es la que hereda de BroadcastReceiver
. Alguna idea???
Aqui esta la clase COMPLETA (solo le faltan los import):
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);
}
}
Método en la clase
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);
}
Clase Temporizador:
public class Temporizador extends TimerTask {
Activity_llamadas activity_llamadas;
@Override
public void run(){
activity_llamadas.activarFuncion();//<----------ESTA ES LA LINEA 15
}
}
Y este es 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)
NUEVO 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't 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_llamadas.(Activity_llamadas.java:28) at com.example.andry.myapplication.Temporisador.run(Temporisador.java:15) at java.util.Timer$TimerImpl.run(Timer.java:284)