Dmitry Frank
Dmitry Frank

Reputation: 10767

Android app design: write correct intent filter

I'm trying to write correct intent filter.

I'm basically familiar with intents, they work fine in my app, but i want to know the better way to do it. Here's what i need to do:

I have an application that owns some data. (data from ECU, engine control unit) There's two Activities: one Activity can display data as a digits, and another can display it as a graphs.

Let's see on the first activity.

  1. What does it do? It displays.
  2. What does it display? ECU data.
  3. How does it display this data? as a digits.

The second activity is almost the same, but third item is different: it displays data as a graphs.

So, I want to be able to send intent that specifies exatly all info, just like "Hey Android, I need to display ECU data as a digits", and then first activity should be opened.

And I also want to be able to send intent that specifies only first two items, just like "Hey Android, I need to display ECU data", and then android should ask user what activity should be opened.

My app also should be able to respond to other apps.

What's the better way to achieve all this?

UPDATE: Here's my xml. What should i specify in <intent-filter>?

  <activity
     android:name=".MyActivity"
     >

     <intent-filter >
        <!-- what should i specify here? -->
     </intent-filter>
  </activity>

UPDATE 2: Here's what i'm trying to do: I have an application with Service that communicates via bluetooth with remote device and receives ECU data. There's some simple Activities that can display this data: as I already said, one can display digits, another can display graphs. This application provides AIDL interface for any another application to be able to get ECU data too.

As I said, these Activities is quite simple, they provides just basic user interface.

Say, one man wants this data to be displayed like moveable-pointer indicator. No problem: he can write his own Activity that will bind my Service, get data from it and display just how he wants.

Then, if i send Intent like "Hey Android, I need to display ECU data", then Android should ask me what Activity should be opened, and I want to be able to see this new Activity too.

Upvotes: 5

Views: 790

Answers (2)

Dmitry Frank
Dmitry Frank

Reputation: 10767

Well, i found myself what i looked for.

in AndroidManifest.xml:

<activity
   android:name=".MyActivityForDigits"
   >

   <intent-filter >
      <action android:name="android.intent.action.VIEW" />
      <data android:mimeType="ru.orionspb.bk100_iface_android/car_data" />
      <category android:name="android.intent.category.DEFAULT" />
      <category android:name="ru.orionspb.bk100_iface_android.category.DIGITS" />
   </intent-filter>
</activity>

<activity
   android:name=".MyActivityForGraphs"
   >

   <intent-filter >
      <action android:name="android.intent.action.VIEW" />
      <data android:mimeType="ru.orionspb.bk100_iface_android/car_data" />
      <category android:name="android.intent.category.DEFAULT" />
      <category android:name="ru.orionspb.bk100_iface_android.category.GRAPHS" />
   </intent-filter>
</activity>

And then i can say "Hey Android, I need to display car data" just like that:

     Intent mIntent = new Intent();
     mIntent.setAction("ru.orionspb.bk100_iface_android.action.VIEW");
     mIntent.setType("ru.orionspb.bk100_iface_android/car_data");
     mIntent.addCategory(android.content.Intent.CATEGORY_DEFAULT);

     startActivity(mIntent);

If i want to say "Hey Android, I need to display car data as a digits", i need to replace CATEGORY_DEFAULT with my own category:

     Intent mIntent = new Intent();
     mIntent.setAction("ru.orionspb.bk100_iface_android.action.VIEW");
     mIntent.setType("ru.orionspb.bk100_iface_android/car_data");
     mIntent.addCategory("ru.orionspb.bk100_iface_android.category.DIGITS");

     startActivity(mIntent);

And that's it. Of course, i can replace DIGITS with GRAPHS to open MyActivityForGraphs instead of MyActivityForDigits. It works just like i want it to work.

Upvotes: 1

TJ Biddle
TJ Biddle

Reputation: 6494

If you're passing information between activities within your own application; it's better to use Bundles (Or you could use SharedPreferences, or read/write to memory but I find bundles easier)

First activity:

Intent i = new Intent().newClass(first.this, second.class);
i.putExtra("keynamehere", value);
startActivity(i);

Second activity:
Bundle b = getIntent().getExtras();
b.getString/getInt/getByte/etc("keynamehere");

If you want to work with custom Intent's - check this question out: Filter Intent based on custom data

I haven't had a chance to play around with it yet, but I think that should help you out - from the looks of it, it seems that they have a 'sender and receiver' like you are trying to do.

Upvotes: 2

Related Questions