Created
July 24, 2013 15:34
-
-
Save joshdholtz/6071686 to your computer and use it in GitHub Desktop.
Using Sentry-Android with a CaptureListener to show if using wifi - Screenshot of success http://i.imgur.com/bZ9qfXA.png
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.joshdholtz.sentry" | |
android:versionCode="1" | |
android:versionName="1.0" > | |
<uses-sdk | |
android:minSdkVersion="8" | |
android:targetSdkVersion="8" /> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<activity | |
android:name="com.joshdholtz.sentry.MainActivity" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.joshdholtz.sentry; | |
import java.util.HashMap; | |
import java.util.Map; | |
import org.json.JSONException; | |
import com.joshdholtz.sentry.Sentry.SentryEventBuilder; | |
import com.joshdholtz.sentry.Sentry.SentryEventBuilder.SentryEventLevel; | |
import com.joshdholtz.sentry.Sentry.SentryEventCaptureListener; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
import android.os.Bundle; | |
import android.app.Activity; | |
import android.view.Menu; | |
public class MainActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// Sentry will look for uncaught exceptions from previous runs and send them | |
Sentry.init(this, "YOUR-DSN"); | |
// Sets a listener to intercept the SentryEventBuilder before | |
// each capture to set values that could change state | |
Sentry.setCaptureListener(new SentryEventCaptureListener() { | |
@Override | |
public SentryEventBuilder beforeCapture(SentryEventBuilder builder) { | |
// Needs permission - <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | |
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); | |
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); | |
// Sets extra key if wifi is connected | |
try { | |
builder.getExtra().put("wifi", String.valueOf(mWifi.isConnected())); | |
} catch (JSONException e) {} | |
return builder; | |
} | |
}); | |
// Capture event | |
Sentry.captureEvent(new Sentry.SentryEventBuilder() | |
.setMessage("Being awesome at stuff") | |
.setCulprit("Josh D Holtz") | |
.setTimestamp(System.currentTimeMillis()) | |
); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.activity_main, menu); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment