Android BroadcastReceiver does not work phone calls -
i looking examples.
my class like:
<pre><code> package com.example.pjimnez.samsung_auto_reply; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.content.intentfilter; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.telephony.phonestatelistener; import android.telephony.smsmanager; import android.telephony.telephonymanager; import android.widget.button; import android.widget.textview; import android.view.view; import android.widget.toast; import android.content.intent; import android.content.intentfilter; public class mainactivity extends appcompatactivity { //intentfilter screenstatefilter; button btnstart; textview txthome; //phonestate ophonestate; intentfilter filter1; private final broadcastreceiver myphonestate = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { string state = intent.getstringextra(telephonymanager.extra_state); if (state == null) { //outgoing call string number = intent.getstringextra(intent.extra_phone_number); toast.maketext(context, number, toast.length_long).show(); } else if (state.equals(telephonymanager.extra_state_offhook)) { //log.e("tag", "extra_state_offhook"); toast.maketext(context, "extra_state_offhook", toast.length_long).show(); } else if (state.equals(telephonymanager.extra_state_idle)) { //log.e("tag", "extra_state_idle"); toast.maketext(context, "extra_state_idle", toast.length_long).show(); } else if (state.equals(telephonymanager.extra_state_ringing)) { //incoming call string number = intent.getstringextra(telephonymanager.extra_incoming_number); //log.e("tag", "incoming number : " + number); toast.maketext(context, number, toast.length_long).show(); } else toast.maketext(context, "none", toast.length_long).show(); } }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); filter1 = new intentfilter(telephonymanager.action_phone_state_changed); registerreceiver(this.myphonestate, filter1); btnstart = (button) findviewbyid(r.id.btnstart);// instancia del objeto boton intro //no funciona en modo escucha btnstart.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { txthome = (textview) findviewbyid(r.id.txthome); txthome.settext("dio click en el boton!" );//cambio el contenido del textview //envia el mensahe sms /* smsmanager smsmanager = smsmanager.getdefault(); smsmanager.sendtextmessage("5524234613", null, "mensaje de prueba", null, null); */ } }); } } enter code here </code></pre>
my manifest has permissions to:
android.permission.read_phone_state
android.permission.receive_sms
android.permission.send_sms
the problem app never show message on phone change state
i not know whats wrong broadcastreceiver
could helpme
try this:
1- add class phonestatebroadcastreceiver extends broadcastreceiver , overwrite onreceive()
2- @ runtime, request phone_state permission upfront
activitycompat.requestpermissions(mymainactivity, new string[]{manifest.permission.read_phone_state}, read_phone_state_code);
and give via system dialog
3- make phone call
you'll see intent caught in onreceive():
intent: intent { act=android.intent.action.read_phone_state flg=0x10 cmp=com.myapp.network.phonestatebroadcastreceiver (has extras) } action: android.intent.action.phone_state
hope helps
Comments
Post a Comment