我有这个AccessibilityService类:
/img/boats/avatars/C:\xampp\tmp\phpFA50.tmp
正如您在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);
}}
方法中所看到的,我使用方法onAccessibilityEvent
发送意图。
在MainActivity中,我使用BroadcastReceiver接收如下值:
sendBroadcast
当我调用ussd时,我的应用程序运行正常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);}}
方法正常工作且值显示在Toast中,但方法onAccessibilityEvent
不起作用,我不知道问题出在哪里。请帮帮我。
答案 0 :(得分:0)
Intent intent = new Intent();
intent.setAction("com.example.broadcast.MY_NOTIFICATION");
intent.putExtra("message");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
来自Android: “intent的操作字符串必须提供应用程序的Java包名称语法,并唯一标识广播事件。”
答案 1 :(得分:0)
已正确启动的AccessibilityService,您的Activity将在不同的进程中存在。这是定义。假设您已正确启动AccessibilityService,以下是您想要的。
public class USSDService extends AccessibilityService {
public void onAccessibilityEvent(AccessibilityEvent event) {
Intent intent = new Intent(getPackageName() + "ACTION_ID");
intent.putExtra("extra_id", "value");
sendBroadcast(intent);
}
}
然后您将在您的活动中注册此广播,如下所示:
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"));
}
}
这里重要的部分是A:您的意图过滤器与您的包名称相关联,并且" ACTION_ID"总是相同的字符串。最终,这可能最好是通过Activity类或服务类中的公共静态最终字符串...或者在其他类中抽象出这层通信。