Android use Broadcast Receivers to send value

2019-08-21 04:02发布

问题:

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.

回答1:

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 the app's Java package name syntax and uniquely identify the broadcast event."



回答2:

An AccessibilityService that has been launched properly and your Activity are going to exist in separate processes. This is by definition. Assuming that you have launched your AccessibilityService properly, the following is what you want.

public class USSDService extends AccessibilityService {

    public void onAccessibilityEvent(AccessibilityEvent event) {

        Intent intent = new Intent(getPackageName() + "ACTION_ID");
        intent.putExtra("extra_id", "value");

        sendBroadcast(intent);
    }
}

You would then register for this broadcast in your Activity like so:

public class MainActivity extends AppCompatActivity {

    protected void onResume(){
        super.onResume();

        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.d("Broadcast", intent.getExtras().getString("extra_id"));
            }
        }, new IntentFilter(getPackageName() + "ACTION_ID"));
    }
}

Important part here is A: your intent filter is tied to your package name, and that "ACTION_ID" is always the same string. Ultimately this is probably best accomplished by a public static final string in either your Activity class or your service class... or in some other class abstracting away this layer of communication.