gpt4 book ai didi

android - 阻止 USSD 对话并读取 USSD 响应?

转载 作者:IT老高 更新时间:2023-10-28 23:30:13 25 4
gpt4 key购买 nike

我对 ussd 进行了很多研究,但我无法阅读 USSD 响应,以及如何防止类似此应用程序的 ussd 对话。

https://play.google.com/store/apps/details?id=com.iba.ussdchecker&hl=en

我安装了这个 https://github.com/alaasalman/ussdinterceptor重启我的手机(S2 android 4.0.3),发送ussd代码但没有任何反应,有人告诉我必须阅读日志,但我该怎么做。

我尝试使用这些代码从日志中读取 USSD

USSD.java

    package com.example.ussd;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import android.util.Log;

public class USSD {

private static String startmsg = "displayMMIComplete"; // start msg to look
// for
private static String endmsg = "MMI code has finished running"; // stop msg
private static String trimmsg = "- using text from MMI message: '"; // a msg
// to
// remove
// from
// the
// text

private long before = 3000; // delay (ms) before creation of the class
// before a msg (USDD) is valid (use timestamp)
private long after = 3000; // delay (ms) after creation of the class that a
// msg (USDD) is valid (wait after ms)

private String msg = ""; // the USSD message
private boolean found = false;
private long t = -1; // timestamp of the found log

public USSD() {
this(3000, 3000);
}

// USSD in log : example
public USSD(long before_creation, long after_creation) {
before = before_creation;
after = after_creation;
long timestamp = System.currentTimeMillis(); // creation of the class
// --> look for the USSD
// msg in the logs
Log.d("USSDClass",
"Class creation - timestamp: " + String.valueOf(timestamp));
try {
// sample code taken from alogcat ...
Process logcatProc = Runtime.getRuntime().exec(
"logcat -v time -b main PhoneUtils:D"); // get PhoneUtils
// debug log with
// time information
BufferedReader mReader = new BufferedReader(new InputStreamReader(
logcatProc.getInputStream()), 1024);
String line = "";
boolean tostop = false;
long stop = timestamp + after; // to stop the while after "after" ms
while (((line = mReader.readLine()) != null)
&& (System.currentTimeMillis() < stop) && (tostop == false)) {
if (line.length() > 19) // the line should be at least with a
// length of a timestamp (19) !
{
if (line.contains(startmsg)) // check if it is a USSD msg
{
// log example :
// "12-10 20:36:39.321 D/PhoneUtils( 178): displayMMIComplete: state=COMPLETE"
t = extracttimestamp(line); // extract the timestamp of
// thie msg
Log.d("USSDClass", "Found line at timestamp : "
+ String.valueOf(t));
if (t >= timestamp - before)
found = true; // start of an USDD is found & is
// recent !
} else if (found) {
// log example :
// "12-10 20:36:39.321 D/PhoneUtils( 178): displayMMIComplete: state=COMPLETE"
if (line.contains(endmsg))
tostop = true;
else {
// log example :
// "12-10 20:36:39.321 D/PhoneUtils( 178): - using text from MMI message: 'Your USSD message with one or several lines"
Log.d("USSDClass", "Line content : " + line);
String[] v = line.split("\\): "); // doesn't need
// log
// information
// --> split
// with "): "
// separator
if (v.length > 1)
msg += v[1].replace(trimmsg, "").trim() + "\n";

}
}
}
}
} catch (IOException e) {
Log.d("USSDClass", "Exception:" + e.toString());
}
}

public boolean IsFound() {
return found;
}

public String getMsg() {
return msg;
}

// extract timestamp from a log line with format
// "MM-dd HH:mm:ss.ms Level/App:msg" Example : 12-10 20:36:39.321
// Note : known bug : happy new year check will not work !!!
private long extracttimestamp(String line) {
long timestamp = -1; // default value if no timestamp is found
String[] v = line.split(" ");
if (v.length > 1) // check if there is space
{
Calendar C = Calendar.getInstance();
int y = C.get(Calendar.YEAR);
String txt = v[0] + "-" + y + " " + v[1]; // transform in format
// "MM-dd-yyyy HH:mm:ss"
SimpleDateFormat formatter = new SimpleDateFormat(
"MM-dd-yyyy HH:mm:ss");
try {
Date tmp = formatter.parse(txt);
timestamp = tmp.getTime();
String[] ms = v[1].split("."); // get ms
if (ms.length > 1)
timestamp += Integer.getInteger(ms[1]);

} catch (ParseException e) {
Log.d("USSDClass",
"USDD.extractimestamp exception:" + e.toString());
}
}
return timestamp;

}

}

UssdActivity.java

package com.example.ussd;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;

public class UssdActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private TextView view;
private AutoCompleteTextView number;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
this.view = (TextView) findViewById(R.id.Text2);
this.number = (AutoCompleteTextView) findViewById(R.id.Text1);
}

@Override
public void onClick(View arg0) {
String encodedHash = Uri.encode("#");
call("*" + number.getText() + encodedHash);
this.view.setText("");
}

protected void call(String phoneNumber) {
try {
startActivityForResult(
new Intent("android.intent.action.CALL", Uri.parse("tel:"
+ phoneNumber)), 1);
} catch (Exception eExcept) {
this.view.append("\n\n " + "\n" + eExcept.toString());
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
USSD ussd = new USSD(4000, 4000);
if (ussd.IsFound())
this.view.append("\n test" + ussd.getMsg());
else
this.view.append("" + R.string.error_ussd_msg);
}

}

输出是这样的:

com.sec.android.app.callsetting.allcalls:com.sec.android.callsetting.allcalls.AllCallsProvider Terminated ALSA PLAYBACK device hifi

最佳答案

可以使用无障碍服务。首先创建一个服务类:

public class USSDService extends AccessibilityService {

public static String TAG = USSDService.class.getSimpleName();

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
Log.d(TAG, "onAccessibilityEvent");

AccessibilityNodeInfo source = event.getSource();
/* if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && !event.getClassName().equals("android.app.AlertDialog")) { // android.app.AlertDialog is the standard but not for all phones */
if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && !String.valueOf(event.getClassName()).contains("AlertDialog")) {
return;
}
if(event.getEventType() == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && (source == null || !source.getClassName().equals("android.widget.TextView"))) {
return;
}
if(event.getEventType() == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && TextUtils.isEmpty(source.getText())) {
return;
}

List<CharSequence> eventText;

if(event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
eventText = event.getText();
} else {
eventText = Collections.singletonList(source.getText());
}

String text = processUSSDText(eventText);

if( TextUtils.isEmpty(text) ) return;

// Close dialog
performGlobalAction(GLOBAL_ACTION_BACK); // This works on 4.1+ only

Log.d(TAG, text);
// Handle USSD response here

}

private String processUSSDText(List<CharSequence> eventText) {
for (CharSequence s : eventText) {
String text = String.valueOf(s);
// Return text if text is the expected ussd response
if( true ) {
return text;
}
}
return null;
}

@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 | AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED;
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
setServiceInfo(info);
}
}

在 Android list 中声明它

<service android:name=".USSDService"
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/ussd_service" />
</service>

创建一个描述无障碍服务的 xml 文件,名为 ussd_service

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeWindowStateChanged|typeWindowContentChanged"
android:accessibilityFeedbackType="feedbackGeneric"
android:accessibilityFlags="flagDefault"
android:canRetrieveWindowContent="true"
android:description="@string/accessibility_service_description"
android:notificationTimeout="0"
android:packageNames="com.android.phone" />

就是这样。安装应用后,您必须在辅助功能设置中启用该服务(设置->辅助功能设置-> YourAppName)。

解决方案描述herehere (russian) .

关于android - 阻止 USSD 对话并读取 USSD 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22057625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com