8

是否有使用com.google.android.feedback.FeedbackActivity的示例,就像它在 Google+ 应用中用于发送任何反馈一样?

我试着用它开始

Intent intent = new Intent();
intent.setClassName("com.google.android.feedback",  "com.google.android.feedback.FeedbackActivity");
startActivity(intent);

但我只得到

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.android.feedback/com.google.android.feedback.FeedbackActivity}: java.lang.NullPointerException
4

1 回答 1

10

所有 API 的解决方案

我已经添加了我所有的研究和相关帖子

我一直在寻找最好的解决方案。请在此处查看开源的 Google“MyTracks”应用程序和 Google 代码:

https://code.google.com/p/mytracks/source/browse/MyTracks/src/com/google/android/apps/mytracks/TrackListActivity.java

看看他们如何处理 API 级别与其 API 适配器类之间的兼容性:

https://code.google.com/p/mytracks/source/browse/MyTracks/src/com/google/android/apps/mytracks#mytracks%2Futil

处理菜单:

基于 API => 14(允许反馈):

menu.findItem(R.id.track_list_feedback)
    .setVisible(ApiAdapterFactory.getApiAdapter().isGoogleFeedbackAvailable());

如果 API 低于 14,这将删除“发送反馈”按钮。

发送反馈:

https://code.google.com/p/mytracks/source/browse/MyTracks/src/com/google/android/apps/mytracks/util/GoogleFeedbackUtils.java

基于 API => 14(发送反馈):

public class GoogleFeedbackUtils {

  private static final String TAG = GoogleFeedbackUtils.class.getSimpleName();

  private GoogleFeedbackUtils() {}

  /**
   * Binds the Google Feedback service.
   * 
   * @param context the context
   */
  public static void bindFeedback(Context context) {
    Intent intent = new Intent(Intent.ACTION_BUG_REPORT);
    intent.setComponent(new ComponentName("com.google.android.gms", "com.google.android.gms.feedback.LegacyBugReportService"));
    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
      public void onServiceConnected(ComponentName name, IBinder service) {
        try {
          service.transact(Binder.FIRST_CALL_TRANSACTION, Parcel.obtain(), null, 0);
        } catch (RemoteException e) {
          Log.e(TAG, "RemoteException", e);
        }
      }

        @Override
      public void onServiceDisconnected(ComponentName name) {}
    };
    // Bind to the service after creating it if necessary
    context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
  }
}

菜单代码:

https://code.google.com/p/mytracks/source/browse/MyTracks/src/com/google/android/apps/mytracks/TrackListActivity.java

来自源代码的片段,基于 API => 14:

 @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    Intent intent;
    switch (item.getItemId()) {
      case R.id.track_list_feedback:
        GoogleFeedbackUtils.bindFeedback(this);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

API 10+ 的解决方案:

在此处阅读: 如何使用 Intent.ACTION_APP_ERROR 作为 Android 中“反馈”框架的一种手段? 在这里: http ://blog.tomtasche.at/2012/10/use-built-in-feedback-mechanism-on.html

@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_CRASH;
    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;

    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);
    }
}

所有 API 的解决方案

底线:将为所有安装了 API 10+ 并安装了应用程序的手机生成应用程序报告,或者可以通过电子邮件发送信息。

1.确保用户已安装应用程序

if (applicationExist("com.google.android.feedback"))

2.如果用户安装了应用程序,直接运行反馈应用程序

intent.setClassName("com.google.android.feedback", "com.google.android.feedback.FeedbackActivity");

3.如果用户没有安装应用程序,发送反馈到Email

@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_NONE;
        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[] { "burrowsapps@gmail.com" });
                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;
}
于 2013-12-30T22:22:11.337 回答