如何从服务(aidl)向活动发送数据

时间:2013-11-20 05:53:25

标签: android github ussd

我想在我的应用中开发一个应用 ussd消息,我已经从https://github.com/alaasalman/ussdinterceptor下载了简单的源代码,这对我很有用。但我不知道如何从 CDUSSDService 发送uri数据并​​在我的活动中收到。这是CDUSSDService class

公共类CDUSSDService扩展了服务{

    private String TAG = CDUSSDService.class.getSimpleName();
    private boolean mActive = false;  //we will only activate this "USSD listener" when we want it

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(Intent.ACTION_INSERT)){
                //activity wishes to listen to USSD returns, so activate this
                mActive = true;
                Log.d(TAG, "activate ussd listener");
            }
            else if(intent.getAction().equals(Intent.ACTION_DELETE)){
                mActive = false;
                Log.d(TAG, "deactivate ussd listener");
            }
        }
    };

    private final IExtendedNetworkService.Stub mBinder = new IExtendedNetworkService.Stub () {
        public void clearMmiString() throws RemoteException {
            Log.d(TAG, "called clear");
        }

        public void setMmiString(String number) throws RemoteException {
            Log.d (TAG, "setMmiString:" + number);
        }

        public CharSequence getMmiRunningText() throws RemoteException {
            if(mActive == true){
                return null;
            }

            return "USSD Running";
        }

        public CharSequence getUserMessage(CharSequence text)
                throws RemoteException {
            Log.d(TAG, "get user message " + text);

            if(mActive == false){
                //listener is still inactive, so return whatever we got
                Log.d(TAG, "inactive " + text);
                return text;
            }

            //listener is active, so broadcast data and suppress it from default behavior

            //build data to send with intent for activity, format URI as per RFC 2396
            Uri ussdDataUri = new Uri.Builder()
            .scheme(getBaseContext().getString(R.string.uri_scheme))
            .authority(getBaseContext().getString(R.string.uri_authority))
            .path(getBaseContext().getString(R.string.uri_path))
            .appendQueryParameter(getBaseContext().getString(R.string.uri_param_name), text.toString())
            .build();

            sendBroadcast(new Intent(Intent.ACTION_GET_CONTENT, ussdDataUri));

            mActive = false;
            return null;
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "called onbind");

        //the insert/delete intents will be fired by activity to activate/deactivate listener since service cannot be stopped
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_INSERT);
        filter.addAction(Intent.ACTION_DELETE);
        filter.addDataScheme(getBaseContext().getString(R.string.uri_scheme));
        filter.addDataAuthority(getBaseContext().getString(R.string.uri_authority), null);
        filter.addDataPath(getBaseContext().getString(R.string.uri_path), PatternMatcher.PATTERN_LITERAL);
        registerReceiver(receiver, filter);

        return mBinder;
    }   
}

我是Android的新手,我不明白如何修改它:

sendBroadcast(new Intent(Intent.ACTION_GET_CONTENT, ussdDataUri)) 

在  getUserMessage(CharSequence text) 并在我的活动中接收意图数据。请帮助我。谢谢。

0 个答案:

没有答案