以下是该方案:
显示活动(活动)。如果来电话,活动应该接收意图(将“来电屏幕”发送到后台/将其从显示器中隐藏)并且其本身对用户可见。我不一定要打电话来打电话,因为我在很多问题中都读到了公共API无法解决的问题。
我想要的只是以某种方式使我的活动隐藏在顶部隐藏的Android默认来电屏幕。
仅当我的活动可见时才需要此行为,这与使用PHONE_STATE广播接收器启动我的活动不相等。后一个问题在SO上得到了多次回答。
请帮帮我。我一直在寻找近一天的方向。
感谢您的时间。
答案 0 :(得分:5)
这就是我解决它的方法:
<强>的Manifest.xml 强>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
...
<receiver android:name=".MyPhoneBroadcastReceiver">
<intent-filter android:priority="99999">
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<activity android:name=".LockScreenActivity" android:noHistory="true" android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.ANSWER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<强> MyPhoneBroadcastReceiver.java 强>
public void onReceive(final Context context, Intent intent) {
Bundle extras = intent.getExtras();
...
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
new Handler().postDelayed(new Runnable() {
public void run() {
Intent intentPhoneCall = new Intent("android.intent.action.ANSWER");
intentPhoneCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentPhoneCall);
}
}, 100);
}
}
}
LockScreenActivity.java - 一个带有UI的常规活动类,显示屏幕已被锁定。它覆盖了屏幕的100%区域,即没有导航/状态栏。 HOME / MENU键也被禁用。这就是我实现的目标:How can I detect user pressing HOME key in my activity?
P.S。 :技巧不是主要逻辑,而是延迟100ms。没有它,每当您通过电话拨打电话时,系统默认来电屏幕都会删除您的自定义(家庭)锁定屏幕!
答案 1 :(得分:1)
@Override
public void onReceive(final Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
final String incomingNumber = extras.getString("incoming_number");
Handler callActionHandler = new Handler();
Runnable runRingingActivity = new Runnable() {
@Override
public void run() {
Intent intentPhoneCall = new Intent("android.intent.action.ANSWER");
intentPhoneCall.putExtra("INCOMING_NUM", incomingNumber);
intentPhoneCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentPhoneCall);
}
};
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
callActionHandler.postDelayed(runRingingActivity, 2000);
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
callActionHandler.removeCallbacks(runRingingActivity);
// setResultCode(Activity.RESULT_CANCELED);
}
}
}
然后在接收器类侧使用PhoneStateListener ..我就这样使用....
/*********** My receiver class onCreate() contains ************/
TelephonyManager telephony = (TelephonyManager)
getSystemService(getApplicationContext().TELEPHONY_SERVICE);
telephony.listen(myListener, PhoneStateListener.LISTEN_CALL_STATE);
class MyPhoneStateListener extends PhoneStateListener {
Activity curActivity;
public MyPhoneStateListener(Activity _curActivity){
this.curActivity = _curActivity;
}
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.i("TEST APP", "Cal End");
curActivity.finish();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i("TEST APP", "Hello");
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.i("TEST APP", "Calling...");
break;
}
}
}