android源碼中添加自己的佈局文件與PhonewindowManager裏調用

思考

1.往android framework 中添加自己的佈局需要在什麼位置添加?
2.直接加上去會遇到什麼問題?

源碼

佈局
// An highlighted block
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
	  <TextView
        android:id="@+android:id/tv"
        android:layout_width="match_parent"
        android:text="Bitte berechnen Sie das Ergebnis der folgenden mathematischen Formel zur Überprüfung"
        android:layout_margin="16dp"
        android:textSize="25sp"
        android:textColor="@android:color/black"
		android:visibility="gone"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+android:id/vs_tv_number"
        android:layout_width="match_parent"
        android:text="19 * 23 = ?"
        android:layout_margin="16dp"
        android:textSize="30sp"
        android:layout_height="35dp"/>

    <EditText
        android:id="@+android:id/vs_et_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:inputType="text"/>
</LinearLayout>

以上佈局代碼跟普通apk唯一有區別就是id,普通apk是@+id/
往framework里加普通apk的佈局,只需把普通佈局的改成@+android:id/即可。然後把佈局文件拷貝到framework/base/core/res/res/xml/(xml是我自己要拷到這個目錄,也可以拷到layout目錄)
到這之後還需要改幾個地方:framework/base/core/res/res/value/目錄下的symbols.xml,public.xml和ids.xml。
symbols.xml —是把佈局文件名寫入

ids.xml —是把佈局裏的控件寫入


public.xml —是把控件id指定(type id是0x010200開頭)


id如果不是0x010200就編譯報錯 如下:
error: trying to add resource ‘id/vs_et_input’ with ID 0x0104001c but type ‘id’ already has ID 2
看這個錯誤提示 ID 2 適用於id type,不能再把id type指定到 ID 4(string type的引用)

java代碼調用

// An highlighted block
private void checkResult() {
       Random random = new Random(System.currentTimeMillis());
       View inputView = LayoutInflater.from(ActivityThread.currentActivityThread().getSystemUiContext()).inflate(com.android.internal.R.xml.layout_dialog_report, null);
       final TextView tv = inputView.findViewById(com.android.internal.R.id.vs_tv_number);
       final EditText et = inputView.findViewById(com.android.internal.R.id.vs_et_input);
       final int x = random.nextInt(100);
       final int y = random.nextInt(100);
       final int operationalRules = random.nextInt(4);
       switch (operationalRules) {
           case 0:
               tv.setText(x + " + " + y + " = ?");
               break;
           case 1:
               tv.setText(x + " - " + y + " = ?");
               break;
           case 2:
               tv.setText(x + " * " + y + " = ?");
               break;

           case 3:
               tv.setText(x + " / " + y + " = ?");
               break;

       }

       AlertDialog.Builder n = new AlertDialog.Builder(ActivityThread.currentActivityThread().getSystemUiContext());
       n.setTitle("Bitte berechnen Sie das Ergebnis der folgenden mathematischen Formel zur Überprüfung");
       n.setView(inputView); // 設置顯示的view
       n.setPositiveButton("OK", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int which) {
               String str = et.getText().toString();

               if(str == null){
                   tv.setText("Falsche Eingabe");
                   try {
                       Thread.sleep(3000);
                   }catch (Exception e){
                       e.printStackTrace();
                   }
                   dialog.dismiss();
                   return;
               }
               String regEx="[^0-9]";
               Pattern p = Pattern.compile(regEx);
               Matcher m = p.matcher(str);
               String num =  m.replaceAll("").trim();
               int i = Integer.parseInt(num);
               int result = 0;
               switch (operationalRules) {
                   case 0:
                       result = x + y;
                       break;
                   case 1:
                       result = x - y;
                       break;

                   case 2:
                       result = x * y;
                       break;
                   case 3:
                       if (y == 0) {
                           result = x * y;
                           break;
                       }
                       result = x / y;
                       break;

               }
               android.util.Log.i("alan_vs","i="+i+"result ="+result);
               if (i == result) {
                   jiachang = false;
                   android.util.Log.i("alan_vs","checkResult 1"+jiachang);
                   dialog.dismiss();
               } else {
                   jiachang = true;
               }
           }
       });

       n.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int which) {
               dialog.dismiss();
               jiachang = true;
           }
       });
       AlertDialog dialog = n.create();
   	dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
       dialog.show();
   }

調用和編譯註意

在PhonewindowManager裏面調用記得用Handler, Unable to add window – token null is not valid; is your activity running?–需要dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);解決,佈局文件新增修改的源碼需要更新api,因此需要make update-api編譯api更新

效果圖

以上源碼新增佈局文件以及調用顯示的效果圖

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章