인 텐트의 모든 엑스트라 나열 텐트의 모든 엑스트라 (및

디버깅 목적으로 인 텐트의 모든 엑스트라 (및 해당 값)를 나열하고 싶습니다. 이제 열쇠를 얻는 것은 문제가되지 않습니다

Set<String> keys = intent.getExtras().keySet();

그러나 키의 값을 얻는 것은 나에게는 하나입니다. 일부 값은 문자열이고 일부는 부울입니다 … 루프에서 값을 가져오고 (키를 반복하는) 값을 로그 파일에 쓰는 방법은 무엇입니까? 힌트 주셔서 감사합니다!



답변

문서화되지 않은 (타사) 의도에 대한 정보를 얻는 데 사용한 내용은 다음과 같습니다.

Bundle bundle = intent.getExtras();
if (bundle != null) {
    for (String key : bundle.keySet()) {
        Log.e(TAG, key + " : " + (bundle.get(key) != null ? bundle.get(key) : "NULL"));
    }
}

bundle루프 전에 null 인지 확인하십시오 .


답변

이것이 인 텐트의 모든 여분을 덤프하는 유틸리티 메소드를 정의하는 방법입니다.

import java.util.Iterator;
import java.util.Set;
import android.os.Bundle;


public static void dumpIntent(Intent i){

    Bundle bundle = i.getExtras();
    if (bundle != null) {
        Set<String> keys = bundle.keySet();
        Iterator<String> it = keys.iterator();
        Log.e(LOG_TAG,"Dumping Intent start");
        while (it.hasNext()) {
            String key = it.next();
            Log.e(LOG_TAG,"[" + key + "=" + bundle.get(key)+"]");
        }
        Log.e(LOG_TAG,"Dumping Intent end");
    }
}


답변

한 줄의 코드로 할 수 있습니다.

Log.d("intent URI", intent.toUri(0));

다음과 같이 출력됩니다.

“#Intent; action = android.intent.action.MAIN; category = android.intent.category.LAUNCHER; launchFlags = 0x10a00000; component = com.mydomain.myapp / .StartActivity; sourceBounds = 12 % 20870 % 20276 % 201167; l .profile = 0; 끝 “

이 문자열의 끝에 (굵은 글씨로 표시 한 부분) 추가 목록 (이 예제에서는 하나만 추가)을 찾을 수 있습니다.

이것은 toUri 문서 에 따르면 : “URI는 인 텐트의 데이터를 기본 URI로 포함하고 액션, 카테고리, 유형, 플래그, 패키지, 구성 요소 및 기타를 설명하는 추가 조각으로 구성됩니다.”


답변

private TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    tv = new TextView(this);
    tv.setText("Extras: \n\r");

    setContentView(tv);

    StringBuilder str = new StringBuilder();
    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        Set<String> keys = bundle.keySet();
        Iterator<String> it = keys.iterator();
        while (it.hasNext()) {
            String key = it.next();
            str.append(key);
            str.append(":");
            str.append(bundle.get(key));
            str.append("\n\r");
        }
        tv.setText(str.toString());
    }
}


답변

Bundle의 get (String key) 메소드는 Object를 리턴합니다. 가장 좋은 방법은 각 키에서 get (String)을 호출하고 Object에서 toString ()을 사용하여 키 세트를 출력하는 것입니다. 이것은 프리미티브에 가장 효과적이지만 toString ()을 구현하지 않는 객체에 문제가 발생할 수 있습니다.


답변

Bundle extras = getIntent().getExtras();
Set<String> ks = extras.keySet();
Iterator<String> iterator = ks.iterator();
while (iterator.hasNext()) {
    Log.d("KEY", iterator.next());
}


답변

의도의 내용을 로그에 출력하고 쉽게 읽을 수있는 방법을 원했습니다. 그래서 여기에 생각해 냈습니다. LogUtil클래스를 만든 다음 dumpIntent()@Pratik이 만든 메서드를 사용하여 조금 수정했습니다. 모든 모습은 다음과 같습니다.

public class LogUtil {

    private static final String TAG = "IntentDump";

    public static void dumpIntent(Intent i){
        Bundle bundle = i.getExtras();
        if (bundle != null) {
            Set<String> keys = bundle.keySet();

            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("IntentDump \n\r");
            stringBuilder.append("-------------------------------------------------------------\n\r");

            for (String key : keys) {
                stringBuilder.append(key).append("=").append(bundle.get(key)).append("\n\r");
            }

            stringBuilder.append("-------------------------------------------------------------\n\r");
            Log.i(TAG, stringBuilder.toString());
        }
    }
}

이것이 누군가를 돕기를 바랍니다!