gpt4 book ai didi

android - GetTY allState 始终返回空闲状态

转载 作者:太空狗 更新时间:2023-10-29 12:44:56 26 4
gpt4 key购买 nike

以下是我的代码,我在其中使用反射来访问 com.android.internal.telephony.CallManager 。我正在使用它的函数 GetActiveFGCallState() 来获得不同的状态,例如拨出调用的警报,但它一直让我空闲即使状态为 OFF_HOOK 或 RINGING。所以我从中了解到我的调用不在前台,因为如果没有 Activity 的前台调用,则 Activity 前台调用的状态返回 IDLE。那么我的理解正确吗?如果是,那么如何将调用 Activity 带到前台?如果不是,那么可能是什么问题?

主要 Activity 代码:

package com.example.misscall;


import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {
final Context context = this;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent callIntent = new Intent(Intent.ACTION_CALL,
Uri.parse("tel:" + "03365188508"));
callIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(callIntent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

主要文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.misscall"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-feature android:name="android.hardware.telephony">
</uses-feature>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<receiver android:name="com.example.misscall.DataConnection">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
<activity
android:name="com.example.misscall.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

数据连接类

    import android.os.RemoteException;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class DataConnection extends BroadcastReceiver {
TelephonyManager Tm;
ITelephony telephonyService;
Class c = null;
Method methodGetInstance = null;
Method methodGetActiveFgCallState=null;
String TAG="Tag";
Object objectCallManager=null;
@Override
public void onReceive(final Context context, Intent intent) {

Tm=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
final ClassLoader classLoader = this.getClass().getClassLoader();
try {
final Class<?> classCallManager = classLoader.loadClass("com.android.internal.telephony.CallManager");
Log.e(TAG, "CallManager: Class loaded " + classCallManager.toString());

methodGetInstance = classCallManager.getDeclaredMethod("getInstance");
methodGetInstance.setAccessible(true);
Log.e(TAG, "CallManager: Method loaded " + methodGetInstance.getName());

objectCallManager = methodGetInstance.invoke(null);
Log.e(TAG, "CallManager: Object loaded " + objectCallManager.getClass().getName());

Method[] aClassMethods = classCallManager.getDeclaredMethods();
for(Method m : aClassMethods)
{
Log.e("MEthods",m.getName());
}
methodGetActiveFgCallState = classCallManager.getDeclaredMethod("getActiveFgCallState");
Log.e(TAG, "CallManager: Method loaded " + methodGetActiveFgCallState.getName());

Log.e(TAG, "CallManager: What is the Call state = " + methodGetActiveFgCallState.invoke(objectCallManager));
}
catch (ClassNotFoundException e) {
Log.e(TAG, e.getClass().getName() + e.toString());
}
catch (NoSuchMethodException e) {
Log.e(TAG, e.getClass().getName() + e.toString());
}
catch (InvocationTargetException e) {
Log.e(TAG, e.getClass().getName() + e.toString());
}
catch (IllegalAccessException e) {
Log.e(TAG, e.getClass().getName() + e.toString());
}
Tm.listen(new PhoneStateListener(){
public void onCallStateChanged(int state,String number) {
super.onCallStateChanged(state, number);

try {
if (methodGetActiveFgCallState.invoke(objectCallManager).toString().toLowerCase() .equals("idle"))
{
Toast.makeText(context, "I am in idle state", Toast.LENGTH_LONG).show();
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

},PhoneStateListener.LISTEN_CALL_STATE);


}

}

最佳答案

请尝试以下操作:

    PhoneStateListener callStateListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber)
{
if(state==TelephonyManager.CALL_STATE_RINGING)
{
Toast.makeText(getApplicationContext(),"Phone Is Riging", Toast.LENGTH_LONG).show();

}
if(state==TelephonyManager.CALL_STATE_OFFHOOK)
{
Toast.makeText(getApplicationContext(),"Phone is Currently in A call", Toast.LENGTH_LONG).show();
}

if(state==TelephonyManager.CALL_STATE_IDLE)
{
Toast.makeText(getApplicationContext(),"phone is neither ringing nor in a call", Toast.LENGTH_LONG).show();
}
}
};


telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);

Reference .

关于android - GetTY allState 始终返回空闲状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19651416/

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