Skip to content

Instantly share code, notes, and snippets.

@jaredsburrows
Created February 11, 2014 19:25
Show Gist options
  • Save jaredsburrows/8942210 to your computer and use it in GitHub Desktop.
Save jaredsburrows/8942210 to your computer and use it in GitHub Desktop.
Send Feedback
import java.io.PrintWriter;
import java.io.StringWriter;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ApplicationErrorReport;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
// GoogleFeedbackUtils.bindFeedback(this);
sendFeedback();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private void sendFeedback() {
try {
int i = 3 / 0;
} catch (Exception e) {
ApplicationErrorReport report = new ApplicationErrorReport();
report.packageName = report.processName = getApplication().getPackageName();
report.time = System.currentTimeMillis();
report.type = ApplicationErrorReport.TYPE_ANR;
report.systemApp = false;
ApplicationErrorReport.CrashInfo crash = new ApplicationErrorReport.CrashInfo();
crash.exceptionClassName = e.getClass().getSimpleName();
crash.exceptionMessage = e.getMessage();
StringWriter writer = new StringWriter();
PrintWriter printer = new PrintWriter(writer);
e.printStackTrace(printer);
crash.stackTrace = writer.toString();
StackTraceElement stack = e.getStackTrace()[0];
crash.throwClassName = stack.getClassName();
crash.throwFileName = stack.getFileName();
crash.throwLineNumber = stack.getLineNumber();
crash.throwMethodName = stack.getMethodName();
report.crashInfo = crash;
try
{
if (applicationExist("com.google.android.feedback"))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.google.android.feedback", "com.google.android.feedback.FeedbackActivity");
intent.putExtra(Intent.EXTRA_BUG_REPORT, report);
startActivity(intent);
}
else
{
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" });
intent.putExtra(Intent.EXTRA_SUBJECT, getApplicationContext().getApplicationInfo().loadLabel(getApplicationContext().getPackageManager()).toString()+"("+getPackageManager().getPackageInfo(getApplicationInfo().packageName, 0).versionName+")"+" Contact Form | Device: "+Build.MANUFACTURER+" "+Build.DEVICE+"("+Build.MODEL+") API: "+Build.VERSION.SDK_INT);
intent.setType("plain/html");
startActivity(intent);
}
} catch (Exception e2) { }
}
}
private boolean applicationExist(String uri)
{
PackageManager pm = this.getPackageManager();
boolean exists = false;
try
{
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
exists = true;
}
catch (Exception e) { }
return exists;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment