|
1 | 1 | package com.google.firebase.example.crashlytics; |
2 | 2 |
|
3 | 3 | import android.os.Bundle; |
4 | | -import androidx.appcompat.app.AppCompatActivity; |
5 | | -import android.util.Log; |
6 | 4 | import android.view.View; |
7 | 5 | import android.view.ViewGroup; |
8 | 6 | import android.widget.Button; |
9 | | - |
| 7 | +import androidx.appcompat.app.AppCompatActivity; |
10 | 8 | import com.google.firebase.crashlytics.FirebaseCrashlytics; |
11 | 9 |
|
12 | 10 | public class MainActivity extends AppCompatActivity { |
13 | 11 |
|
14 | | - @Override |
15 | | - protected void onCreate(Bundle savedInstanceState) { |
16 | | - super.onCreate(savedInstanceState); |
17 | | - } |
18 | | - |
19 | | - public void setKeysBasic() { |
20 | | - // [START crash_set_keys_basic] |
21 | | - FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance(); |
22 | | - |
23 | | - crashlytics.setCustomKey("my_string_key", "foo" /* string value */); |
24 | | - |
25 | | - crashlytics.setCustomKey("my_bool_key", true /* boolean value */); |
26 | | - |
27 | | - crashlytics.setCustomKey("my_double_key", 1.0 /* double value */); |
28 | | - |
29 | | - crashlytics.setCustomKey("my_float_key", 1.0f /* float value */); |
30 | | - |
31 | | - crashlytics.setCustomKey("my_int_key", 1 /* int value */); |
32 | | - // [END crash_set_keys_basic] |
33 | | - } |
34 | | - |
35 | | - public void resetKey() { |
36 | | - // [START crash_re_set_key] |
37 | | - FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance(); |
38 | | - |
39 | | - crashlytics.setCustomKey("current_level", 3); |
40 | | - crashlytics.setCustomKey("last_UI_action", "logged_in"); |
41 | | - // [END crash_re_set_key] |
42 | | - } |
43 | | - |
44 | | - public void logReportAndPrint() { |
45 | | - // [START crash_log_report_and_print] |
46 | | - FirebaseCrashlytics.getInstance().log("message"); |
47 | | - // [END crash_log_report_and_print] |
48 | | - } |
49 | | - |
50 | | - public void logReportOnly() { |
51 | | - // [START crash_log_report_only] |
52 | | - FirebaseCrashlytics.getInstance().log("message"); |
53 | | - // [END crash_log_report_only] |
54 | | - } |
55 | | - |
56 | | - public void enableAtRuntime() { |
57 | | - // [START crash_enable_at_runtime] |
58 | | - FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(true); |
59 | | - // [END crash_enable_at_runtime] |
60 | | - } |
61 | | - |
62 | | - public void setUserId() { |
63 | | - // [START crash_set_user_id] |
64 | | - FirebaseCrashlytics.getInstance().setUserId("user123456789"); |
65 | | - // [END crash_set_user_id] |
| 12 | + @Override |
| 13 | + protected void onCreate(Bundle savedInstanceState) { |
| 14 | + super.onCreate(savedInstanceState); |
| 15 | + } |
| 16 | + |
| 17 | + public void setKeysBasic() { |
| 18 | + // [START crash_set_keys_basic] |
| 19 | + FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance(); |
| 20 | + |
| 21 | + crashlytics.setCustomKey("my_string_key", "foo" /* string value */); |
| 22 | + |
| 23 | + crashlytics.setCustomKey("my_bool_key", true /* boolean value */); |
| 24 | + |
| 25 | + crashlytics.setCustomKey("my_double_key", 1.0 /* double value */); |
| 26 | + |
| 27 | + crashlytics.setCustomKey("my_float_key", 1.0f /* float value */); |
| 28 | + |
| 29 | + crashlytics.setCustomKey("my_int_key", 1 /* int value */); |
| 30 | + // [END crash_set_keys_basic] |
| 31 | + } |
| 32 | + |
| 33 | + public void resetKey() { |
| 34 | + // [START crash_re_set_key] |
| 35 | + FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance(); |
| 36 | + |
| 37 | + crashlytics.setCustomKey("current_level", 3); |
| 38 | + crashlytics.setCustomKey("last_UI_action", "logged_in"); |
| 39 | + // [END crash_re_set_key] |
| 40 | + } |
| 41 | + |
| 42 | + public void logReportAndPrint() { |
| 43 | + // [START crash_log_report_and_print] |
| 44 | + FirebaseCrashlytics.getInstance().log("message"); |
| 45 | + // [END crash_log_report_and_print] |
| 46 | + } |
| 47 | + |
| 48 | + public void logReportOnly() { |
| 49 | + // [START crash_log_report_only] |
| 50 | + FirebaseCrashlytics.getInstance().log("message"); |
| 51 | + // [END crash_log_report_only] |
| 52 | + } |
| 53 | + |
| 54 | + public void enableAtRuntime() { |
| 55 | + // [START crash_enable_at_runtime] |
| 56 | + FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(true); |
| 57 | + // [END crash_enable_at_runtime] |
| 58 | + } |
| 59 | + |
| 60 | + public void setUserId() { |
| 61 | + // [START crash_set_user_id] |
| 62 | + FirebaseCrashlytics.getInstance().setUserId("user123456789"); |
| 63 | + // [END crash_set_user_id] |
| 64 | + } |
| 65 | + |
| 66 | + public void methodThatThrows() throws Exception { |
| 67 | + throw new Exception(); |
| 68 | + } |
| 69 | + |
| 70 | + public void logCaughtEx() { |
| 71 | + // [START crash_log_caught_ex] |
| 72 | + try { |
| 73 | + methodThatThrows(); |
| 74 | + } catch (Exception e) { |
| 75 | + FirebaseCrashlytics.getInstance().recordException(e); |
| 76 | + // handle your exception here |
66 | 77 | } |
67 | | - |
68 | | - public void methodThatThrows() throws Exception { |
69 | | - throw new Exception(); |
70 | | - } |
71 | | - |
72 | | - public void logCaughtEx() { |
73 | | - // [START crash_log_caught_ex] |
74 | | - try { |
75 | | - methodThatThrows(); |
76 | | - } catch (Exception e) { |
77 | | - FirebaseCrashlytics.getInstance().recordException(e); |
78 | | - // handle your exception here |
79 | | - } |
80 | | - // [END crash_log_caught_ex] |
| 78 | + // [END crash_log_caught_ex] |
| 79 | + } |
| 80 | + |
| 81 | + public void logCaughtExWithCustomKeys() { |
| 82 | + // [START crash_log_caught_ex_custom_keys] |
| 83 | + try { |
| 84 | + methodThatThrows(); |
| 85 | + } catch (Exception e) { |
| 86 | + FirebaseCrashlytics.getInstance() |
| 87 | + .recordException(e, Map.of("key_1", "value_1", "key_2", "value_2")); |
| 88 | + // handle your exception here |
81 | 89 | } |
82 | | - |
83 | | - public void logCaughtExWithCustomKeys() { |
84 | | - // [START crash_log_caught_ex_custom_keys] |
85 | | - try { |
86 | | - methodThatThrows(); |
87 | | - } catch (Exception e) { |
88 | | - FirebaseCrashlytics.getInstance() |
89 | | - .recordException(e, Map.of("key_1", "value_1", "key_2", "value_2")); |
90 | | - // handle your exception here |
91 | | - } |
92 | | - // [END crash_log_caught_ex_custom_keys] |
93 | | - } |
94 | | - |
95 | | - public void forceACrash() { |
96 | | - // [START crash_force_crash] |
97 | | - Button crashButton = new Button(this); |
98 | | - crashButton.setText("Crash!"); |
99 | | - crashButton.setOnClickListener(new View.OnClickListener() { |
100 | | - public void onClick(View view) { |
101 | | - throw new RuntimeException(); // Force a crash |
102 | | - } |
| 90 | + // [END crash_log_caught_ex_custom_keys] |
| 91 | + } |
| 92 | + |
| 93 | + public void forceACrash() { |
| 94 | + // [START crash_force_crash] |
| 95 | + Button crashButton = new Button(this); |
| 96 | + crashButton.setText("Crash!"); |
| 97 | + crashButton.setOnClickListener( |
| 98 | + new View.OnClickListener() { |
| 99 | + public void onClick(View view) { |
| 100 | + throw new RuntimeException(); // Force a crash |
| 101 | + } |
103 | 102 | }); |
104 | 103 |
|
105 | | - addContentView(crashButton, new ViewGroup.LayoutParams( |
106 | | - ViewGroup.LayoutParams.MATCH_PARENT, |
107 | | - ViewGroup.LayoutParams.WRAP_CONTENT)); |
108 | | - // [END crash_force_crash] |
109 | | - } |
| 104 | + addContentView( |
| 105 | + crashButton, |
| 106 | + new ViewGroup.LayoutParams( |
| 107 | + ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); |
| 108 | + // [END crash_force_crash] |
| 109 | + } |
110 | 110 | } |
0 commit comments