Recievers and Intent Filters

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

Recievers and Intent Filters

Postby wizlon » Sat Jan 12, 2008 4:37 pm

Hi everyone. I've been looking round for a decent tutorial on intent filters to open my app(or part of it) based on a chosen file but I can't find a good enough tutorial.

I've basically got a file browser from a tutorial here (thanks +-) and when I open select a text file (.txt) for edit I want to load a app I created that will edit text files, I've tried adding in a reciever class and an intent filter, but I'm getting nowhere.

Here is some of the code I am using.

in my andriod manifest.
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <activity class=".fileDisplayer" android:label="@string/file_displayer">
  2.  
  3.                 <intent-filter>
  4.  
  5.                 <action android:value="android.intent.action.MAIN" />
  6.  
  7.                 <category android:value="android.intent.category.LAUNCHER" />
  8.  
  9.             </intent-filter>
  10.  
  11.         </activity>
  12.  
  13.        
  14.  
  15.         <receiver class=".fileDisplayerReciever">
  16.  
  17.             <intent-filter>
  18.  
  19.                  <action android:value="android.intent.action.EDIT" />
  20.  
  21.                  <category android:value="android.intent.category.DEFAULT" />
  22.  
  23.             </intent-filter>
  24.  
  25.         </receiver>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


in my onlick for the list item
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. int lio = aDirectory.getAbsolutePath().lastIndexOf('.') + 1;
  2.  
  3.                                  String ext = aDirectory.getAbsolutePath().substring(lio);
  4.  
  5.                                  
  6.  
  7.                                  if(ext.equals("txt"))
  8.  
  9.                                  {
  10.  
  11.                                         Intent i = new Intent();
  12.  
  13.                                     i.setClass(c, fileDisplayer.class);
  14.  
  15.                                         startSubActivity(i, 1);
  16.  
  17.                                          
  18.  
  19.                                  }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Any help would be very much appreciated.
wizlon
Junior Developer
Junior Developer
 
Posts: 23
Joined: Mon Jan 07, 2008 2:17 pm

Top

Postby plusminus » Sat Jan 12, 2008 5:42 pm

Hello wizlon,

I have not tested it, but your Intent-"Setup" doesn't look right for your problem. It would probably start your Main-Activity if it was not already started.

You should try sth. like this(you didn't specify that you wanted the EDIT_ACTION, your IntentReceiver was listening to):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         Intent i = new Intent(android.content.Intent.EDIT_ACTION, ContentURI.create(YOURFILEHERE), c, fileDisplayer.class);
  2.         startSubActivity(i, 1337);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

("c" should be (the context of) the active Activity).

Perhaps sth like the following is already enough, but the above should ensure, that no other Activity grabs the Intent before your IntentReceiver gets it.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent i = new Intent(android.content.Intent.EDIT_ACTION);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Let us know if it helped you :)

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 wizlon » Sun Jan 13, 2008 11:56 am

Thanks for the help, actions and intent filters don't really seem that well documented, theres certainly a lack of good examples.

As it happens my problem still exists, when i select and item from my list I get a nullpointerexception, this may be happening because of a problem with my list, I've set up a button to implicitly call my new screen, at that works fine. I'm going to have a bit of a play soon but any ideas would be appreciated.

thanks for the help though.
wizlon
Junior Developer
Junior Developer
 
Posts: 23
Joined: Mon Jan 07, 2008 2:17 pm

Postby plusminus » Sun Jan 13, 2008 12:43 pm

Helllo wizlon,

if you post the piece of code where it is happening, we will probably find the solution :)

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 wizlon » Sun Jan 13, 2008 4:10 pm

hi, sorry about before, I was in a bit of a rush. Here is the code i've got in. It's all very similar to one of your examples where when you press on a item it asks you before you open it.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. protected void onListItemClick(ListView l, View v, int position, long id) {
  2.  
  3.           int selectionRowID = (int) this.getSelectionRowID();
  4.  
  5.           String selectedFileString = this.directoryEntries.get(selectionRowID);
  6.  
  7.           if (selectedFileString.equals(".")) {
  8.  
  9.                // Refresh
  10.  
  11.                   c = l.getContext();
  12.  
  13.                this.browseTo(this.currentDirectory);
  14.  
  15.           } else if(selectedFileString.equals("..")){
  16.  
  17.                this.upOneLevel();
  18.  
  19.           } else {
  20.  
  21.                File clickedFile = null;
  22.  
  23.                switch(this.displayMode){
  24.  
  25.                     case RELATIVE:
  26.  
  27.                          clickedFile = new File(this.currentDirectory.getAbsolutePath()
  28.  
  29.                                                             + this.directoryEntries.get(selectionRowID));
  30.  
  31.                          break;
  32.  
  33.                     case ABSOLUTE:
  34.  
  35.                          clickedFile = new File(this.directoryEntries.get(selectionRowID));
  36.  
  37.                          break;
  38.  
  39.                }
  40.  
  41.                if(clickedFile != null)
  42.  
  43.                {
  44.  
  45.                    this.browseTo(clickedFile);
  46.  
  47.                    
  48.  
  49.                    
  50.  
  51.                    
  52.  
  53.                }
  54.  
  55.                    
  56.  
  57.           }
  58.  
  59.  
  60.  
  61. private void browseTo(final File aDirectory){
  62.  
  63.           if (aDirectory.isDirectory()){
  64.  
  65.                this.currentDirectory = aDirectory;
  66.  
  67.                fill(aDirectory.listFiles());
  68.  
  69.           }else{
  70.  
  71.                OnClickListener okButtonListener = new OnClickListener(){
  72.  
  73.                     // @Override
  74.  
  75.                     public void onClick(DialogInterface arg0, int arg1) {
  76.  
  77.                          //try {
  78.  
  79.                                  
  80.  
  81.                                  int lio = aDirectory.getAbsolutePath().lastIndexOf('.') + 1;
  82.  
  83.                                  String ext = aDirectory.getAbsolutePath().substring(lio);
  84.  
  85.                                  
  86.  
  87.                                  if(ext.equals("txt"))
  88.  
  89.                                  {
  90.  
  91.                                          
  92.  
  93.                                          Intent i = new Intent(android.content.Intent.EDIT_ACTION, ContentURI.create(aDirectory.getAbsolutePath()), c, fileDisplayer.class);
  94.  
  95.                                      startSubActivity(i, 1337);
  96.  
  97.                                          
  98.  
  99.                                        
  100.  
  101.                                  }
  102.  
  103.                                
  104.  
  105.                     }
  106.  
  107.                };
  108.  
  109.                OnClickListener cancelButtonListener = new OnClickListener(){
  110.  
  111.                     // @Override
  112.  
  113.                     public void onClick(DialogInterface arg0, int arg1) {
  114.  
  115.                          // Do nothing
  116.  
  117.                     }
  118.  
  119.                };
  120.  
  121.                AlertDialog.show(this,"Question", "Do you want to open that file?\n"
  122.  
  123.                                         + aDirectory.getName(),
  124.  
  125.                                         "OK", okButtonListener,
  126.  
  127.                                         "Cancel", cancelButtonListener, false, null);
  128.  
  129.           }
  130.  
  131.      }
  132.  
  133.  
  134.  
  135.  
Parsed in 0.049 seconds, using GeSHi 1.0.8.4


Another question. I'm actually filtering the file extension manually there, but how do you "register" a file extension to an intent filter?

Thanks for your help.
wizlon
Junior Developer
Junior Developer
 
Posts: 23
Joined: Mon Jan 07, 2008 2:17 pm

Postby timbobsteve » Mon Sep 29, 2008 9:54 pm

Sorry to resurrect a zombie thread, but I thought it better than starting a brand new one and having people say "use the search feature!" :P

I am trying to achieve the same result as wizion was in his example code. I have the file-browser application and I want it to open files based on their filetype.

Now there are two ways I can think of this being done:

1) Filter the file extension and attempt to open each file in the correct program
Problems:
  • I would have to write filters for every file extension under the sun
  • There is always the chance that a file is really a .zip file but named with .png... which may break the opening application
2) Let Android decide what File type it is and then set the new Intent's MIME-type data to be a certain value:
Problems:
  • Will I have to register 100+ different MIME Types? (wouldn't be good for interoperability)
  • How do I register MIME types?
  • How do I resolve MIME types in the <intent-filter> ?

I know this is an old thread, but hopefully someone has figured this out by now and can point me in the right direction. I am also of the opinion that Intent's and Intent-Receivers/Intent-Filters don't have a lot of simple-to-understand documentation available... which makes the novices entry into Android a little bit harder.

Anyways, love the work you are doing with the forums, +-, I have found the forums to be a valuable resource.
Regards,
Timbobsteve
timbobsteve
Freshman
Freshman
 
Posts: 4
Joined: Tue Sep 23, 2008 8:58 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 15 guests