2016-04-07 17 views
0

Hier rufe ich USSD-Code an und versuche dann, die entsprechende Antwort zu lesen, indem ich AccessibilityService erweitere. Dieser Prozess funktioniert gut. Aber mein Problem ist, wenn ich versuche, die entsprechenden Daten (oder Wert) mit LocalBroadCast Manager zu Aktivität sagen (MainActivity.java) BroadcastReceiver nicht einmal aufgerufen werden.Werte von Local BroadcastManager können nicht an Aktivität gesendet werden

Kann mir jemand helfen, Daten mit LocalBroadcastManager zu Aktivität zu übertragen?

Hier finden Sie meine Java-Klassen.

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> 
+0

@MikeM. Können Sie mir sagen, ob Sie eine andere Idee haben, dieses Problem zu lösen? –

+0

@MikeM. Aber wenn ich ussd anrufe, kann ich statt startActivityForResult() statt startActivity (intent) –

+0

@MikeM verwenden. Ich denke, wir können nicht startenActivityForResult (Absicht); Wenn wir android.content.Intent.ACTION_CALL aufrufen –

Antwort

0

Gerade register und unregister Ihre BroadcastReceiver in OnResume() und OnPause() Lifecycles des activity.No müssen in OnCreate() und OnDestroy() .Auch prüfen Action in BroadcastReciver der onReceive() wie

registrieren und deregistrieren
if(intent.getAction().equals("your action")){ 
     //do something for that action 
    } 

Überprüfen Sie auch, ob der sendBroadcast in Ihrem Dienst aufgerufen wird.