に送信できません。ここで私はUSSDコードを呼び出していますが、次にAccessibilityServiceを拡張して対応する応答を読み取ろうとしています。このプロセスは正常に動作しています。 しかし、私の問題は、LocalBroadCast Managerを使用して対応するデータ(または値)をアクティビティ(MainActivity.java) BroadcastReceiverが呼び出されなくても転送しようとしているときです。Local BroadcastManagerから値を
LocalBroadcastManagerを使用してデータを転送する手助けがありますか?
下記のマイククラスを見つけてください。
MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText edt_ussd;
String callstring = "";
Button btn_call;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
edt_ussd = (EditText) findViewById(R.id.editText);
btn_call = (Button) findViewById(R.id.btn_call);
btn_call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startService(new Intent(MainActivity.this, UssdDemo.class));
call_Ussd(edt_ussd.getText().toString().trim());
}
});
LocalBroadcastManager.getInstance(MainActivity.this).registerReceiver(mMsgReceiver,
new IntentFilter("test_ussd.twinkletech.org.ussd_test.LBroadCst"));
}
public void call_Ussd(String ussd) {
String s = edt_ussd.getText().toString();//calledittext is editText on the
//screen from which can get the number
if ((s.startsWith("*")) && (s.endsWith("#"))) {
//if true then it is a USSD call----
callstring = s.substring(0, s.length() - 1);
callstring = callstring + Uri.encode("#");
Log.d("CALL TYPE---------->", "USSD CALL");
System.out.println("==== output : " + callstring);
} else {
callstring = s;
Log.d("CALL TYPE---------->", "Not a USSD CALL");
}
System.out.println("==== callstring : " + callstring);
Intent i = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + callstring));
startActivity(i);
}
@Override
protected void onResume() {
super.onResume();
System.out.println("--Coming inside onResume--");
LocalBroadcastManager.getInstance(MainActivity.this).registerReceiver(mMsgReceiver, new IntentFilter("test_ussd.twinkletech.org.ussd_test.LBroadCst"));
}
@Override
protected void onPause() {
super.onPause();
System.out.println("--Coming inside onPause--");
LocalBroadcastManager.getInstance(MainActivity.this).unregisterReceiver(mMsgReceiver);
}
private BroadcastReceiver mMsgReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("-->> Msg received from LOCAL broadcast receiver MainActivity >>>>>>>>>>");
System.out.println("-->> Get actiion : "+intent.getAction());
System.out.println("-->> Intent value : "+intent.getStringExtra("response_msg"));
}
} ;
@Override
protected void onDestroy() {
super.onDestroy();
// Unregister since the activity is about to be closed
LocalBroadcastManager.getInstance(MainActivity.this).unregisterReceiver(mMsgReceiver);
}
}
UssdDemo.java
public class UssdDemo extends AccessibilityService {
public static String TAG = "UssdDemo";
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
System.out.println("<<< onAccessbltyEvent");
String text = event.getText().toString();
System.out.println("<<< --- : " + text);
if (event.getClassName().equals("android.app.AlertDialog")) {
performGlobalAction(GLOBAL_ACTION_BACK);
System.out.println("<<< Return value : " + text);
Intent intent = new Intent();
intent.setAction("test_ussd.twinkletech.org.ussd_test.LBroadCst");
intent.putExtra("response_msg", text);
// write a broad cast receiver and call sendbroadcast() from here, if you want to parse the message for balance, date
LocalBroadcastManager.getInstance(UssdDemo.this).sendBroadcast(intent);
}
}
@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);
}}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="test_ussd.twinkletech.org.ussd_test.MainActivity"
tools:showIn="@layout/activity_main">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|top">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="Enter USSD code"
android:textSize="30sp"
android:layout_marginTop="10dp"
android:gravity="center"
android:textStyle="bold"
android:textColor="@color/colorPrimary"
android:shadowColor="#806A6A"
android:shadowDx="7"
android:shadowDy="7"
android:shadowRadius="10"
android:layout_marginBottom="20dp"
/>
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="5dp"
android:paddingBottom="10dp"
android:layout_marginTop="30dp"
android:hint="ussd code"
android:inputType="phone" />
<Button
android:id="@+id/btn_call"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/colorPrimary"
android:text="CALL"
android:layout_marginTop="10dp"
android:textColor="@android:color/white"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center|left"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RESPONSE : "
android:textSize="16dp"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold"
/>
<TextView
android:id="@+id/txt_talkBal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="@android:color/black"
android:text=""
/>
</LinearLayout>
</LinearLayout>
</ScrollView></LinearLayout>
たManifest.xml
<service android:name=".UssdDemo"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data android:name="android.accessibilityservice"
android:resource="@xml/config_service" />
</service>
@MikeM。この問題を克服するための他のアイデアがあるかどうか教えてください。 –
@MikeM。しかし私がussdを呼び出すとき、startActivityForResult()をstartActivity(intent)の代わりに使用することはできません –
@MikeM私はstartActivityForResult(intent)を行うことができないと思います。 android.content.Intent.ACTION_CALL –