獲取呼叫自己方法的使用者的包名和類名
阿新 • • 發佈:2018-12-20
此功能可以獲得第三方的對自己功能的使用情況,可以知道非法操作!
//下面程式碼段是獲取呼叫自己方法的人的包名和類名
1.比如一個叫MainActivity內部呼叫了這個初始化
MainActivity中有呼叫:ABC.getInstance();
結果:name:com.xx.MainActivity
2.比如從MainActivity進入了DemoActivity
DemoActivity中有呼叫:ABC.getInstance();
結果:name:com.xx.DemoActivity
文章出處:https://blog.csdn.net/pangzaifei/article/details/70213731
public static ABC getInstance(Context context) { if (mAbc == null) { synchronized (ABC.class) {//同步程式碼塊 if (mAbc == null) { mAbc = new ABC(context); } } } PackageManager packageManager = context.getPackageManager(); try { PackageInfo info = packageManager.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES); ActivityInfo[] activities = info.activities; if (activities.length > 0) { for (int i = 0; i < activities.length - 1; i++) { String name = activities[i].name; LogHelper.e("fffpzf", "name:" + name); } } } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); LogHelper.e("fffpzf", "name 異常"); } return mAbc; }