1. 程式人生 > 實用技巧 >從一個java小程式看java的初始化流程

從一個java小程式看java的初始化流程

對“直接通過藍芽來獲取連線手機的簡訊資訊”這個需求做了一些技術調研,如下是調研過程中的一些記錄(持續更新中)。

1、無法得到BluetoothMasClient類

在文章https://blog.csdn.net/u012439416/article/details/54349812(藍芽map協議原始碼解析)這篇文章中有提到通過MAP方式來實現藍芽讀取簡訊,具體的實現方式為:

 1  //註冊廣播
 2  IntentFilter filter = new IntentFilter();  
 3  filter.addAction("android.bluetooth.device.action.SDP_RECORD");
4 registerReceiver(mReceiver, filter); 5 //監聽廣播 6 private final BroadcastReceiver mReceivers = new BroadcastReceiver() { 7 @Override 8 public void onReceive(Context context, Intent intent) { 9 String action = intent.getAction(); 10 if (action.equals(BluetoothDevice.ACTION_SDP_RECORD)) {
11 BluetoothDevice dev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 12 ParcelUuid uuid = intent.getParcelableExtra(BluetoothDevice.EXTRA_UUID); 13 if (uuid.equals(BluetoothUuid.MAS)) { 14 SdpMasRecord masrec = 15 intent.getParcelableExtra(BluetoothDevice.EXTRA_SDP_RECORD);
16 BluetoothMasClient mapclient = new BluetoothMasClient(mDevice, masrec, 17 mMapHandler); 18 } 19 } 20 } 21 };

這裡面有些類和屬性,在API-27及以上已經被加了@hide標籤,當然這一點供應商提供的提供可以解決。在除錯過程中,上述廣播也能監聽到。但第16行的紅色字型部分有個比較關鍵的類BluetoothMasClient,獲取簡訊資訊的方法都需要通過該類例項來完成,在API-27(Android8.1)及以上系統都無法獲取到。

(1)API-27上無法直接引用BluetoothMasClient類

在系統原始碼frameworks\opt\bluetooth\src\android\bluetooth\client\map下可以找到該類,引用路徑為"android.bluetooth.client.map.BluetoothMasClient",使用時發現無法引用這個類。

(2)在API-27(Android8.1)無法通過反射獲取到BluetoothMasClient類

通過反射的方式,也無法獲取到該類。測試程式碼:

 1 public void getBluetoothMasClient() {
 2     Log.d(TAG, "testGetHide");
 3     try {
 4         Class<?> c = Class.forName("android.bluetooth.client.map.BluetoothMasClient");
 5         Log.d(TAG, "name=" + c.getName());
 6     } catch (ClassNotFoundException e) {
 7         Log.d(TAG, "e=" + e.getMessage());
 8         e.printStackTrace();
 9     }
10 }

執行結果:

 1  W/System.err: java.lang.ClassNotFoundException: android.bluetooth.client.map.BluetoothMasClient
 2  W/System.err:     at java.lang.Class.classForName(Native Method)
 3  W/System.err:     at java.lang.Class.forName(Class.java:453)
 4  W/System.err:     at java.lang.Class.forName(Class.java:378)
 5  W/System.err:     at com.example.demo.sms.BTActivity.testGetHide(BTActivity.java:489)
 6  W/System.err:     at com.example.demo.sms.BTActivity.onCreate(BTActivity.java:73)
 7  W/System.err:     at android.app.Activity.performCreate(Activity.java:7050)
 8  W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
 9  W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2809)
10  W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2931)
11  W/System.err:     at android.app.ActivityThread.-wrap11(Unknown Source:0)
12  W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1620)
13  W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:105)
14  W/System.err:     at android.os.Looper.loop(Looper.java:176)
15  W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6701)
16  W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
17  W/System.err:     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:249)
18  W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)

(3)API-28(Android9.0)上該類已經不存在了

在AS中搜索該類,以及在系統原始碼中按照之前的路徑,都無法再找到該類。