第一中方法:
private void showDialog(){
Context context = getActivity();//不是在activity類中寫的,所以需要獲取context對象
View view = LayoutInflater.from(context).inflate(R.layout.high_opinion_dialog_layout,null,false);
final AlertDialog dialog = new AlertDialog.Builder(context).setView(view).create();
Button btn_cancel_high_opion = view.findViewById(R.id.no);
Button btn_agree_high_opion = view.findViewById(R.id.yes);
RadioButton radioButton = view.findViewById(R.id.radioButton);
radioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.System.putInt(context.getContentResolver(),"isShow",1);
}
});
btn_cancel_high_opion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.System.putInt(context.getContentResolver(),"vowifiIsChecked",0);
//Settings.System.putInt(context.getContentResolver(),"isSwitch",0);
isSwitch = true;
mSwitchPref.setChecked(false);
// updateWfcMode(context, false);
dialog.dismiss();
}
});
btn_agree_high_opion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
//此處設置位置窗體大小,我這裏設置爲了手機屏幕寬度的3/4 注意一定要在show方法調用後再寫設置窗口大小的代碼,否則不起效果會
dialog.getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
}
high_opinion_dialog_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/bg_dialog_white_color"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Emergency calls via Wi-Fi Calling"
android:textSize="16sp" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Emergency calls are not possible via Wi-Fi Calling.the device switches automatically to a cellular network(2G/3G) to place an emergency call.Emergency calls are only possible in areas with cellular coverage."
android:textSize="14sp" />
<RadioButton
android:id="@+id/radioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginBottom="10dp"
android:drawablePadding="4dp"
android:text="Don't show again" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="14dp"
android:orientation="horizontal">
<Button
android:id="@+id/no"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg_dialog_btn_left"
android:gravity="center"
android:singleLine="true"
android:text="Cancle"
android:textAllCaps="false"
android:textColor="#007"
android:textSize="16sp" />
<Button
android:id="@+id/yes"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@drawable/bg_dialog_btn_right"
android:singleLine="true"
android:text="Turn on"
android:textAllCaps="false"
android:textColor="#007"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
在代碼端引用:
showDialog();
效果圖:
第二種方法:
AndroidManifest.xml中進行註冊:
<activity android:name="com.android.settings.AlertDialogActivity"
android:excludeFromRecents="true"
android:theme="@style/myTransparent">
</activity>
AlertDialogActivity.java的內容:
package com.android.systemui;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Window;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.graphics.Color;
import android.view.MotionEvent;
public class AlertDialogActivity extends Activity {
static final String DEVICE_ROOT_TITLE = "Warning";
static final String DEVICE_ROOT_MESSAGE = "This device has been root, there are security risks, please restore the boot";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
AlertDialog d = builder.create();
d.setCancelable(false);
d.setTitle(DEVICE_ROOT_TITLE);
d.setMessage(DEVICE_ROOT_MESSAGE);
d.setButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
d.show();
setFinishOnTouchOutside(false);
}
@Override
public void onBackPressed(){
//do nothing
}
}
color.xml
<color name="transparent">#0000</color>
styles.xml
<style name="myTransparent">
<item name="android:windowBackground">@color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item>
</style>
class KeyguardUpdateMonitor.java中使用:
import com.android.systemui.AlertDialogActivity;
import java.lang.reflect.Field;
import java.util.Map;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
dispatchBootCompleted();
Settings.System.putInt(mContext.getContentResolver(), "is_system_root_check_support", 1);
if(isRoot()){
mHandler.postDelayed(new Runnable(){
public void run(){
Intent intent1 = new Intent();
intent1.setClass(context,AlertDialogActivity.class);
android.util.Log.d("zhangdi","111111111111111111111111111");
context.startActivity(intent1);
}
}, 10000);
}
}
......
public boolean isRoot() {
boolean root = false;
try {
String buildType = "";
try {
Class<?> cls = Class.forName("android.os.SystemProperties");
Method m = cls.getDeclaredMethod("get",String.class,String.class);
buildType = (String)m.invoke(null,"ro.build.type","");
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if(!"eng".equals(buildType)){
if ((!new File("/system/bin/su").exists()) && (!new File("/system/xbin/su").exists()) && (!new File("/system/sbin/su").exists())) {
root = false;
} else {
root = true;
}
}else{
}
} catch (Exception e) {
}
return root;
}
效果圖: