2017-03-19 1 views
0

他のアンドロイドアプリからテキストを受け取り、ブラウザを開くという単純なアプリを開発しようとしています。一度だけ 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); 

    } 

} 

答えて

0

openBrowser方法は、アプリがすでに作成されている(あなたはバックボタンを押していませんでした場合)あなたがアプリを開くことがonCreate方法、二度目にあるhandleSendText方法の魔女から呼び出されます!コードは決して実行されません。

あなたはあなたのコードを編集してonResume法上openBrowserメソッドを呼び出すか、単にボタンをOncliking方法openBrowserを呼び出すためのボタンを行うことができる

Android Activity life cycle

の下にアンドロイド活動のライフサイクルをご確認ください。

関連する問題