2016-04-07 60 views
0

在這裏,我打電話給USSD代碼,然後我試圖通過擴展AccessibilityService來讀取相應的響應。這個過程工作正常。 但我的問題是,當我試圖轉移相應的數據(或價值)使用LocalBroadCast管理器活動說(MainActivity.java) BroadcastReceiver甚至沒有被調用。無法將值從本地BroadcastManager發送到活動

任何人都可以幫助我使用LocalBroadcastManager傳輸數據到活動?

請在下面找到我的java課程。

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。你可以告訴我,如果你有任何其他想法來克服這個問題? –

+0

@MikeM。但是當我調用ussd時,我無法使用startActivityForResult()而不是startActivity(intent) –

+0

@MikeM。我認爲我們無法做到startActivityForResult(intent);當我調用android.content.Intent.ACTION_CALL –

回答

0

只是registerunregisterBroadcastReceiverOnResume()和activity.No的OnPause()生命週期需要註冊和OnCreate()OnDestroy()註銷。還有在 檢查Action BroadcastReciver的onReceive()

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

同時檢查sendBroadcast是否在您的服務中被調用。

相關問題