I have this 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 a broad cast receiver and call sendbroadcast() from here, if you want to parse the message for 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 you can see in the method onAccessibilityEvent
, I send intent by using the method sendBroadcast
.
In MainActivity I use BroadcastReceiver to receive the value like this:
public class MainActivity extends AppCompatActivity {
private BroadcastReceiver bReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
//put here whaterver you want your activity to do with the 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 I call ussd the method onAccessibilityEvent
is work and the value show in Toast, but the method onReceive
did not work and I don't know where the problem is. Please help me.