赞
踩
需求:当应用在后台运行时,当有电话来的时候,需要切换到前台显示。
android:launchMode="singleTask"
public static boolean isForeground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcessInfoList = activityManager.getRunningAppProcesses();
if (appProcessInfoList != null && appProcessInfoList.size() > 0) {
//枚举进程
for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessInfoList) {
if (appProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
if (appProcessInfo.processName.equals(context.getApplicationInfo().processName)) {
return true;
}
}
}
}
return false;
}
public static void setTopApp(Context context) {
if (!isForeground(context)) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//获得当前运行的task(任务)
List<ActivityManager.RunningTaskInfo> taskInfoList = activityManager.getRunningTasks(100);
for (ActivityManager.RunningTaskInfo taskInfo : taskInfoList) {
Log.e("MainActivity", "进程Task:" + taskInfo.topActivity.getPackageName());
//找到本应用的 task,并将它切换到前台
if (taskInfo.topActivity.getPackageName().equals(context.getPackageName())) {
activityManager.moveTaskToFront(taskInfo.id, 0);
break;
}
}
}
}
此时基本上实现了功能需求,网上教程基本上到这里也截止了,但是在尝试过后,很大可能是无法将应用从后台调转到前台的。因为现在手机版本高了,需要进行其他操作,申请悬浮窗权限.
悬浮窗权限和其他权限不同,需要进行单独申请,申请时也要转到系统界面开启。
<!-- 悬浮窗 -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
/**
* 确定对话框 单个按钮
*/
public class SingleDialog extends Dialog {
private TextView dialog_message;
private TextView dialog_ok;
private Context mContext;
private String tvMessage;
private String tvOk;
private OnClickListener onClickListener;
public SingleDialog(Context context) {
super(context);
}
public SingleDialog(Context context, String tvMessage, String tvBtn, OnClickListener onClickListenerPositive) {
super(context);
this.mContext = context;
this.tvMessage = tvMessage;
this.tvOk = tvBtn;
this.onClickListener = onClickListenerPositive;
}
protected SingleDialog(Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_draw_over);
dialog_message = findViewById(R.id.dialog_draw_message);
dialog_ok = findViewById(R.id.dialog_draw_ok);
Window dialogWindow = getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
DisplayMetrics d = mContext.getResources().getDisplayMetrics();
lp.width = (int) (d.widthPixels * 0.7);
lp.height = (int) (d.heightPixels * 0.7);
dialogWindow.setAttributes(lp);
dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
SingleDialog.this.setCanceledOnTouchOutside(false);
SingleDialog.this.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
} else {
return false;
}
}//屏蔽back键
});
dialog_message.setText(tvMessage);
dialog_ok.setText(tvOk);
dialog_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClickListener.onClick(SingleDialog.this, DialogInterface.BUTTON_POSITIVE);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/dialog_draw_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="15dp"
android:gravity="center"
android:textColor="#090808"
android:textSize="17sp" />
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="15dp"
android:background="#090808" />
<TextView
android:id="@+id/dialog_draw_ok"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:textColor="#090808"
android:textSize="16sp" />
</LinearLayout>
public class OverPermissionUtil {
//权限检测和申请
public static void requestDrawOverlays(Activity activity) {
if (!Settings.canDrawOverlays(activity)) {
new SingleDialog(activity, "当前未获取悬浮窗权限", "去开启", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
startActivityForResult(activity,
new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + activity.getPackageName())),
0, null);
}
}).show();
}
}
}
//因为调用一次可能调不过去,后台应用无法转前台应用,多调用几次就好了。
if (!ProcessUtil.isForeground(context)) {
for (int i = 0; i < 10; i++) {
if (ProcessUtil.isForeground(context)) {
break;
} else {
ProcessUtil.setTopApp(context);
}
}
}
//这一种方式代码少
Intent intent = context.getPackageManager().
getLaunchIntentForPackage(context.getPackageName());
context.startActivity(intent);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。