是否有使用 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
最佳答案
所有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+的解决方案:
在这里阅读:
How to use Intent.ACTION_APP_ERROR as a means for a "feedback" framework in 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.如果用户未安装应用程序,则将反馈发送到电子邮件
@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[] { "[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;
}