Android修改分析:自定义TP测试回调问题
问题
工厂测试模式,把触屏测试项测试完,并成功;返回主界面,还是灰色未检测标志。
分析
1. 初步分析为TP测试Activity未发送验证成功信号给主界面,后通过log发现Activity发送了信号。
2. 再次分析是工厂测试主界面未接收到Activity,验证发现主界面确实未接收到Activity发送的信号。
分析未接收到信号的原因:
packages/apps/WFactoryTest/src/com/waterworld/factorytest/ext/GridItemFragment.java
if( status == Utils.NONE ) {
String action = bean.getAction();
Intent intent = new Intent();
intent.setAction(action);
Log.d(TAG, "handleMessage: start->"+action);
startActivityForResult(intent, Utils.REQUEST_CODE_CIRCLE);
break;
}
public void onActivityResult( int requestCode, int resultCode, Intent data ){
Log.d(TAG, "onActivityResult: requestCode"+requestCode+ " resultCode "+requestCode +" "+data);
if( data != null ){
try {
if (requestCode == Utils.REQUEST_CODE_CIRCLE) {
String factory = data.getStringExtra(Utils.NAME);
int status = data.getIntExtra(Utils.TEST_RESULT, Utils.NONE);
FactoryDatas.updateBeanStatus(factory, status);
// Sleep();
Message message = Message.obtain();
message.what = Utils.START;
handler.sendMessage(message);
} else if (requestCode == Utils.REQUEST_CODE) {
String factory = data.getStringExtra(Utils.NAME);
int status = data.getIntExtra(Utils.TEST_RESULT, Utils.NONE);
FactoryDatas.updateBeanStatus(factory, status);
}
}catch(Exception e){
Log.d(TAG, "onActivityResult: ", e.fillInStackTrace());
e.printStackTrace();
}
}
}
packages/apps/WFactoryTest/AndroidManifest.xml
<activity
android:name=".activitys.TouchTest"
android:exported="false"
android:label="@string/title_activity_tp_test"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />+
<action android:name="com.waterworld.intent.action.TOUCH_TEST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".activitys.TouchTest2"
android:exported="false"
android:label="@string/title_activity_tp_test"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="com.waterworld.intent.action.TOUCH_TEST2" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
packages/apps/WFactoryTest/res/xml/factory.xml
<Item
name="yax_touch"
action="com.waterworld.intent.action.TOUCH_TEST"
title="@string/title_activity_tp_test"></Item>
packages/apps/WFactoryTest/src/com/waterworld/factorytest/activitys/TouchTest.java
public Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
startActivity(new Intent(TouchTest.this,TouchTest2.class));
finish();
}
packages/apps/WFactoryTest/src/com/waterworld/factorytest/activitys/TouchTest2.java
public Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
setResultBeforeFinish(Utils.SUCCESS);
finish();
}
public void setResultBeforeFinish(int status) {
Log.d(TAG, "setResultBeforeFinish: " + status);
Intent intent = new Intent();
intent.putExtra(Utils.NAME, "yax_touch");
intent.putExtra(Utils.TEST_RESULT, status);
setResult(status, intent);
}
结果
分析发现是主界面的startActivityForResult
没有获得第一个TPActivity的响应,而第二个TPActivity的setResult
没有返回给第一个TPActibity。
解决方案:
packages/apps/WFactoryTest/src/com/waterworld/factorytest/activitys/TouchTest.java
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
startActivity(new Intent(TouchTest.this,TouchTest2.class));
finish();
Intent intent = new Intent(TouchTest.this,TouchTest2.class);
startActivityForResult(intent, 1);
//finish();
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
if(resultCode == Utils.SUCCESS) {
String ResultData = data.getStringExtra(Utils.NAME);
int status = data.getIntExtra(Utils.TEST_RESULT, Utils.NONE);
setResultBeforeFinish(ResultData,status);
finish();
} else if (resultCode == Utils.FAILED) {
String ResultData = data.getStringExtra(Utils.NAME);
int status = data.getIntExtra(Utils.TEST_RESULT, Utils.NONE);
setResultBeforeFinish(ResultData,status);
finish();
}
break;
default:
}
}
public void setResultBeforeFinish(String name, int status) {
Intent intent = new Intent();
intent.putExtra(Utils.NAME, name);
intent.putExtra(Utils.TEST_RESULT, status);
setResult(status, intent);
}