您好我正在尝试通过蓝牙发送文件,但我有Android 2.X的问题(注意它适用于Android 4.X)。这是我的代码:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.setComponent(new ComponentName("com.android.bluetooth",
"com.android.bluetooth.opp.BluetoothOppLauncherActivity"));
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(sharingIntent);
当它到达startActivity(sharingIntent)行时,它会引发异常:
12-21 17:09:43.379: W/System.err(29452): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.bluetooth/com.android.bluetooth.opp.BluetoothOppLauncherActivity}; have you declared this activity in your AndroidManifest.xml?
12-21 17:09:43.379: W/System.err(29452): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
12-21 17:09:43.379: W/System.err(29452): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
12-21 17:09:43.379: W/System.err(29452): at android.app.Activity.startActivityForResult(Activity.java:2827)
12-21 17:09:43.379: W/System.err(29452): at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:833)
12-21 17:09:43.379: W/System.err(29452): at android.support.v4.app.Fragment.startActivity(Fragment.java:856)
...
在清单中声明活动不起作用。
我该怎么办?
答案 0 :(得分:0)
我遇到了这个问题但不是每个设备都有。您可以通过检测操作系统版本来使用解决方法,如果它低于11,则使用setPackage()方法。还可以使用Intent选择器来防止崩溃。
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
if(Build.VERSION.SDK_INT < 11){
sharingIntent.setPackage("com.android.bluetooth");
}else{
sharingIntent.setComponent(new ComponentName("com.android.bluetooth",
"com.android.bluetooth.opp.BluetoothOppLauncherActivity"));
}
startActivity(Intent.createChooser(sharingIntent, "Share"));
希望它有所帮助!