How do I create custom intents?

Put your problem here if it does not fit any of the other categories.

How do I create custom intents?

Postby workmaster2n » Fri Feb 08, 2008 8:16 pm

I'm a little confused by intents in general, so I'll go piece by piece -
Let's say my project is called Zeus
How do I create a new intent - I want to do this so that I can write an intent receiver that listens only for this specific intent. My intent will be based on GPS location, so I was thinking ZEUS_LOCATION_CHANGED as a name for my intent - I don't want to use the general LOCATION_CHANGED because my intent receiver only needs to react to ZEUS_LOCATION_CHANGED intents (I don't want to slow the system having my intent receiver intercept LOCATION_CHANGED intents and then realize I don't handle those) - Do I just declare a string in an xml file? Do I set a constant? What about the namespace (like the LOCATION_CHANGED in android.activity.intent.LOCATION_CHANGED)?

Thanks for the help!!
workmaster2n
Freshman
Freshman
 
Posts: 5
Joined: Fri Feb 08, 2008 8:10 pm
Location: Bouler, Colorado, USA

Top

Postby plusminus » Fri Feb 08, 2008 9:54 pm

Hello workmaster,

I just defined a final String within my project like this :darrow:. Perhaps put it into a Constats.java or similar.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         private static final String ZEUS_LOCATION_CHANGED =
  2.  
  3.                                 new String("my.custom.intent.action.ZEUS_LOCATION_CHANGED");
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


An application taking use of that Intent, needs to know exactly that name.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby workmaster2n » Fri Feb 08, 2008 10:06 pm

Thanks for the fast response!
If I have application A throw the intent with "my.custom.intent.action.ZEUS_LOCATION_CHANGED" as the intent that is thrown and application B has the intent receiver, do I just need to set the intent filter for "my.custom.intent.action.ZEUS_LOCATION_CHANGED" or would I need to do "projectA.my.custom.intent.action.ZEUS_LOCATION_CHANGED" (some form of dot notation)? Do I need to just use the string I define or do I need to indicate that the string is located in another file? I guess a deeper question would be - when I send an intent, am I just sending a string (like "my.custom.intent.action.ZEUS_LOCATION_CHANGED") to the OS or am I sending something more complex?

You are amazing plusminus!
workmaster2n
Freshman
Freshman
 
Posts: 5
Joined: Fri Feb 08, 2008 8:10 pm
Location: Bouler, Colorado, USA

Postby plusminus » Sat Feb 09, 2008 11:38 am

Hello workmaster,

all Intents are filled just with one String [+ optional a ContentURI, package-constraints]:
String android.content.Intent.DIAL_ACTION

public static final String DIAL_ACTION
....
Constant Value: "android.intent.action.DIAL"


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby workmaster2n » Sat Feb 09, 2008 5:43 pm

Thanks! That makes sense (a lot more than the documentation).
workmaster2n
Freshman
Freshman
 
Posts: 5
Joined: Fri Feb 08, 2008 8:10 pm
Location: Bouler, Colorado, USA

Postby blundell » Tue Nov 18, 2008 3:52 pm

Ok so I've got my custom intent

Code: Select all
private static final String SAVE_DATA =
        new String("my.custom.intent.action.SAVE_DATA");


and i've got my broadcast receiver that will save the data when the Intent is fired

Code: Select all
public class SaveIntentReceiver extends BroadcastReceiver {
   
   @Override
   public void onReceive(Context context, final Intent intent) {

      class SQLClass extends ListActivity {
         
         private final String MY_DATABASE_NAME = "myCoolUserDB";
          private final String MY_DATABASE_TABLE = "t_Users";
         
         public void saveData(){
            String where = intent.getExtras().getString("where");
            String time = intent.getExtras().getString("time");
            String date = intent.getExtras().getString("date");
            String extras = intent.getExtras().getString("extras");
      
            SQLiteDatabase myDB = null;         
            try {
               /* Create the Database (no Errors if it already exists) */
               myDB = this.openOrCreateDatabase(MY_DATABASE_NAME, MODE_PRIVATE, null);
               /* Create a Table in the Database. */
               myDB.execSQL("CREATE TABLE IF NOT EXISTS " + MY_DATABASE_TABLE
                     + " (LastName VARCHAR, FirstName VARCHAR," + " Country VARCHAR, Age INT(3));");
               myDB.execSQL("INSERT INTO " + MY_DATABASE_TABLE + " (Where, Time, Date, Extras)"
                     + " VALUES ("+where+"," +time+"," +date+","+extras+");");
            } finally {
               if (myDB != null)
                  myDB.close();
            }
         }
      }
      
      SQLClass mySave = new SQLClass();
      mySave.saveData();
      
   }
}



So i have a button that when pressed starts my custom intent, but how? so far:
Code: Select all

private void onSave(final EditText where, final EditText time, final EditText date, final EditText extras) {
      
      Intent saveIntent = new Intent(this, SaveIntentReceiver.class);
      
      saveIntent.putExtra("where", where.toString());
      saveIntent.putExtra("time", time.toString());
      saveIntent.putExtra("date", date.toString());
      saveIntent.putExtra("extras", extras.toString());
      
      // SEND INTENT HERE
   }


Plx help!
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Postby MrSnowflake » Tue Nov 18, 2008 4:37 pm

User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Custom Intents Query

Postby n.aniruddha » Thu Dec 11, 2008 11:46 pm

Hey.

I am also programming an application that requires the creation of custom intents. In my calling application class I have created the string and intent as you have said.

private static final String SEND_IM = "my.android.custom.intent.action.SEND_IM";
Context con = this;
....
Intent imIntent = new Intent();
imIntent.setAction(SEND_IM);
imIntent.putExtras(bundle);
imIntent.setClassName("cs.columbia.android.sip.simpleclient","cs.columbia.android.sip.simpleclient.SIPclient.class");
con.sendBroadcast(imIntent);

When a "Send" button is clicked I want the above intent to be fired. I have the appropriate onClick() function for that.

The receiving application class implements BroadcastReceiver and the onReceive() function. Also, in the AndroidManifest.xml file of target application, I have included the following:

<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".SIPclient"
android:label="@string/app_name">
<intent-filter>
<action android:name="my.android.custom.intent.action.SIPIM" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
</application>

After the button click in the first application, the target application is not being launched. What do I need to do? Also, do I need to add target application information in the AndroidManifest file of calling application?

Thanks and Regards,
Aniruddha.
n.aniruddha
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Dec 11, 2008 11:31 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 14 guests