我正在使用以下代码来分享文字
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "share test");
startActivity(Intent.createChooser(sharingIntent, "Share using"));
我想知道用户选择分享的应用包名称。 我尝试使用 IntentSender 为5.1+设备使用以下代码
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.putExtra(Intent.EXTRA_TEXT, "share test");
sharingIntent.setType("text/plain");
Intent receiver = new Intent(this, BroadcastTest.class);
receiver.putExtra("test", "test");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
Intent chooser = Intent.createChooser(sharingIntent, "Share using", pendingIntent.getIntentSender());
startActivity(chooser);
以下是BroadcastReceiver
public class BroadcastTest extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
for (String key : intent.getExtras().keySet()) {
Log.d(getClass().getSimpleName(), " " + intent.getExtras().get(key));
}
}
}
提到了这个答案https://stackoverflow.com/a/38342788/6053724但是没有调用BroadcastReceiver的onReceive()。 是否有任何我想让它工作的东西?
更新:发现上述代码在5.1(api 22)上效果很好但在6.0(api 23)广播时没有立即收到应用程序进行共享或有时广播丢失。< / p>
答案 0 :(得分:0)