正如我们从已经著名的blog post 中学习的那样
首先,查看 Android 源代码中的 PhoneUtils 类。
[...]
具体来说,查看第 217 行,一个名为
“com.android.ussd.IExtendedNetworkService” 正在编写中。 那又怎样
你需要做的是实现你自己的服务来响应
意图。该服务需要按照
IExtendedNetworkService.aidl,它是 Android 框架的一部分。
从哪里开始?最好关注这个更出名的blog post
首先,我们了解基础知识:
- 我们需要一个实现
IExtendedNetworkService 接口的服务。
- 服务将知道意图
"com.android.ussd.IExtendedNetworkService",因此它会在应用清单中具有相应的意图过滤器。
- 我们知道
com.android.phone.PhoneUtils 将绑定到该服务。 (如果你不知道什么是绑定服务,请参考here)
- 在 Bind 上,我们返回一个 binder,其中包含 PhoneUtils 实际调用的函数
- 该服务必须正在运行,因此我们将在系统重新启动时启动它。为此,我们需要一个广播接收器。 (如果你不知道这是什么,想更好地了解这一切,请参考here)
让我们开始吧。
首先是接收器,这是最简单的部分。我们用这些内容创建一个名为 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));
}
}
现在,服务本身。我自己没有尝试过,但我已经阅读了代码here 以找到方法解释,我已经放入了 cmets。我还编辑了一些东西。
据我了解,您将在 getUserMessage 中收到实际文本,在那里您解析文本,并在弹出窗口中返回您想要的内容。如果您不想要弹出窗口,请返回 null。因此,您还应该在此处对该文本进行任何其他操作。
public class USSDDumbExtendedNetworkService extends Service {
public static final String TAG = "ExtNetService";
public static CharSequence mRetVal = null;
public static boolean mActive = true;
private boolean change = false;
private String msgUssdRunning = null;
private final IExtendedNetworkService.Stub mBinder = new IExtendedNetworkService.Stub() {
//Set a MMI/USSD command to ExtendedNetworkService for further process. This should be called when a MMI command is placed from panel.
//we don't need it in this case
@Override
public void setMmiString(String number) throws RemoteException {
}
//return the specific string which is used to prompt MMI/USSD is running
@Override
public CharSequence getMmiRunningText() throws RemoteException {
return msgUssdRunning;
}
//Get specific message which should be displayed on pop-up dialog.
Parameters:
text original MMI/USSD message response from framework
Returns:
specific user message correspond to text. null stands for no pop-up dialog need to show.
@Override
public CharSequence getUserMessage(CharSequence text)
throws RemoteException {
return text;
}
//Clear pre-set MMI/USSD command. This should be called when user cancel a pre-dialed MMI command.
//we don't need it in this case
@Override
public void clearMmiString() throws RemoteException {
}
};
@Override
public IBinder onBind(Intent intent) {
msgUssdRunning = "Some text to show";
return mBinder;
}
public IBinder asBinder() {
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}}
最后一部分,清单。您必须注册服务和广播接收器
<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>