問題起因
今天從工廠接收到一個(gè)BUG是:自己管理的模塊暗碼信息查詢功能失效膝晾。
分析問題
其實(shí)這個(gè)模塊問題做久了感覺也沒什么難度锐墙,只是這是個(gè)新的問題導(dǎo)致我不知道如何去下手词顾。(之前都是改改暗碼拉出來的界面或者添加暗碼等等)
*首先幸乒,分析問題之前我并沒有對(duì)這個(gè)流程有多少了解,只知道是有Dialer雅任,對(duì)暗碼格式判斷然后發(fā)送個(gè)廣播,有暗碼APK接收做處理
*解決完問題之后回想起來感覺還是蠻簡單的咨跌,猶豫要不要寫這種博客沪么。
解決問題
*抓LOG
SecretCode.java
public class ....SecretCode extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
...
if (intent.getAction().equals(SECRET_CODE_ACTION)) {
android.util.Log.d("InfoService", "type = " + type);
if (type != null) {
Intent i = new Intent(InfoService.ACTION_BROADCAST);
i.putExtra("edit_input", type);
context.sendBroadcast(i);
return;
}
}
}
}
}
}
InfoService.java
public class InfoService extends Service{
private static final String TAG = "InfoService";
public static final String ACTION_BROADCAST = "com.---.InfoService";
public static final String CAT_INTERNEL_VERSION = "*#86436*#";
public static final String CAT_CUSTOM_VERSION = "*#84666*#";
public static final String CAT_DEVICES_VERSION = "*#0661#";
private static final String CAT_MMX_CUSTOM_VERSION = "*#8375#";
private static final String CAT_PK_CUSTOM_VERSION = "*#79#";
private static final String CAT_WIKO_CUSTOM_VERSION = "*#*#563412#*#*";
private static final String CAT_BLU_PCB_VERSION="*#999*#";
private static final String CAT_BLU_DEVICE_VERSION="*#888*#";
private static final String CAT_PK_CUSTOM_VERSION2 = "*#*#79#*#*";
private BroadcastReceiver mReceiver;
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate(){
super.onCreate();
mReceiver = new InfoReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_BROADCAST);
registerReceiver(mReceiver, filter);
Log.i(TAG, "registerReceiver: InfoReceiver");
}
private class InfoReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
Log.i(TAG, "receive:"+action);
if(action.equals(ACTION_BROADCAST)){
dobackRun(context, intent);
}
}
}
dobackRun(context, intent); //暗碼邏輯實(shí)現(xiàn)
public void onDestroy(){
super.onDestroy();
if(mReceiver != null){
unregisterReceiver(mReceiver);
mReceiver = null;
}
}
}
代碼很簡單,由InfoService這個(gè)服務(wù)動(dòng)態(tài)注冊(cè)一個(gè)廣播接受者锌半,然后...SecretCode這個(gè)接收者接收到由Dialer發(fā)送的廣播之后進(jìn)行簡單的邏輯判斷處理(有些省略)繼續(xù)發(fā)送一個(gè)廣播禽车。再有后臺(tái)InfoService接收處理。
看到這刊殉,大概就把該模塊簡單的理清楚了殉摔,那為什么暗碼功能失效呢?
通過抓Log知道记焊,...SecretCode這個(gè)接收者是由靜態(tài)注冊(cè)逸月,這個(gè)接收廣播沒有問題,但是由它發(fā)送的廣播InfoReceiver接收不到遍膜,一直以為是廣播被截獲了碗硬,導(dǎo)致接收不到,在這上面也花了很長時(shí)間瓢颅,無從驗(yàn)證廣播的截獲恩尾。
之后,也沒有從這一角度去分析問題了挽懦,抱著試一試的態(tài)度翰意,我通過下面這兩條adb命令定位到是InfoService這個(gè)服務(wù)沒有啟動(dòng)才導(dǎo)致的動(dòng)態(tài)注冊(cè)的廣播不能接受,從而暗碼功能失效
adb shell am startservice -n com.---.productInfo/com.---.productInfo.InfoService
adb shell am stopservice -n com.---.productInfo/com.---.productInfo.InfoService
BootReceiver.java
public class BootReceiver extends BroadcastReceiver {
private static final String TAG = "---ProductInfoBootReceiver";
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Log.i(TAG, "----BootReceiver---");
context.startService(new Intent(context,InfoService.class));
}
}
}
這個(gè)服務(wù)是由接收到開機(jī)廣播來啟動(dòng)的巾兆,沒有啟動(dòng)當(dāng)然是沒有接收到開機(jī)廣播猎物,通過Log看,很多沒有接收到開機(jī)廣播接收者角塑,因?yàn)槭枪财脚_(tái)的蔫磨,其他的項(xiàng)目沒有這個(gè)問題,而就這個(gè)訂單有暗碼失效圃伶,那么多人改這個(gè)平臺(tái)代碼堤如,鬼知道是哪里改出了問題,就先把問題丟給性能優(yōu)化的人了窒朋。