Android本机来电屏幕上的弹出窗口,如真实来电者Android应用程序
我正在开发一个广播接收器,用于Android系统中的来电,在接到来电时,我想在本机来电屏幕上弹出一个弹出窗口 我完成了代码。但现在的问题是,在中,当电话铃响时,Android本机来电屏幕上的弹出窗口,如真实来电者Android应用程序,android,android-layout,android-widget,broadcastreceiver,android-broadcast,Android,Android Layout,Android Widget,Broadcastreceiver,Android Broadcast,我正在开发一个广播接收器,用于Android系统中的来电,在接到来电时,我想在本机来电屏幕上弹出一个弹出窗口 我完成了代码。但现在的问题是,在中,当电话铃响时,phone\u状态将作为OFF-HOOK,如果我正在调用某个活动,它会被调用,但它下面的代码不会被执行。我列出了代码: 我的广播接收机 我所称的活动: import android.app.Activity; import android.os.Bundle; import android.telephony.TelephonyManag
phone\u状态
将作为OFF-HOOK
,如果我正在调用某个活动,它会被调用,但它下面的代码不会被执行。我列出了代码:
我的广播接收机
我所称的活动:
import android.app.Activity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View.MeasureSpec;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
public class IncomingCallActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
Log.d("IncomingCallActivity: onCreate: ", "flag2");
*/ After this line, the code is not executed in Android 4.1 (Jelly Bean) only/*
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
Log.d("IncomingCallActivity: onCreate: ", "flagy");
setContentView(R.layout.main);
Log.d("IncomingCallActivity: onCreate: ", "flagz");
String number = getIntent().getStringExtra(
TelephonyManager.EXTRA_INCOMING_NUMBER);
TextView text = (TextView) findViewById(R.id.text);
text.setText("Incoming call from " + number);
}
catch (Exception e) {
Log.d("Exception", e.toString());
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
之后
该代码没有在Android4.1(JellyBean)中执行,但在其他版本中可以运行
我已经尝试了几乎所有我能做的方法。这段代码在本机通话屏幕上显示一个半透明的活动,它不会阻止背景控件,比如拿起电话。但我希望它像真正的呼叫者一样。我附上了一个关于真正的来电者如何在来电屏幕上显示窗口的快照
如何为Android应用程序实现此功能
真正的调用者就是这样工作的:
我目前的产出:
更新1
在赏金之后,我也没有得到我想要的东西,但我会回到一切;我正在努力。无论如何,这段代码适用于大多数Android手机。如果有人打算使用并获得解决方案,请写在这里,这样每个人都能从中受益
更新2
我尝试在广播接收器的onReceive方法中实现Toast,因为Toast是Android的本地组件,但它也没有在android4.1(jellybean)中显示出来
我的想法是在广播接收器的onReceive方法中实现Toast,然后根据我们的需要更改其设计,并调整其显示持续时间。但还有一个问题是,findViewById在广播接收器中不起作用,因此我认为我们必须以编程方式进行线性布局,以自定义toast。在使用
super.onCreate
方法之前尝试代码。我想在调用super之后,代码被跳过了。有时,这种技巧对我很有效。我不确定您的自定义GUI是否总是位于默认GUI之上,因为系统广播接收器和接收器都试图在屏幕顶部显示其GUI。我们不确定先调用哪一个,但要使GUI位于屏幕顶部,一个棘手的工作是当手机铃声响起时,在使用handler 1-2秒后调用您的活动
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Intent intent = new Intent(context, AcceptReject.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}, 2000);
我希望它能对你有所帮助。我认为你不应该为了达到所描述的结果而开始活动。您需要一个具有
布局参数的单独视图。在其布局参数中键入\u SYSTEM\u OVERLAY
您可以在屏幕上的任何位置定位此视图,或者只覆盖整个屏幕
以下是几行代码:
_av = new ActivatorView(this);
_avLayoutParams = new WindowManager.LayoutParams(0, 0, 0, 0,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
PixelFormat.OPAQUE);
_avLayoutParams.screenBrightness = _fScreenBrightness = 20f;
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(_av, _avLayoutParams);
完整的源代码,考虑它是一个示例。
< P>我刚刚在模拟器上测试过,它和TureCaleRe:public void onReceive(Context context, Intent intent) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSPARENT);
params.height = LayoutParams.MATCH_PARENT;
params.width = LayoutParams.MATCH_PARENT;
params.format = PixelFormat.TRANSLUCENT;
params.gravity = Gravity.TOP;
LinearLayout ly = new LinearLayout(context);
ly.setBackgroundColor(Color.RED);
ly.setOrientation(LinearLayout.VERTICAL);
wm.addView(ly, params);
}
在舱单中:
<receiver android:name="" android:enabled="true" >
<intent-filter android:priority="-1">
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
我也在研究它(我在这里理解你可能是错误的)。您想要实现的是在Android 4.2(果冻豆)中显示该活动。我只是设置了一个延迟来显示活动。我在不同的课堂上使用过PhoneStateListener。我能够在来电屏幕上显示新活动。这是我的全部代码:
文件MyBroadcastReceiver.java
文件CustomPhoneStateListener.java
你的活动将保持你所创造的状态。。。
注意:我在这段代码中也遇到了一些问题,它们就在这里
我正在尝试类似的方法,在来电屏幕上添加一个额外的按钮 Sam Adams发布的答案对我有用,尽管我是从PhoneStateListener调用代码的。除此之外,他的代码唯一真正的区别是我在膨胀布局:
overlay = (RelativeLayout) inflater.inflate(R.layout.overlay, null);
wm.addView(overlay, params);
它在模拟器和HTC One S(运行Android 4.1.1)上工作
您需要记住的是,保留对正在添加的覆盖视图的引用,并在手机返回空闲状态(当侦听器获取TelephonyManager.call_STATE_idle时)时再次删除它(windowmanager实例上的call removeView()),否则覆盖将保留在屏幕上
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if(overlay!=null)
{
wm.removeView(overlay);
overlay = null;
}
我的方法:
ps:wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
我们还面临着类似的问题,即覆盖层没有显示在带有pin锁的设备上。对我们有效的解决方案如下:
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mParams = new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT,
LayoutParams.TYPE_SYSTEM_ERROR,
LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
是布局参数。键入系统错误造成了差异。使用简单的广播接收器,并将此代码放入广播接收器:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Intent i = new Intent(context, CallingIncoming.class);
i.putExtras(intent);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK /*| Intent.FLAG_ACTIVITY_CLEAR_TASK*/);
context.startActivity(i);
}
}, 450);//It will help you to delay your screen and after it your screen will be top of default app screen
public void onReceive(final Context context, final Intent intent) {
Log.v(LOG_TAG, "Receieved notification about network status");
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSPARENT);
params.height = WindowManager.LayoutParams.MATCH_PARENT;
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.format = PixelFormat.TRANSLUCENT;
params.gravity = Gravity.TOP;
LinearLayout ly = new LinearLayout(context);
ly.setBackgroundColor(Color.RED);
ly.setOrientation(LinearLayout.VERTICAL);
wm.addView(ly, params);
}
试试这个
AlertDialog.Builder builder = new AlertDialog.Builder(context.getApplicationContext());
LayoutInflater inflater = LayoutInflater.from(context);
View dialogView = inflater.inflate(R.layout.caller_dialog, null);
ImageView button = dialogView.findViewById(R.id.close_btn);
builder.setView(dialogView);
final AlertDialog alert = builder.create();
alert.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
alert.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);
alert.setCanceledOnTouchOutside(true);
alert.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = alert.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
window.setGravity(Gravity.TOP);
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//close the service and remove the from from the window
alert.dismiss();
}
});
不,我以前没有在create上尝试过。我会试试的。但是,在super.oncreate()之前编写它的原因是什么呢?我将尝试一下,但背后的原因是什么。你为什么认为它会起作用没有理由。。我知道这听起来很疯狂,但我以前为我工作过(但不是这个问题),我会在周六尝试,因为我
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mParams = new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT,
LayoutParams.TYPE_SYSTEM_ERROR,
LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Intent i = new Intent(context, CallingIncoming.class);
i.putExtras(intent);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK /*| Intent.FLAG_ACTIVITY_CLEAR_TASK*/);
context.startActivity(i);
}
}, 450);//It will help you to delay your screen and after it your screen will be top of default app screen
public void onReceive(final Context context, final Intent intent) {
Log.v(LOG_TAG, "Receieved notification about network status");
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSPARENT);
params.height = WindowManager.LayoutParams.MATCH_PARENT;
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.format = PixelFormat.TRANSLUCENT;
params.gravity = Gravity.TOP;
LinearLayout ly = new LinearLayout(context);
ly.setBackgroundColor(Color.RED);
ly.setOrientation(LinearLayout.VERTICAL);
wm.addView(ly, params);
}
AlertDialog.Builder builder = new AlertDialog.Builder(context.getApplicationContext());
LayoutInflater inflater = LayoutInflater.from(context);
View dialogView = inflater.inflate(R.layout.caller_dialog, null);
ImageView button = dialogView.findViewById(R.id.close_btn);
builder.setView(dialogView);
final AlertDialog alert = builder.create();
alert.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
alert.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);
alert.setCanceledOnTouchOutside(true);
alert.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = alert.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
window.setGravity(Gravity.TOP);
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//close the service and remove the from from the window
alert.dismiss();
}
});