0

I am attempting to get one activity to launh another via an intent and intent filter. Here is what I have.

In the launching activity:

 Intent i = new Intent();
 i.setAction("com.test.apps.CATAPP");
 i.addCategory("com.test.apps");
 this.startActivityForResult(i,APP_REQUEST_CODE);

The intent filter in the receiving app:

 <intent-filter>
   <action android:name="com.test.apps.CATAPP" />
   <category android:name="android.intent.category.DEFAULT" />
   <category android:name="com.test.apps" />
   <data android:mimeType="text/plain"/>
</intent-filter>

This fails to work. However, if I take the example from Wei Meng Lee's Android 4 Development, it works and then only if the URI and the Intent.ACTION_VIEW are present.

Code in the launching app:

   Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
   i.setAction("com.test.apps.CATAPP");
   i.addCategory("com.test.apps");
   this.startActivityForResult(i,APP_REQUEST_CODE);

Intend in the receiving app:

 <intent-filter>
      <action android:name="android.intent.action.VIEW" />
       <action android:name="com.test.apps.CATAPP" />
       <category android:name="android.intent.category.DEFAULT" />
      <category android:name="com.test.apps" />
      <data android:scheme="http"/>

 </intent-filter>

Where am I going wrong? Have I made a simple mistake?

I have spend the last couple of hours trawling stack overflow, for anwer to this but only found similar questions, with answers that did not solve my problem or were not applicable.

Andrew S
  • 2,847
  • 3
  • 33
  • 50
  • try it without the default category once – Droidekas Feb 20 '15 at 08:24
  • @Droidekas. Everything I've read both in my text book, on the android docs and on stack overflow indicate the necessity of the default category and not it does not work. – Andrew S Feb 20 '15 at 08:46
  • read [this](http://stackoverflow.com/questions/5727828/what-is-the-purpose-of-android-intent-category-default) and `android:exported="true"`? also add the request code your entire activity entry from the manifest.Since this much data does not provide the error,maybe more context would help – Droidekas Feb 20 '15 at 08:49
  • Frankly I am confused why do you want to specify an explicit cateogry intent and then add `category.DEFAULT` – Droidekas Feb 20 '15 at 09:01
  • @Droidekas because it is a requirement of how this process works. From Google "Note: In order to receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter. The methods startActivity() and startActivityForResult() treat all intents as if they declared the CATEGORY_DEFAULT category. If you do not declare it in your intent filter, no implicit intents will resolve to your activity." – Andrew S Feb 20 '15 at 09:23

1 Answers1

1

In your first example, your Intent is not going to match the IntentFilter of the Activity. You've specified a data element in the IntentFilter, but you've not set one on the Intent. You can either set the MIME type on the Intent:

i.setType("text/plain");

Or remove the <data> element from the <intent-filter> in the manifest, depending on your requirements.

If you just want to pass some simple text, set the Intent's type as shown above, and attach a String extra to it.

i.putExtra(Intent.EXTRA_TEXT, "Plain text.");

Then, to retrieve the text in the next Activity:

String plainText = getIntent().getStringExtra(Intent.EXTRA_TEXT);

if(plainText != null)
...

Note that the Intent.EXTRA_TEXT constant is merely a convenience. The extra key can be anything you want, as long as it's the same in both places.

Mike M.
  • 38,532
  • 8
  • 99
  • 95
  • Hmm, yep this seems to be the problem thank you. Just another question, if I wanted to send some text to the receiving activity and not a URL. What would I write? – Andrew S Feb 20 '15 at 09:56