java - Android use Broadcast Receivers to send value -
i have accessibilityservice class:
public class ussdservice extends accessibilityservice { public static string tag = "ussdservice"; public string responsee=""; @override public void onaccessibilityevent(accessibilityevent event) { log.d(tag, "onaccessibilityevent"); string text = event.gettext().tostring(); if (event.getclassname().equals("android.app.alertdialog")) { performglobalaction(global_action_back); log.d(tag, text); intent intent = new intent("message"); intent.putextra("value", text); toast.maketext (this,text,toast.length_long).show(); this.sendbroadcast(intent);// write broad cast receiver , call sendbroadcast() here, if want parse message balance, date } } @override public void oninterrupt() { } @override protected void onserviceconnected() { super.onserviceconnected(); log.d(tag, "onserviceconnected"); accessibilityserviceinfo info = new accessibilityserviceinfo(); info.flags = accessibilityserviceinfo.default; info.packagenames = new string[]{"com.android.phone"}; info.eventtypes = accessibilityevent.type_window_state_changed; info.feedbacktype = accessibilityserviceinfo.feedback_generic; setserviceinfo(info); }}
as can see in method onaccessibilityevent
, send intent using method sendbroadcast
.
in mainactivity use broadcastreceiver receive value this:
public class mainactivity extends appcompatactivity { private broadcastreceiver breceiver = new broadcastreceiver(){ @override public void onreceive(context context, intent intent) { //put here whaterver want activity intent received log.i("onreceive",intent.getstringextra("value") ); } }; protected void onresume(){ super.onresume(); log.i("onresume", "22222"); localbroadcastmanager.getinstance(this).registerreceiver(breceiver, new intentfilter("message")); } protected void onpause (){ super.onpause(); log.i("onpause", "11111"); localbroadcastmanager.getinstance(this).unregisterreceiver(breceiver); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main);}}
my app works fine when call ussd method onaccessibilityevent
work , value show in toast, method onreceive
did not work , don't know problem is. please me.
intent intent = new intent(); intent.setaction("com.example.broadcast.my_notification"); intent.putextra("message"); localbroadcastmanager.getinstance(this).sendbroadcast(intent);
from android: "the intent's action string must provide app's java package name syntax , uniquely identify broadcast event."
Comments
Post a Comment