当前位置: 首页 > 面试题库 >

使用IExtendedNetworkService在Android中获取USSD响应

左丘昕
2023-03-14
问题内容

我正在尝试寻找在Android中发出USSD请求的方法。我找到了-http://commandus.com/blog/?p=58。我将所有需要的文件添加到我的项目中。

USSDDumbExtendedNetworkService.java:

package com.android.ussdcodes;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.IBinder;
import android.os.PatternMatcher;
import android.os.RemoteException;
import android.util.Log;

import com.android.internal.telephony.IExtendedNetworkService;
import com.android.ussdcodes.R;

/**
 * Service implements IExtendedNetworkService interface. 
 * USSDDumbExtendedNetworkService
 * Service must have name "com.android.ussd.IExtendedNetworkService" of the intent declared
 * in the Android manifest file so com.android.phone.PhoneUtils class bind
 * to this service after system rebooted.
 * Please note service is loaded after system reboot! 
 * Your application must check is system rebooted. 
 * @see Util#syslogHasLine(String, String, String, boolean)   
 */
public class USSDDumbExtendedNetworkService extends Service {
    public static final String TAG = "CommandusUSSDExtNetSvc";
    public static final String LOG_STAMP = "*USSDTestExtendedNetworkService bind successfully*";
    public static final String URI_SCHEME = "ussdcodes";
    public static final String URI_AUTHORITY = "android.com";
    public static final String URI_PATH = "/";
    public static final String URI_PAR = "return";
    public static final String URI_PARON = "on";
    public static final String URI_PAROFF = "off";
    public static final String MAGIC_ON = ":ON;)";
    public static final String MAGIC_OFF = ":OFF;(";
    public static final String MAGIC_RETVAL = ":RETVAL;(";

    private static boolean mActive = false;
    private static CharSequence mRetVal = null;
    private Context mContext = null;
    private String msgUssdRunning = "USSD running...";

    final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_INSERT.equals(intent.getAction())) {
                mContext = context;
                if (mContext != null) {
                    msgUssdRunning = mContext.getString(R.string.USSD_run);
                    mActive = true;
                    Log.d(TAG, "activate");
                }
            } else if (Intent.ACTION_DELETE.equals(intent.getAction())) {
                mContext = null;
                mActive = false;
                Log.d(TAG, "deactivate");
            }
        }
    };

    private final IExtendedNetworkService.Stub mBinder = new IExtendedNetworkService.Stub() {
        @Override
        public void setMmiString(String number) throws RemoteException {
            Log.d(TAG, "setMmiString: " + number);
        }

        @Override
        public CharSequence getMmiRunningText() throws RemoteException {
            Log.d(TAG, "getMmiRunningText: " + msgUssdRunning);
            return msgUssdRunning;
        }

        @Override
        public CharSequence getUserMessage(CharSequence text)
                throws RemoteException {
            if (MAGIC_ON.contentEquals(text)) {
                mActive = true;
                Log.d(TAG, "control: ON");
                return text;
            } else {
                if (MAGIC_OFF.contentEquals(text)) {
                    mActive = false;
                    Log.d(TAG, "control: OFF");
                    return text;
                } else {
                    if (MAGIC_RETVAL.contentEquals(text)) {
                        mActive = false;
                        Log.d(TAG, "control: return");
                        return mRetVal;
                    }
                }
            }

            if (!mActive) {
                Log.d(TAG, "getUserMessage deactivated: " + text);
                return text;
            }
            String s = text.toString();
            // store s to the !
            Uri uri = new Uri.Builder()
                .scheme(URI_SCHEME)
                .authority(URI_AUTHORITY)
                .path(URI_PATH)
                .appendQueryParameter(URI_PAR, text.toString())
                .build();
            sendBroadcast(new Intent(Intent.ACTION_GET_CONTENT, uri));
            mActive = false;
            mRetVal = text;
            Log.d(TAG, "getUserMessage: " + text + "=" + s);
            return null;
        }

        @Override
        public void clearMmiString() throws RemoteException {
            Log.d(TAG, "clearMmiString");
        }
    };


    /**
     * Put stamp to the system log when PhoneUtils bind to the service
     * after Android has rebooted. Application must call {@link Util#syslogHasLine(String, String, String, boolean)} to 
     * check is phone rebooted or no. Without reboot phone application does not bind tom this service! 
     */
    @Override
    public IBinder onBind(Intent intent) {
        // Do not localize!
        Log.i(TAG, LOG_STAMP);
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_INSERT);
        filter.addAction(Intent.ACTION_DELETE);
        filter.addDataScheme(URI_SCHEME);
        filter.addDataAuthority(URI_AUTHORITY, null);
        filter.addDataPath(URI_PATH, PatternMatcher.PATTERN_LITERAL);
        registerReceiver(mReceiver, filter);

        return mBinder; 
    }

    public IBinder asBinder() {
        Log.d(TAG, "asBinder");
        return mBinder;
    }

}

表现:

<receiver android:name="com.android.ussdcodes.BootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service
            android:name=".USSDDumbExtendedNetworkService" >
            <intent-filter android:icon="@drawable/ic_launcher">
                <action android:name="com.android.ussd.IExtendedNetworkService" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </service>

BootReceiver.java:

package com.android.ussdcodes;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.d("USSDService", context.getString(R.string.service_started));
        context.startService(new Intent(context,USSDDumbExtendedNetworkService.class));
    }

}

因此,现在该服务在启动完成后启动。

我的问题是我如何发送USSD请求并获得服务响应?谢谢!)


问题答案:

好吧,我找到了答案。

我只是把链接放在要点上。

停用消息

USSDDumbExtendedNetworkService.mActive = false;

发送USSD:

Intent launchCall = new Intent(Intent.ACTION_CALL,
                        Uri.parse("tel:" + Uri.encode(ussdcode)));
launchCall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
launchCall.addFlags(Intent.FLAG_FROM_BACKGROUND);
startActivity(launchCall);

再次激活消息

USSDDumbExtendedNetworkService.mActive = true;
USSDDumbExtendedNetworkService.mRetVal = null;


 类似资料:
  • 我的问题是我如何发送USSD请求并得到服务响应?(谢谢!)

  • 我想找到一种从USSD代码中得到响应的方法。 我正在使用索尼爱立信Xperia Ray,我只在手机屏幕上找到了弹出的消息。我没有从后台得到任何响应代码。

  • 我正在尝试解析运营商网络的USSD响应。找到一个链接https://github.com/alaasalman/ussdinterceptor,但它不支持4.2.2及以上版本。是否有任何api或方法可以达到此目的?

  • 相关问题 < code>Json的响应是< code >新行分隔符Json: 我正在使用来提出请求: 我可以在我的中看到响应,但收到以下错误: 中格式错误的JSON < li >响应在< code>NdJson中,它无法正确解析它 如何正确解析它?

  • 当我向服务器发布一些url时,我试图在webview中获取响应标题。我使用的是shouldInterceptRequest方法。 此方法返回WebResource响应对象。但我不知道如何从中获取响应标头。 默认情况下,返回super。shouldInterceptRequest(视图、请求) 返回null。 那么,应该做些什么才能捕捉到实际的webview响应呢。