他のアンドロイドアプリからテキストを受け取り、ブラウザを開くという単純なアプリを開発しようとしています。一度だけ https://developer.android.com/training/sharing/receive.html他のAndroidアプリからのデータ受信は一度しか動作しません
それは動作しますが、:
私はここのドキュメントで説明したように、それを実装しています。 初めて他のアプリケーションからテキストを共有すると、ブラウザが正しく開かれます。 しかし、2回目は私のアプリだけが開かれますが、ブラウザは開かれません。
この理由は何ですか?
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the intent that started this activity
Intent intent = getIntent();
// Get the action of the intent
String action = intent.getAction();
// Get the type of intent (Text or Image)
String type = intent.getType();
// When Intent's action is 'ACTION+SEND' and Type is not null
if (Intent.ACTION_SEND.equals(action) && type != null) {
// When tyoe is 'text/plain'
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent
}
}
}
private void handleSendText(Intent intent) {
// Get the text from intent
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
openBrowser(sharedText);
}
}
private void openBrowser(String text) {
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://example.com/api.php?text=" + text));
startActivity(browserIntent);
}
}